
Table of Contents
ToggleC Programming Operators
There are many types of operators in C programming to perform arithmetic, consitional and bitwise operations. In this chapter you will learn about different types of C operators and their uses.
Table of Contents
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
Programming Operators
An operator is a type of symbol that can operate on values or variable. For example: + (plus sign) is an operator used for addition.
There are many types of operators in C programming to perform different types of operations. For better understanding, operators can be divided in the following ways.
Operators in C Programming
- C Arithmetic Operator - Arithmetic Operator
- C Increment and Decrement Operator - Increment and Decrement Operator
- C Assignment Operator - Assignment Operator
- C Relational Operator - Relational Operator
- C Logical Operator - Logical Operator
- C Bitwise Operator - Bitwise Operator
- C Conditional Operator - Coditional Operator
- C Special Operator - Special Operator
C is the Arithmetic Operator
Arithmetic Operators perform mathematical calculations such as addition, subtraction, multiplication and division.
Example: C Arithmetic Operator
// This example shows how arithmetic operators work in C programs.
#include
int main()
{
int a = 11,b = 5, c;
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c=a/b;
printf("a/b = %d \n",c);
c=a%b;
printf("Remainder when a divided by b = %d \n",c);
return 0;
}
a + b = 16
a - b = 6
a * b = 55
a / b = 2
Remainder when a divided by b=1
The +, and * operators correctly perform addition, subtraction and multiplication respectively. 11/5 Generally if you divide 11 by 5 () the quotient is 2.2 But the output of our program is 2.
Because both variables are of type Integer. So the output is also integer. The compiler truncates the decimal number after the decimal so the restult is 2 instead of 2.2.
Quotients are obtained with the modulo operator %. In the above example dividing by a = 11 k gives the quotient as 1 b = 5 The % operator is used only or integers.
Let a = 10.0, b = 4.0, c = 10 and d = 4 then C Programming
a/b = 2.5 //because both operands are floating point variables.
a/d = 2.5 //Because an operand is a floating point variable.
c/b = 2.5 //Because an operand is a floating point variable.
c/d = 2 // because both operands are integer variables.
C Increment and Decrement Operators
To increase or decrease the operand of consant or variable by 1, there are two operators in C programming, one is increment (increment) ++ and the other is decrement (decrement).
The increment operator increases the value of the variable by one whereas the decrement operator decreases the value by one. These two operators are called unary operators are called unary operators because they can operate on single operands.
Example: C Increment and Decrement Operators
// This example shows how increment and decrement operators work in C programs.
#include
int main()
{
int a = 15, b = 150;
float c = 50.5, d = 80.5;
printf("++a = %d \n", ++a);
printf("--b = %d \n", --b);
printf("++c = %f \n", ++c);
printf("--d = %f \n", --d);
return 0;
}
++a = 16
--b = 149
++c = 51.500000
--d = 79.500000
Here operators ++ and — are used as prefix. This operator can also be used as a postfix. Such as – a++ and a–. To learn more about increment and decrement and decrement operators visit the chapter Increment and Decrement Operators in C Programs.

C is the Assignment Operator
The assignment operator is used to assign or store a value to a variable. The most commonly used assignment operator is =
Example: C Assignment Operator
// This example shows how the assignment operator works in a C program.
#include
int main()
{
int a = 10, c;
c = a;
printf("c = %d \n", c);
c += a; // c = c + a
printf("c = %d \n", c);
c -= a; // c = c-a
printf("c = %d \n", c);
c *= a; // c = c*a
printf("c = %d \n", c);
c /= a; // c = c/a
printf("c = %d \n", c);
c %= a; // c = c%a
printf("c = %d \n", c);
return 0;
}
c = 10
c = 20
c = 10
c = 100
c = 10
c = 0
C is the Relational Operator
In C programming, relational operator (relational operator) checks the relationship between two operands. Returns 1 if relation is true; Returns 0 if relation is false.
Relational operators are used in decision making and loops.
Example: C Relational Operator
// This example shows how relational operators work in C programs.
#include
int main()
{
int a = 5, b = 5, c = 10;
printf("%d == %d = %d \n", a, b, a == b); // true
printf("%d == %d = %d \n", a, c, a == c); // false
printf("%d > %d = %d \n", a, b, a > b); //false
printf("%d > %d = %d \n", a, c, a > c); //false
printf("%d < %d = %d \n", a, b, a < b); //false
printf("%d < %d = %d \n", a, c, a < c); //true
printf("%d != %d = %d \n", a, b, a != b); //false
printf("%d != %d = %d \n", a, c, a != c); //true
printf("%d >= %d = %d \n", a, b, a >= b); //true
printf("%d >= %d = %d \n", a, c, a >= c); //false
printf("%d <= %d = %d \n", a, b, a <= b); //true
printf("%d <= %d = %d \n", a, c, a <= c); //true
return 0;
}
5 == 5 = 1
5 == 10 = 0
5 > 5 = 0
5 > 10 = 0
5 < 5 = 0
5 < 10 = 1
5 != 5 = 0
5 != 10 = 1
5 >= 5 = 1
5 >= 10 = 0
5 <= 5 = 1
5 <= 10 = 1
C is Ligical Operator
&&, II, in C programming and! The operators are called logical operators. Logical operators are called logical operators. Logical operators are commonly used in decision making in C programming.
Example: C Logical Operator
// This example shows how logical operators work in C programs.
#include
int main()
{
int a = 5, b = 5, c = 10, result;
result = (a = b) && (c > b);
printf("(a = b) && (c > b) equals to %d \n", result);
result = (a = b) && (c < b);
printf("(a = b) && (c < b) equals to %d \n", result);
result = (a = b) || (c < b);
printf("(a = b) || (c < b) equals to %d \n", result);
result = (a != b) || (c < b);
printf("(a != b) || (c < b) equals to %d \n", result);
result = !(a != b);
printf("!(a == b) equals to %d \n", result);
result = !(a == b);
printf("!(a == b) equals to %d \n", result);
return 0;
}
(a = b) && (c > b) equals to 1
(a = b) && (c < b) equals to 0
(a = b) || (c < b) equals to 1
(a != b) || (c < b) equals to 0
!(a != b) equals to 1
!(a == b) equals to 0
Explanation of the above example
- (a = b) And (c > b) since both operands are true. (a = b) && (c > 5) the result 1.
- Because the value of the operand (c < b) is false, (a = b) && (c < b) the result is 0.
- Because the value of the operand (a = b) is true, (a = b) || (c < b) the result is 1.
- (a != b) And (c < b) since both operands are false, (a !=b) || (c < b) the result is 0.
- Because the value of the operand (a != b) is false, !(a != b) the result is 1. Because !(a != b) has value true.
-
Because the value of the operand (a == b) is true, !(a == b) the result 0. Because !(a == b) its value is false.
C is the Bitwise Operator
Arithmetic operations such as addition, subtraction, multiplication and division are converted to bit-level during calculations, resulting in faster processing and saving power.
Bitwise operator is used to perform bit-level operations in C programming.
Suppose, in the table below x = 10 (0000 1010 in binary) and y = 4 (0000 0100 in binary)
To learn more about bitwise operators visit our C Biwixe Operators section.
Other Operators
C is the Comma Operator
The comma operator is used to declare related expressions simultaneously. For example:
int a, b = 10, c = 5, d;
C size of Operator
sizeof Operators are unary operators that return the size of data such constants, variables, arrays, structures, etc.
sizeof(variable)
Example: Size of Operator
#include
#include
void main()
{
int a;
float b;
double c;
char d;
printf("Size of Integer: %d bytes\n",sizeof(a));
printf("Size of float: %d bytes\n",sizeof(b));
printf("Size of double: %d bytes\n",sizeof(c));
printf("Size of character: %d byte\n",sizeof(d));
getch();
}
output
Size of Integer: 2 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of character: 1 byte
C ternary Operator
When an operator works with three operands, it is called a ternary operator. The ternary operator looks ?: like this. Ternary operators are also called conditional operators.
The working steps of the conditional operator are described below:
Boolean expression in above syntax is condition and true part and false part can be value or variable or statement or any mathematical expression. if the condition is True then the True part is executed otherwise the false part is executed.
Example: C ternary (conditional) Operator
#include
#include
void main()
{
int a, b, c, large;
clrscr();
printf("Enter any three number: ");
scanf("%d%d%d",&a,&b,&c);
large=a>b ? (a>c?a:c) : (b>c?b:c);
printf("Largest Number is: %d",large);
getch();
}
Enter any there numbers: 10 15 7
The largest number is 15
Other operators such as reference operator (&), dereference operator(*) and member selection opertor (->) are discussed in the C pointer chapter.
Pingback: C Programming Comments -
Pingback: C Program Add Two Number - Quick Learn
Pingback: C Program to perform all arithmetic operations - Quick Learn