Tag Archives: C operators

C operators and expressions

An operator is a symbol that tells computer to perform certain mathematical and logical operations on identifier. Operators are used in program to manipulate data and variables. C language provide following type of operators:

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operator
  • Increment and Decrement Operators
  • Conditional Operators
  • Misc Operators

We will go through all operators one by one.

All below examples are performed on integer variable, output may differ on float variable.

Arithmetic Operators :

Consider A=5 ,B=2 .

S.No. Operator Operation Example
1. + Addition A+B will give 7
2. Subtraction A-B will give 3
3 * Multiplication A*B will give 10
4 / Divide A/B will give 2
5 % Modulo division A%B will give 1

Relational Operators :

S.No. Operator Example Output
1. > A>B True
2. < A<B False
3. >= A>=B True
4. <= A<=B False
5. == A==B False
6. != A!=B True

Logical Operators :

Let A=1,B=0

S.No. Operator Description Example
1. && AND operator A && B will give False (0)
2. || OR Operator A||B will give True (1)
3. ! Not Operator !A will give False (0)

Bitwise Operators:

 x

y

 x|y

x & y

x ^ y

 Operator  Explanation

0

0

0

0

0

&  Bitwise AND

0

1

1

0

1

|  Bitwise OR

1

0

1

0

1

~  Bitwise NOT

1

1

1

1

0

^  XOR
<<  Left Shift
>>  Right Shift

Assignment Operators :

Operator
Example
 Explanation 
 
Simple assignment operator
 
 =
sum = 10
Value 10 is assigned to variable sum
Compound assignment operator 
+=
sum+=10
This is same as  sum = sum+10
 -=
sum – = 10
This is same as sum = sum – 10
 *=
sum*=10
This is same as sum = sum*10
 /=
sum/=10
This is same as sum = sum/10
%=
sum%=10
This is same as sum = sum%10
&=
sum&=10
This is same as sum = sum&10
^=
sum^=10
This is same as sum = sum^10

 

Increment and Decrement Operator :

Increment and Decrement operator are used to increment and decrement variable value by one .

Operators are :

Post increment : varname ++

Pre increment : ++varname

Post decrement: varname–

Pre decrement: –varname

example:

i=1;

i++; // This means i=i+1 ;

i=1;

i–; //This means i=i-1;

 

Conditional Operator :

_?_:_;

Conditional operator returns value depending on truth of condition

eg.

i=9;

a=(i>10?10:11);

in this example i=9 which is less than 10 so condition is false so value assigned to a will be 11;if condition is true than the assigned value will be 10;

Misc Operators:

 S.No
 Operators 
Description
1
&
This is used to get the address of the variable.
Example:        &a will give address of a.
2
*
This is used as pointer to a variable.
Example: * a  where, * is pointer to the variable a.
3
Size of ()
This gives the size of the variable.
Example: size of (char) will give us 1.