c programming Operators

Operators are ‘C’ tokens which can join together individual constants, variables array elements, and function references. Operators act upon data items called as operands.

Classification

Arithmetic Operators
Arithmetic operator Meaning
+ Adding or unary plus
Subtraction or unary minus
* Multiplication
/ Division
% Modulo division

Integer arithmetic: Both the operands are an integer in integer arithmetic. It always yields an integer value.
Ex: int a=10,b=5;  a+b;  a-b; a*b; a/b ; a%b etc.

Real arithmetic: Both the operands are real in real arithmetic. It yields real values as a result. It cannot be applied to the % operator.
Ex: 

float a=10.0, b=5.0; 
a+b=10.0+5.0=15.0 i.e. 15.000000 
a-b= 10.0-5.0 =5.0 i.e. 5.000000 
a*b= 10.0*5.0 =50.0 i.e. 50.000000 
a/b= 10.0/5.0 = 2.0 i.e. 2.000000 
a%b=10.0%5.0 (invalid expression)

Mixed Mode Arithmetic: when one operand is an integer and other is real it is known as mixed mode arithmetic. Here the result will always be real.
Ex: 

int a=10; 
float b=5.0; 
a+b=10+5.0=15.0 
a-b = 10-5.0 =5.0 
a*b = 10*5.0 =50.0 
a/b = 10/5.0 = 2.0 
a%b=10%5.0 (invalid expression)
Relational Operators

These are used to compare two quantities. These operators always return 1(true) or 0 (false). If the expression is true then it returns with 1 else 0

Relational Operator  Action
< 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

 

Expression Result
10<5 0 (false)
10>5 1 (true)
10==15 0 (false)
10!=15 1 (true)
(10+5)==(3*5) 1 (true)
Logical Operators
logical Operator      Action
&& logical AND
|| logical OR
! logical NOT

Syntax:exp1 && exp2;

Syntax: exp1 || exp2;

.Syntax:!exp1;
Ex:

int a=10,b=15, c=20,i; 
i= (a>b) && (b<c); //The value of i in this expression will be 0. 
i= (a<b) && (b<c); //The value of i in this expression will be 1. 
i= (a>b) || (b<c); //The value of i in this expression will be 1. 
i= ! (a>b); //The value of i in this expression will be 1.
Assignment Operators

The operators are used to assign the result of an expression to a variable
Syntax: Variable = exp;

Assignment Operator  Ex: int a=10,b=5; Action
a=a+1; or a+=1; adds 1 to a and assigns the value to a
a=a-1; or a-=1; decrements a by 1 and assigns the value to a
a=a/(b+1) or a/=b+1; divides a by (b+1) and assigns the result in a
a*=b+2; or a=a*(b+2) multiplies (b+2) with a and assigns the result in a

Increment and Decrement operators

Operator (int a,b) Action Result
a=1;  b=++a; pre-increment increments a value by 1 and assigns the value to b. i.e., a=2 and b=2
a= 1;  b=a++; post-increment Assigns the value to b then increments a value by 1. i.e., a=2 and b=1
a=2;  b=–a; pre-decrement decrements a value by 1 and assigns the value to b. i.e., a=1 and b=1
a=2;  b=a–; post-decrement Assigns the value to b then decrements a value by 1. i.e., a=1 and b=2
Conditional Operator

Ex:

int a=5,b=10,c=15,y; 
y=(a>b) ? b : c; //The value of y is 15 
y=(a<b) ? b : c; //The value of y is 10

Ex2:

/* Greatest of three numbers using conditional operation */ 
int a=5,b=10,c=15,y; 
y=(a>b && a>c) ? a: (b>c) ? b: c; //Here greatest of three numbers is stored in x;
Bitwise Operators

Bitwise operators are similar to that of logical operators except that they work on binary bits. When bitwise operators are used with variables, they are internally converted to binary numbers and then bitwise operators are applied on individual bits.

Bitwise operators do manipulation on bit stored in memory. These operators work with char and int type only. They cannot use floating point numbers.

Bitwise Operators Action
~ 1’s compliment
| Bitwise OR
^ Bitwise exclusive OR
& Bitwise AND
<< Left shift
 >> Right shift

Ex:

int a=10,b=8; //Assum that int size is 2 bytes 
/* The equivalent binary value of a is 0000 0000 0000 1010 
   The equivalent binary value of b is 0000 0000 0000 1000 
*/ 
printf("%d", a & b); // The value of a & b = 0000 0000 0000 1000 i.e.,8 

printf("%d", a|b); // The value of a | b = 0000 0000 0000 1010 i.e., 10 

printf("%d", a^b); //The value of a ^ b= 0000 0000 0000 0010 i.e., 2 

printf("%d" ,a<<2); 
/* 
     a(10) binary is 0000 0000 0000 1010 a<<2. 
     It left shift the binary bits twice. i.e.,0000 0000 0010 1000 i.e. 40 */ 

printf("%d", a>>2); 
/* 
    a(10) binary is 0000 0000 0000 1010 a>>2. 
    It right shifts the binary bits twice. i.e., 0000 0000 0000 0010 i.e. 2 
*/
Special Operator

There are some special operators available in ‘C’ such as sizeof operator and member selection operators (. and →).

sizeof Operator: It returns numbers of bytes the operand occupies; operand may be a variable, a constant, or a data type qualifier.
Syntax: sizeof(operand)
Ex:

int y; 
y= sizeof (int); //here value of y will be 2 byte it its 16bit compiler

Member Operators: These are used to access members of structure and unions.

Operators priority wise

Operators Operation Clubbing Priority
()

[]

->

.

Function call

Square bracket

Structure operator

Structure operator

Left to Right 1st
+

++

!

~

*

&

sizeof

type

Unary plus

Unary minus

Increment

Decrement

Not operator

Ones complement

Pointer operator

Address operator

Size of an object

Type case

Right to Left 2nd
*

/

%

Multiplication

Division

Modular division

Left to Right 3rd
+

_

Addition

Subtraction

Left to Right 4th
<<

>>

Left shift

Right shift

Left to Right 5th
<

<=

>

>=

Less than

Less than or equal to

Greater than

Greater than or equal to

Left to Right 6th
==

!=

Equality

Inequality

Left to right 7th
& Bitwise AND Left to right 8th
^ Bitwise XOR Left to right 9th
| Bitwise OR Left to right 10th
&& Logical AND Left to right 11th
|| Logical OR Left to right 12th
? : Conditional operator Right to left 13th
=, +=,-=,*=, /=, %=, &=,^=, |=, <<=, >>= Assignment operators Right to left 14th

Expressions

An expression in C is constructed using operators, variables and or constants.

Ex:

x+y*3.5 
x<y x<y && x>5