C Programming Operators
Introduction to Operators
Operators are symbols that perform operations on one or more operands. In C, operators are
used to manipulate data and control the flow of a program.
Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations.
Addition: + (e.g., No1 + No2)
Subtraction: - (e.g., Value - No3)
Multiplication: * (e.g., Num1 * 3)
Division: / (e.g., H1 / 5)
Remainder (Modulo): % (e.g., No1 % no2)
Example Calculation:
Let's calculate the result of 10 / 3 * 2 + 10 % 3.
1. Division: 10 / 3 = 3 (Integer division truncates the decimal part).
2. Remainder: 10 % 3 = 1.
3. The expression becomes 3 * 2 + 1.
4. Multiplication: 3 * 2 = 6.
5. The expression becomes 6 + 1.
6. Addition: 6 + 1 = 7.
So, 10 / 3 * 2 + 10 % 3 evaluates to 7.
Operator Precedence and Associativity
Operator Precedence: Determines the order in which operators are evaluated in an
expression. Operators with higher precedence are evaluated before operators with lower
precedence.
Operator Associativity: Determines the order in which operators of the same precedence are
evaluated. It can be left-to-right or right-to-left.
Parentheses (): Can be used to override the default precedence and associativity, forcing a
specific order of evaluation.
Precedence and Associativity Table:
Order Operator(s) Associativity
1 () Left to Right
2 *, /, % Left to Right
3 +, - Left to Right
Example of Precedence and Associativity:
Let's evaluate the expression: 55 / 10 % 2 * 5 - 10 % (4 - 2)
1. Parentheses: (4 - 2) evaluates to 2. The expression is now: 55 / 10 % 2 * 5 - 10 % 2
2. Division and Remainder (Left to Right):
55 / 10 evaluates to 5 (integer division). The expression is now: 5 % 2 * 5 - 10 % 2
5 % 2 evaluates to 1. The expression is now: 1 * 5 - 10 % 2
10 % 2 evaluates to 0. The expression is now: 1 * 5 - 0
3. Multiplication (Left to Right):
1 * 5 evaluates to 5. The expression is now: 5 - 0
4. Subtraction:
5 - 0 evaluates to 5.
The final result is 5.
Equality and Relational Operators
These operators compare two expressions and return a boolean value (true or false, represented
as 1 for true and 0 for false in C).
Equal to: == (e.g., x == y)
Not Equal to: != (e.g., x != 2)
Greater than: > (e.g., x > 6)
Less than: < (e.g., x < y)
Greater than or equal to: >= (e.g., x >= 9)
Less than or equal to: <= (e.g., x <= y)
Operator Precedence with Equality and Relational
Operators:
Order Operator(s) Associativity
1 () Left to Right
2 *, /, % Left to Right
3 +, - Left to Right
4 <, <=, >, >= Left to Right
5 ==, != Left to Right
Logical Operators
Logical operators are used to combine or modify conditions, creating more complex boolean
expressions.
Logical AND:
&&
(Returns true if both operands are true)
Example: A == 1 && B == 2
Logical OR:
||
(Returns true if at least one operand is true)
Example: Mid == 70 || Final == 70
Logical NOT:
!
(Reverses the boolean value of its operand)
Example: ! (grade == 'F')
Assignment Operators
Assignment operators are used to assign values to variables.
Simple Assignment: = (e.g., variable = expression;)
There are also shorthand assignment operators that combine an arithmetic operation with
assignment. The general form is variable operator= expression;, which is equivalent to variable =
variable operator expression;.
Addition Assignment: += (e.g., a += 2 is the same as a = a + 2)
Subtraction Assignment: -= (e.g., a -= 2 is the same as a = a - 2)
Multiplication Assignment: *= (e.g., a *= 2 is the same as a = a * 2)
Division Assignment: /= (e.g., a /= 2 is the same as a = a / 2)
Remainder Assignment: %= (e.g., a %= 2 is the same as a = a % 2)
Increment and Decrement Operators
These operators are used to increase or decrease the value of a variable by one.
Increment Operator: ++
Decrement Operator: --
These operators can be used in two ways:
Prefix:
The operation is performed
before
the variable's value is used in the expression.
k = ++n; // n is incremented first, then its new value is assigned to k.
k = --n; // n is decremented first, then its new value is assigned to k.
Postfix:
The variable's value is used in the expression
before
the operation is performed.
k = n++; // k is assigned the current value of n, then n is incremented.
k = n--; // k is assigned the current value of n, then n is decremented.
Example with Increment/Decrement:
Let x = 5 and y = 10.
1. x += 2:
This is equivalent to x = x + 2.
x becomes 5 + 2 = 7.
Result: x is now 7.
2. printf("%d\n", x++); printf("%d\n", x);:
First printf: x++ is a postfix increment. The current value of x (7) is printed. Then x is
incremented to 8.
Output of first printf: 7
Second printf: Prints the current value of x, which is now 8.
Output of second printf: 8
Result: x is now 8.
3. printf("%d\n", ++y); printf("%d\n", y);:
First printf: ++y is a prefix increment. y is incremented first to 11. Then its new value (11) is
printed.
Output of first printf: 11
Second printf: Prints the current value of y, which is 11.
Output of second printf: 11
Result: y is now 11.