Unary Operator:
--> Unary expressions are formed by combining a unary operator with a single operand.
--> All unary operators are of equal precedence and have right-to-left associativity.
Example:
int a = 1;int b = ++a; // b = 2Example:
int a = 1;int b = a++; // b = 1int c = a; // c = 2
Arithmetic Operator:
--> An arithmetic operator performs mathematical operations such as addition, subtraction, multiplication, division etc on numerical values.
Example:
C program to demonstrate working of binary arithmetic operators
int main()
{
int a = 10, b = 4, res;
// printing a and b
printf("a is %d and b is %d\n", a, b);
res = a + b; // addition
printf("a+b is %d\n", res);
res = a - b; // subtraction
printf("a-b is %d\n", res);
res = a * b; // multiplication
printf("a*b is %d\n", res);
res = a / b; // division
printf("a/b is %d\n", res);
res = a % b; // modulus
printf("a%b is %d\n", res);
return 0;
}
OUTPUT:
a is 10 and b is: 4
a+b is: 14
a-b is: 6
a*b is: 40
a/b is: 2
a%b is: 2
Relational Operator:
--> Relational operators are used to find the relation between two variables. i.e. to compare the values of two variables in a C program.
Example:
#include <stdio.h>OUTPUT:
int main()
{
int m=40,n=20;
if (m == n)
{
printf("m and n are equal");
}
else
{
printf("m and n are not equal");
}
}
m and n are not equal
Logical Operator:
--> These operators are used to perform logical operations on the given expressions.
--> There are 3 logical operators in C language. They are, logical AND (&&), logical OR (||) and logical NOT (!).
Example:
#include <stdio.h>OUTPUT:
int main()
{
int m=40,n=20;
int o=20,p=30;
if (m>n && m !=0)
{
printf("&& Operator : Both conditions are true\n");
}
if (o>p || p!=20)
{
printf("|| Operator : Only one condition is true\n");
}
if (!(m>n && m !=0))
{
printf("! Operator : Both conditions are true\n");
}
else
{
printf("! Operator : Both conditions are true. " \
"But, status is inverted as false\n");
}
}
&& Operator : Both conditions are true
|| Operator : Only one condition is true
! Operator : Both conditions are true. But, status is inverted as false
Bitwise Operator:
--> Bitwise operators are used for manipulating a data at the bit level
--> Bit-level programming mainly consists of 0 and 1.
Example:
#include <stdio.h>
int main()
{
int a = 20; /* 20 = 010100 */
int b = 21; /* 21 = 010101 */
int c = 0;
c = a & b; /* 20 = 010100 */
printf("AND - Value of c is %d\n", c );
c = a | b; /* 21 = 010101 */
printf("OR - Value of c is %d\n", c );
c = a ^ b; /* 1 = 0001 */
printf("Exclusive-OR - Value of c is %d\n", c );
getch();
}
OUTPUT:
AND - Value of c is 20
OR - Value of c is 21
Exclusive-OR - Value of c is 1
Assignment Operator:
--> Assignment Operator is Used to assign value to an variable.
--> Assignment Operator have Two Values – L-Value and R-Value.Operator copies R-Value into L-Value.
--> Assignment Operator is binary operator which operates on two operands.
Example:
#include<stdio.h>
int main() {
int res;
res = 11;
res += 4;
printf("\nResult of Ex1 = %d", res);
res = 11;
res -= 4;
printf("\nResult of Ex2 = %d", res);
res = 11;
res *= 4;
printf("\nResult of Ex3 = %d", res);
res = 11;
res /= 4;
printf("\nResult of Ex4 = %d", res);
res = 11;
res %= 4;
printf("\nResult of Ex5 = %d", res);
return 0;
}
OUTPUT:
Result of Ex1 = 15
Result of Ex2 = 7
Result of Ex3 = 44
Result of Ex4 = 2
Result of Ex5 = 3
Ternary or conditional Operator:
--> Programmers use ternary operators in C for decision making inplace of conditional statements if and else.
--> The ternary operator is an operator that takes three arguments. The first argument is a comparison argument, the second is the result upon a true comparison, and the third is the result upon a false comparison.
--> If it helps you can think of the operator as shortened way of writing an if-else statement.
int a = 10, b = 20, c;OUTPUT:
c = (a < b) ? a : b;
printf("%d", c);
10
Practice Questions
Question 1
#include<stdio.h>
main()
{
int a;
a=--2;
printf("a=%d",a);
}
Question 2
#include<stdio.h>
main()
{
int i=2,j=3,k,l;
float a,b;
k=i/j*j;
l=j/i*i;
a=i/j*j;
b=j/i*i;
printf("k=%d l=%d a=%f b=%f \n",k,l,a,b);
}
Question 3
#include<stdio.h>
main()
{
int x=3,y=5,z;
z=x&&y;
printf("\n z=%d",z);
z=x&y;
printf("\n z=%d",z);
}
Question 4
#include<stdio.h>
main()
{
int x=7,y=10,a,b;
a=x||y;
printf("\n a=%d",a);
b=x|y;
printf("\n b=%d",b);
}
Question 5
#include<stdio.h>
main()
{
int x=3,y,z;
y=x=10;
z=x<10;
printf("\n x=%d y=%d z=%d",x,y,z);
}
Question 6
#include<stdio.h>
main()
{
int k=35;
printf("\n%d%d%d",k==5,k=50,k>40);
k=- -2;
printf("\n %d",k);
}
Question 7
#include<stdio.h>
main()
{
int x,y;
scanf("%d",&x);
y=(x<0)?-x:x;
printf("absolute value of %d is %d \n",x,y);
}
Question 8
#include<stdio.h>
main()
{
int x=2,y=8,z=6;
x=y==z;
printf("\n%d\n",x);
}
Question 9
#include<stdio.h>
main()
{
int x=4,y,z;
y=--x;
z=x--;
printf("\n %d %d %d",x,y,z);
}
Question 10
#include<stdio.h>
main()
{
int x=4,y=3,z;
z=x-- - y;
printf("%d %d %d",x,y,z);
}
Question 11
#include<stdio.h>
main()
{
int a=3,b=4,c=1;
b=(a>b?(a>c?a:c):(b>c?b:c));
printf("%d",b);
}
Question 12
#include<stdio.h>
main()
{
int x=5;
printf("%d %d %d\n",x,x<<2,x>>2);
}
Question 13
#include<stdio.h>
main()
{
int x=20,y=35;
x=y++ + x++;
y=++y + ++x;
printf("%d %d\n",x,y);
}
Question 14
#include<stdio.h>
main()
{
printf("\n %d %d %d\n",sizeof('4'),sizeof("4"),sizeof(4));
}