Operators in C Conditional Operator Special Operator
Conditional Operator As conditional operator works on three operands, so it is also known as the ternary operator. The behavior of the conditional operator is like the if-else statement as 'if-else' statement is also a decision-making statement. The conditional statements are the decision-making statements which depends upon the output of the expression. It is represented by two symbols, i.e., '?' and ':’. Syntax: variable = Expression1 ? Expression2 : Expression3
Examples Consider two variables A=10 and B=20 Max = (A>B) ? A : B Consider three variables X=10, Y=40 and Z=35 Max= (X>Y) ? ( (X>Z) ? X : Z ) : ( (Y>Z) ? Y: Z ) True False
Special Operators Some other special operators which are supported by C are- Operator Description Example sizeof () The sizeof operator will return the size of the variable as result. If a is an integer variable, then sizeof (a); will return result of 4 or 2 according to compiler. & The address operator will return the address of the variable as result. If a is a variable, then &a will return the actual memory location of the variable a. * This operator is used as the pointer to the variable If int a=5, *p; and P=&a; then *p will return 5
Precedence and Associativity The precedence of operators determines which operator is executed first if there is more than one operator in an expression. For example : int a = 14-2*3; 8 The associativity of operators determines the direction in which an expression is evaluated. For example: a = b; Value of b is assigned to a. because the associativity of the = operator is from right to left.
Category Operator Associativity Postfix () [] -> . ++ - - Left to right Unary + - ! ~ ++ - - (type)* & sizeof Right to left Multiplicative * / % Left to right Additive + - Left to right Shift << >> Left to right Relational < <= > >= Left to right Equality == != Left to right Bitwise AND & Left to right Bitwise XOR ^ Left to right Bitwise OR | Left to right Logical AND && Left to right Logical OR || Left to right Conditional ?: Right to left Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left Comma , Left to right The operators with the highest precedence are listed at the top of the table.