In C programming, operators are symbols or special characters that perform operations on variables and values. They form the building blocks of calculations and logical decision-making, enabling developers to write efficient and functional code. Operators simplify tasks like arithmetic computations, comparisons, logical operations, and memory manipulations.
C provides a variety of operators, grouped into the following categories:
Simple program on Arithmetic operators:
#include
int main()
{
int a=10;
int b=3;
printf(“%d + %d ==>%d\n”,a,b,a+b);
printf(“%d -%d ==>%d\n”,a,b,a-b);
printf(“%d * %d ==>%d\n”,a,b,a*b);
printf(“%d / %d ==>%d\n”,a,b,a/b);
printf(“%d %% %d ==>%d\n”,a,b,a%b);
return 0;
}
Output :
a+b=10
a-b=7
a*b=30
a/b=3
a%b=1
In C, relational operators are used to compare two values or expressions.
The result is typically represented as 1 for true and 0 for false.
Here’s a table that summarizes the relational operators in C:
#include
int main()
{
int a=10, b=3;
printf(“%d ==%d==>%d\n”,a,b,a==b);
printf(“%d !=%d==>%d\n”,a,b,a!=b);
printf(“%d <=%d==>%d\n”,a,b,a<=b);
printf(“%d >=%d==>%d\n”,a,b,a>=b);
printf(“%d <%d==>%d\n”,a,b,a<b);
printf(“%d >%d==>%d\n”,a,b,a>b);
return 0;
}
#10 ==3==>0
10 !=3==>1
10 <=3==>0
10 >=3==>1
10 <3==>0
10 >3==>1
#include
int main()
{
int a=10, b=10;
printf(“%d &&%d==>%d\n”,a<b,a>b,a<b&&a>b);
printf(“%d ||%d==>%d\n”,a<b,a>=b,a=b);
printf(“%d ! %d==>%d\n” ,a,b, a!=b);
return 0;
}
Bitwise operators work at the bit level
perform operations directly on the binary representation of integers.
int x = 5; // Binary: 0101
int y = 3; // Binary: 0011
printf(“x & y = %d\n”, x & y); // Output: 1 (Binary: 0001)
printf(“x | y = %d\n”, x | y); // Output: 7 (Binary: 0111)
printf(“x ^ y = %d\n”, x ^ y);
printf(“~x = %d\n”, ~x); // Output: -6 (Binary: Inverted 0101)
printf(“x << 1 = %d\n”, x << 1); // Output: 10 (Binary: 1010)
printf(“x >> 1 = %d\n”, x >> 1); // Output: 2 (Binary: 0010)
Assigning the value to variable.
int a = 10;
a += 104; // Same as a = a + 104; ->114;
a -= 104; // Same as a = a – 104; -> a = -94;
a *= 2; // Same as a = a * 2; -> a = 24
a /= 4; // Same as a = a / 4; -> a = 6
a %= 3; // Same as a = a % 3; -> a = 0
printf(” %d\n”, a);
}
This category includes miscellaneous operators like the conditional (ternary), sizeof, comma, and type casting operators.
int x = 10, y = 20;
int max = (x > y) ? x : y;
printf(“Max value = %d\n”, max); // Output: 20
printf(“Size of int = %zu bytes\n”, sizeof(int)); // Output: 4 (typically)
int a = (x > y, y + 5);
printf(“Value of a = %d\n”, a);
float z = (float)x / 3; // Type Casting: Converts x to float for division.
printf(“Value of z = %.2f\n”, z); // Output: 3.33
Indian Institute of Embedded Systems – IIES