C Operator
C Operator
► Arithmetic Operators
► Relational Operators
► Shift Operators
► Logical Operators
► Bitwise Operators
► Ternary or Conditional Operators
► Assignment Operator
► Misc Operator
Precedence of Operators in C
► The precedence of operator species that which operator will be evaluated first and next. The associativity
specifies the operator direction to be evaluated; it may be left to right or right to left.
► Let's understand the precedence by the example given below:
►
x +andy+ is an operator and calculating sum is an operation.
Here x and y are operands
► Operators which require two operands are called binary operators and which takes single operand are called
unary operators.
Precedence of Operators in C
► The precedence and associativity of C operators is given below:
Category Operator Associativity
Postfix () [] -> . ++ - - Left to right
Unary + - ! ~ ++ - - (type)* & sizeof Right to left
* Multiplication
/ Division
% Modulo division
Relational Operator in C
► Relational operators are used to compare two operators depending on their relation.
► For example, for comparing the price of two things. The value of a relational operator is either 1 or 0. If the
specified relation is true then 1 else 0.
a>b
► Here, > is a relational operator and it will return 1 if a is greater than b else it will return 0.
Relational operator Meaning
< is less than
<= is less than or equal to
> is greater than
>= is greater than or equal to
== is equal to
!= is not equal to
Logical Operator in C
► Logical operators are used when more than one condition is tested. They are used to combine more than
one relational expressions.
► For example:
a > b && c == 1
► Here && is relational operator used to combine two relational expressions a > b and c == 1.
Logical operator Meaning
&& logical AND
|| logical OR
! logical NOT
Note: Don’t get confused between equality operator == and assignment operator =
Bitwise Operator in C
► Bitwise operators are used to manipulate data at a bit level. They are used for testing or shifting the bit.
• If the operator is used before the variable i.e. ++a then it is called prefix increment operator.
• If the operator is used after variable i.e. a++ then it is called postfix increment operator.
• In the prefix operator, first 1 is added and then the value is assigned to the variable.
• In postfix operator, first the value is assigned then only 1 is added and the added value is assigned.
Operator Sample expression Explanation
x is increased by 1, then use the
++ ++x
value of x
Comma operator in C
► It is used to combine more than one expression. A comma linked list of expressions are evaluated left to
right.
sizeof operator in C
• sizeof operator is an operator which when used with operand returns the number of bytes occupied by the
operand.
For example
x = sizeof( a );
Here, the size occupied by variable a will be assigned to x.