What are operators in C?
Declaration gives the list of variables with a different type of values to be used
The operator is something which tells what to be done to the declared variables.
Every operator in c has
- Priority
- Associativity
Why 6/5 gives 1 not 1.2 in C program?
Do you know? if we calculate 6/5 gives 1 not 1.2 in C. first of all,4/8 gives 1 not 0.5. second, Do you know why? third, if values are integers it evaluate as int
if one value is int and another value is float then it evaluates the result as float.
6.5/5.5 gives 1.1818
Have you ever checked arithmetic in c. if not, you can check now
#include <stdio.h>
int main()
{
int k;
k=66/30;
printf("%d",k);
return 0;
}
Observe the results carefully. plus, notice the arithmetic in C is different, if we compared with normal arithmetic.
if both the values are integers, C evaluate result as int not float
Precedence and order of evaluation table
Arithmetic Operators
Note: the modulus % and divide / is not same, % gives remainder and divide / gives quotient
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x=6;
int y=2;
int modulus;
int divide;
{
modulus=x%y;
divide =x/y;
}
printf(" modulus=%d \n",modulus);
printf(" divide=%d \n",divide);
return 0;
}
Output
modulus = 0
divide = 3
Logical Operators
Logical NOT(!)
Logical OR
Logical Operators
Equality Operators
Relational Operator
Unary operator’s
Unary operators are designed for single operands and There are three unary operator’s
Increment operator and decrement operator
Operator | Meaning |
++ | Increase value by 1 |
– – | Decrease value by 1 |
Variants | Example |
Prefix | ++ x or – -x |
Postfix | x+ + or x- – |
#include <stdio.h>
#include <stdlib.h>
int main()
{
int e, f,g,h, a=2, b=2, c=2,d=2; //Initializing variables
e = a++; //postfix
f = b--; //postfix
g = ++c; //prefix
h = --d; //prefix
printf("e=%d f=%d g=%d h=%d",e,f,g,h); //Print
return 0;
}
Output
e=2 f=2 g=3 h=1
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a=2; //Initializing variable
printf("a=%d",a++);
printf("a=%d",a); //Print
return 0;
}
Output
a=2 a=3
Note: a++(postfix increment) and ++a(prefix increment) is not same similary a – -(postfix Decrement) and – -a(prefix Decrement) is not same
In prefix expression : the operator(++a (or) – -a) is applied in front of operand(a)
In postfix expression : the operator(++a (or) – -a) is applied after the operand(a)
Unary Minus(-)
#include <stdio.h>
#include <stdlib.h>
int main()
{
int c, a=2, b=2; //Initializing variable a with 2, variable b with 2 and c
c =(a+b); //Add the two variable
c=-(c); //Unary Minus
printf("%d",c); //Print C variable
return 0;
}
Output
-4
Conditional operator(?:)
Syntax
exp1 ? exp2:exp3
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x,y; //Initializing variable
scanf("%d",&x);
y=(x>4?2:5);
//printf("a=%d",a++);
printf("y=%d",y); //Print
return 0;
}
Output
if x>4 then y=2
if x<4 then y=5
sizeof Operator
The sizeof Operator is designed to give the size of an operand
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x=100, result; //Initializing variable
result=sizeof(x);
printf("result=%d",result); //Print
return 0;
}
Output
result =4
Bitwise operators
Bitwise operators work on bit level not on byte( int, char)
Bitwise AND (&)
Truth table
find a&b, if b=4, a=4
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a=4, b=4, c;
c= a&b;
printf("c=%d", c);
return 0;
}
Output
c= 4
Remember & and && is not the same because & is a bitwise operator and && is a logical operator
Bitwise OR(|)
Truth table
what is a|b? if a=4, b=4
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a=4, b=4, c;
c= a|b;
printf("c=%d", c);
return 0;
}
Output
c=4
Bitwise XOR(^)
Truth table
find the value of a^b, if a=4, b=4
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a=4, b=4, c;
c= a^b;
printf("c=%d", c);
return 0;
}
Output
c = 0
Bitwise NOT(~)
The ~ operator designed to perform only on one variable
find the value of ~a, if a=4
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a=4, c;
c= ~a;
printf("c=%d", c);
return 0;
}
Output
c=-5
Bitwise leftshift(<<)
solve the left shift (<< 2) of a, when a=4
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a=4, c;
c= a<<2;
printf("c=%d", c);
return 0;
}
Output
c =16
Bitwise right shift(>>)
find right shift(>>2) of a, if a=4
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a=4, c;
c= a>>2;
printf("c=%d", c);
return 0;
}
Output
c=1
Leave a Reply