C Operators The ANSI C language has a rich set of operators, which give it great expressiveness. ------------------------------------------------------------------------------ Operator type Associativity ----- () [] -> . left to right ! ~ ++ -- + - (type) * & sizeof unary right to left * / % binary arithmetic left to right + - binary arithmetic left to right << >> binary bitwise left to right < <= > >= binary relational left to right == != binary relational left to right & binary bitwise left to right ^ binary bitwise left to right | binary bitwise left to right && binary logical left to right || binary logical left to right ?: trinary right to left = += -+ etc. binary assignment right to left , binary left to right 1) Parentheses "("and ")" have their normal use establishing precedence of expressions. 2) Square Brackets "[" and "]" are used to indicate array subscripting. 3) Structure Referencing Operators "." and "->" are used to reference structure members. 4) Increment and Decrement unary Operators "++" and "--" are the increment and decrement operators. They can be used as prefix or postfix operators. They can only be applied to an expression yielding an r-value, usually a variable, which can be of arithmetic or pointer type. 5) Bitwise Operators "~" one's complement (unary) "<<" left shift ">>" right shift "&" bitwise and "^" bitwise exclusive or "|" bitwise or 6) Arithmetic Operators "-" (unary), "*", "/", "+" and "-" have their normal meanings as arithmetic operators on arithmetic expressions. The binary "+" and "-" can also be used in "pointer arithmetic". The "%" operator is the mod. 7) Relational Operators "<", "<=", ">" and ">=" are "less than", "less than or equal to", "greater than" and "greater than or equal to". "!=" and "==" are "not equal" and "equal". These comparisons can be applied to arithmetical or pointer expressions. 8) Logical Operators "!", "&&" and "||" are the logical operators for "unary negation", "and" and "or". Note that an expression which is an operand of a logical operators is: TRUE if ( ) != 0 FALSE if ( ) == 0 The logical operators return a 0 or 1. The "&&" and "||" have the useful property that evaluation is assured to be from left to right and terminated when the result can be determined. 9) Pointer Operations "&" and "*" as unary prefix operators provide the "address of" and "contents of" operators. 10) Cast Operator. "(type)" , where type is any valid c type, provides for explicit conversion of an expression to the specified type. 11) sizeof Operator "sizeof" is an unary operator that may be applied to any type or variable. It results in the number of bytes used in storing the object or type of object. When applied to a type parentheses around the type are required.. 12) Conditional Expression "?:" is a conditional expression. t = () ? () : (); is roughly equivalent to if( ) t = exp_2; else t = exp_3; 13) Assignment Operator The assignment operators are: "=", "*=", "/=", "*=", "%=", "+=", "-=", "<<=", "<<=", "&=", "|=" and "^=" The expression ( = ) has the value of , after the side effect = . The expression ( = ) has the value of , after the side effect = ; Comments of C operators. It is generally agreeded that the precedence of the C operators is wrong. The most common error this produces is forgetting to put parentheses around an assignment operator used in a relational expression (a very common C idiom). A second common error is to use "=" instead of "==" in trying to build a relational expression. In either of the cases above, the syntax may still be valid producing no compile or runtime error messages; but, the runtime behavior may not be what is expected.