Operators are symbols that tells a compiler to do certain arithematic or logic operations. Operators works on operands (literals). Example: a + b, here a and b are operands and + is an operator denoting addition operation.

C operators in order of precedence (highest to lowest). Operators on the same group or line have equal precedence.

Operator | Description | Type of Operation | Associativity |
---|---|---|---|

() [] . -> ++ -- |
Parenthesis Brackets Member selection via object name Member selection via pointers Postfix increment/decrement |
Expression | Left to Right |

sizeof & * + - ~ ! ++ -- (type) |
Determines size in bytes Address (of operand) Dereference Unary plus/minus Bitwise Complement Logical Negation Prefix increment/decrement Typecasts a value into a temporary value of type (type) |
Unary | Right to Left |

* / % | Multiplication/Division/Modulus | Multiplicative | Left to Right |

+ - | Addition/Subtraction | Additive | Left to Right |

<< >> | Bitwise left shift, Bitwise right shift | Bitwise shift | Left to Right |

< <= > >= |
Less than/Less than equal to Greater than/Greater than equal to |
Relational | Left to Right |

== != | Equal to/Not Equal to | Equality | Left to Right |

& | Bitwise-AND | Bitwise-AND | Left to Right |

^ | Bitwise-exclusive-OR | Bitwise-exclusive-OR | Left to Right |

| | Bitwise-inclusive-OR | Bitwise-inclusive-OR | Left to Right |

&& | Logical-AND | Logical-AND | Left to Right |

|| | Logical-OR | Logical-OR | Left to Right |

?: | Ternary Conditional Expression | Ternary Conditional Expression | Right to Left |

= += -= *= /= %= &= ^= |= <<= >>= |
Assignment Addition/subtraction assignment Multiplication/division assignment Modulus assignment, bitwise-AND assignment Bitwise-exclusive/inclusive-OR assignment Bitwise left/right shift Assignment |
Simple and Compound Assignments | Right to Left |

, | Comma | Sequential evaluation | Left to Right |

```
//Types of Operators in C with demonstration
#include <stdio.h>
struct car
{
int size;
int price;
}bmw,*toyota;
int sum(int a,int b)
{
return a+b;
}
int main(void)
{
//assignment operator =
int a = 20;
printf("Assignment a = %d\n",a);
int b = 10;
int res = 0;
//parenthesis operator ( )
int c = ( a + b );
printf("Parenthesis ()\n");
//Bracket operator []
int arr[5];
printf("Brackets []\n");
//Comma operator ,
res = (1,2);
printf("Comma as operator %d,%d = %d\n",1,2,res);
//Comma separator ,
res = sum(1,2);
printf("Comma as separator sum(%d,%d) = %d\n",1,2,res);
//Address of
toyota = &bmw;
printf("Address of operand &\n");
//pointer declaration
int *p;
p = &c;
//changing value of c by dereferencing
*p = 1;
printf("Dereference operator *\n");
//member select via object name
bmw.price = 10000;
printf("Member select via object name, bmw.price = %d\n",bmw.price);
//member select via pointers
printf("Member select via pointers, toyota->price = %d\n",toyota->price);
//size of
printf("Sizeof Operator, sizeof(int) = %d\n",sizeof(int));
//Typecast (type)
printf("Typecast (type), (int)3.4 = %d\n",(int)3.4);
//addition operator +
res = a + b;
printf("Addition %d + %d = %d\n",a,b,res);
//subtraction operator -
res = a - b;
printf("Subtraction %d - %d = %d\n",a,b,res);
//multiplication operator *
res = a * b;
printf("Multiplication %d * %d = %d\n",a,b,res);
//division operator /
res = a / b;
printf("Division %d / %d = %d\n",a,b,res);
//modulo operator (remainder) %
res = a % b;
printf("Modulo %d %% %d = %d\n",a,b,res);
//bitwise left shift operator <<
/*4 in binary 100
100 left shift 1 bit, 1000 => 8 in decimal
*/
res = 4 << 1;
printf("Bitwise left shift %d << %d = %d\n",4,1,res);
//bitwise right shift operator >>
/*4 in binary 100
100 right shift 1 bit, 10 => 2 in decimal
*/
res = 4 >> 1;
printf("Bitwise right shift %d >> %d = %d\n",4,1,res);
res = 4;
//bitwise left shift assignment operator <<=
res <<= 1;
printf("Bitwise left shift assignment %d <<= %d = %d\n",4,1,res);
res = 4;
//bitwise right shift assignment operator >>=
res >>= 1;
printf("Bitwise right shift assignment %d >>= %d = %d\n",4,1,res);
//bitwise AND operator &
/*4 in binary 100
100 & 001 = 000 => 0 in decimal
*/
res = 4 & 1;
printf("Bitwise AND %d & %d = %d\n",4,1,res);
//bitwise inclusive OR operator |
/*4 in binary 100
100 | 001 = 101 => 5 in decimal
*/
res = 4 | 1;
printf("Bitwise inclusive OR %d | %d = %d\n",4,1,res);
//bitwise XOR operator ^
/*4 in binary 100
100 ^ 001 = 101 => 5 in decimal
*/
res = 4 ^ 1;
printf("Bitwise XOR %d ^ %d = %d\n",4,1,res);
//bitwise Complement operator ~
/*2 in binary 0000 0010
~0000 0010 = 1111 1101 => -3 in decimal
*/
res = ~2;
printf("Bitwise Complement ~%d = %d\n",2,res);
//Logical AND &&
res = 0 && 1;
printf("Logical AND %d && %d = %d\n",0,1,res);
//Logical OR ||
res = 0 || 1;
printf("Logical OR %d || %d = %d\n",0,1,res);
//Logical Negation !
printf("Logical Negation !%d = %d\n",res,!res);
//Ternary Operator ?:
// result = condition?true:false;
res = (5<2)?1:0;
printf("Ternary Condition (%d<%d)?1:0 = %d\n",5,2,res);
if(1 == (10/10))
printf("Is Equal to ==\n");
if(1 != (20/10))
printf("Is NOT Equal to !=\n");
if(1 < 2)
printf("Less than <\n");
if(2 > 1)
printf("Greater than >\n");
if(2 >= 2)
printf("Greater than equal to >=\n");
if(2 <= 4)
printf("Less than equal to <=\n");
res = 0;
//res = res + 1;
res += 1;
printf("Addition Assignment res += 1 = %d\n",res);
res = 0;
//res = res - 1;
res -= 1;
printf("Subtraction Assignment res -= 1 = %d\n",res);
res = 10;
//res = res * 10;
res *= 10;
printf("Multiplication Assignment res *= 10 = %d\n",res);
res = 10;
//res = res / 10;
res /= 10;
printf("Division Assignment res /= 10 = %d\n",res);
res = 10;
//res = res % 2;
res %= 2;
printf("Modulo Assignment res %%= 2 = %d\n",res);
res = 10;
//res = res & 2;
res &= 2;
printf("Bitwise AND Assignment res &= 2 = %d\n",res);
res = 10;
//res = res | 2;
res |= 2;
printf("Bitwise Inclusive OR Assignment res |= 2 = %d\n",res);
res = 10;
//res = res ^ 2;
res ^= 2;
printf("Bitwise XOR Assignment res ^= 2 = %d\n",res);
res = 0;
//res = res + 1; first increment then assignment
printf("Prefix increment res = %d\n",++res);
res = 0;
//res = res + 1; first assignment then increment
printf("Postfix increment res = %d, ",res++);
printf("Later res = %d\n",res);
return 0;
}
```

```
Assignment a = 20
Parenthesis ()
Brackets []
Comma as operator 1,2 = 2
Comma as separator sum(1,2) = 3
Address of operand &
Dereference operator *
Member select via object name, bmw.price = 10000
Member select via pointers, toyota->price = 10000
Sizeof Operator, sizeof(int) = 4
Typecast (type), (int)3.4 = 3
Addition 20 + 10 = 30
Subtraction 20 - 10 = 10
Multiplication 20 * 10 = 200
Division 20 / 10 = 2
Modulo 20 % 10 = 0
Bitwise left shift 4 << 1 = 8
Bitwise right shift 4 >> 1 = 2
Bitwise left shift assignment 4 <<= 1 = 8
Bitwise right shift assignment 4 >>= 1 = 2
Bitwise AND 4 & 1 = 0
Bitwise inclusive OR 4 | 1 = 5
Bitwise XOR 4 ^ 1 = 5
Bitwise Complement ~2 = -3
Logical AND 0 && 1 = 0
Logical OR 0 || 1 = 1
Logical Negation !1 = 0
Ternary Condition (5<2)?1:0 = 0
Is Equal to ==
Is NOT Equal to !=
Less than <
Greater than >
Greater than equal to >=
Less than equal to <=
Addition Assignment res += 1 = 1
Subtraction Assignment res -= 1 = -1
Multiplication Assignment res *= 10 = 100
Division Assignment res /= 10 = 1
Modulo Assignment res %= 2 = 0
Bitwise AND Assignment res &= 2 = 2
Bitwise Inclusive OR Assignment res |= 2 = 10
Bitwise XOR Assignment res ^= 2 = 8
Prefix increment res = 1
Postfix increment res = 0, Later res = 1
```

Previous

Next

C Programming

Overview Hello World Installation Comments Header Files Data Types Tokens Operators Input/Output Format Specifiers Decision Control Loops Type Qualifiers Storage class specifier Arrays Enums Structures Unions Functions Recursions Inline Functions Pointers Pointer Arithematic Call by value and call by reference Linked Lists Passing of Arrays in Functions Preprocessor and Macros Mathematical Operations String Operations Bit Fields Command Line Arguments Variable Arguments Error Handling Type Casting Typedef File Handling Multi Processing Threads Outdated C Programming Memory Management Compilation C Programs Interview Questions MiscellaneousTrending

C Programming
Remember to follow community guidelines and don't go off topic.