Operators in C Language
In this tutorial, we will be covering the Operators in C Language
C language supports a rich set of built-in operators. An operator is a symbol or a letter that is used to indicate a specific operation on the variables in a program. An Operator is mainly used to tell the compiler to perform a specific mathematical or logical manipulation. They are mainly used in the programs in order to manipulate data and variables.
There are various types of C operators which are as follows:

Arithmetic Operators
C supports all the basic arithmetic operators. Given below are the basic arithmetic operators available in C:
| Arithmetic Operator | Purpose |
| + | Addition |
| - | Subtraction or Unary Minus |
| * | Multiplication |
| / | Division |
| % | Remainder after Division |
The unary minus (-) operator multiplies its single operand by -1. Hence, a number preceded by a minus sign changes its sign. When both the operands are integers (or real), the result produces an integer value (or real value). If one operand is of the real type, and another is an integer type, then the result is always a real number. It is important to note that the division operator % cannot be used on floating-point data.
Example
#include<stdio.h>
int main()
{
int f;
float c;
printf("\nEnter the temperature in Fahrenheit:");
scanf("%d",&f);
c=5.0/9.0*(f-32.0);
printf("Equivalent celsius temperature is:%f",c);
}
Enter the temperature in Fahrenheit:35
Equivalent celsius temperature is:1.666667
The operator % operates as follows:
| Operand1 | Operator | Operand2 | Result |
|---|---|---|---|
|
-9 |
% |
2 |
1 |
|
-9 |
% |
2 |
-1 |
|
-9 |
% |
(-2) |
1 |
|
-9 |
% |
(-2) |
-1 |
Hence the sign of the result is always the sign of the first operand.
Relational Operators
The relational operator is used to contrast two operands to see whether they are similar to each other, unequal, or one is greater or lesser than the other.
While the operand can be variables, constants, or expressions, the result is always a numeric value equivalent to true or false.
C language provides six relational operators.
|
Operator |
Description |
|
== |
Check if two operands are equal |
|
!= |
Check if two operands are not equal. |
|
> |
Check if the operand on the left is greater than the operand on the right |
|
< |
Check operand on the left is smaller than the right operand |
|
>= |
Check left operand is greater than or equal to the right operand |
|
<= |
Check if the operand on the left is smaller than or equal to the right operand |
Example
#include <stdio.h>
int main()
{
int a = 10, b = 10, c = 20;
printf("%d == %d is %d \n", a, b, a == b);
printf("%d == %d is %d \n", a, c, a == c);
printf("%d > %d is %d \n", a, b, a > b);
printf("%d > %d is %d \n", a, c, a > c);
printf("%d < %d is %d \n", a, b, a < b);
printf("%d < %d is %d \n", a, c, a < c);
printf("%d != %d is %d \n", a, b, a != b);
printf("%d != %d is %d \n", a, c, a != c);
printf("%d >= %d is %d \n", a, b, a >= b);
printf("%d >= %d is %d \n", a, c, a >= c);
printf("%d <= %d is %d \n", a, b, a <= b);
printf("%d <= %d is %d \n", a, c, a <= c);
return 0;
}
10 == 10 is 1
10 == 20 is 0
10 > 10 is 0
10 > 20 is 0
10 < 10 is 0
10 < 20 is 1
10 != 10 is 0
10 != 20 is 1
10 >= 10 is 1
10 >= 20 is 0
10 <= 10 is 1
10 <= 20 is 1
A simple relation consists of only one relational expression and takes the following form:
<ae-1><relational operator><ae-2>
<ae-1> and <ae-2> are arithmetic expression, which can be constants, variables, or mixtures of these. The value of the relational operator is 1 or 0. If the relation is true, the result is 1; otherwise, it is 0.
Example
|
Expressions |
Result |
|
6.3<=15 |
True |
|
2.5<-2 |
False |
|
-10>=0 |
False |
|
10<8+3 |
True |
Logical Operators
Logical operators are used to merging two or more relational expressions. C supports three logical operators. A compound relation acts identically producing either true or false value.
|
Operators |
Description |
Result |
|
&& |
Logical AND |
True if both the operands are true |
|
|| |
Logical or |
True if both the operands are true |
|
! |
Logical not |
True if the operand is false and vice versa. |
Example
-
(age>50 && weight <80) will evaluate to true if age is more than 50 and also weight is less than 80. Otherwise, it will evaluate as false.
-
(a < 0 ¦ ch = = ‘a’) will evaluate to true if a is less than 0 while ch is equal to ‘a’, false otherwise.
-
(!(a < 0)) will evaluate to true if a is greater than or equal to 0, false otherwise.
Example
#include <stdio.h>
int main()
{
int a = 10, b = 10, c = 20, result;
result = (a == b) && (c > b);
printf("(a == b) && (c > b) is %d \n", result);
result = (a == b) && (c < b);
printf("(a == b) && (c < b) is %d \n", result);
result = (a == b) || (c < b);
printf("(a == b) || (c < b) is %d \n", result);
result = (a != b) || (c < b);
printf("(a != b) || (c < b) is %d \n", result);
result = !(a != b);
printf("!(a != b) is %d \n", result);
result = !(a == b);
printf("!(a == b) is %d \n", result);
return 0;
}
(a == b) && (c > b) is 1
(a == b) && (c < b) is 0
(a == b) || (c < b) is 1
(a != b) || (c < b) is 0
!(a != b) is 1
!(a == b) is 0
Bitwise Operators
Bitwise operators are used for manipulating the data at the bit level. These operators can be testing the bits or shifting them right or left. Bitwise operators apply to integer data types only.
A list of different bitwise operators available in C language and their corresponding meaning is presented below:
|
Operator |
Description |
|
& |
Bitwise AND |
|
| |
Bitwise OR |
|
^ |
Bitwise XOR |
|
<< |
Left Shift |
|
>> |
Right Shift |
| (Bit-wise OR): Binary operator takes two operands of int type and performs bit-wise OR operation. With assumption that int size is 8-bits:
int a = 5; [binary : 0000 0101]
int b = 9; [binary : 0000 1001]
a | b yields [binary : 0000 1101]
& (Bit-wise AND): Binary operator takes two operands of int type and performs bit-wise AND operation. With same assumption on int size as above:
int a = 5; [binary : 0000 0101]
int b = 9; [binary : 0000 1001]
a & b yields [binary : 0000 0001]
^ (Bit-wise Logical XOR): XOR gives 1 if only one of the operand is 1 else 0. With same assumption on int size as above:
int a = 5; [binary : 0000 0101]
int b = 9; [binary : 0000 1001]
a ^ b yields [binary : 0000 1100]
<< (Shift left): This operator shifts the bits towards left padding the space with 0 by given integer times.
int a = 5; [binary : 0000 0101]
a << 3 yields [binary : 0010 1000]
>> (Shift right): This operator shifts the bits towards right padding the space with 0.
int a = 5; [binary : 0000 0101]
a >> 3 yields [binary : 0000 0000]
Example
#include<stdio.h>
void main()
{
int a=10,b=20;
printf("\n");
printf("a^b is %d\n",a^b);
printf("a&b is %d\n",a&b);
printf("a|b is %d\n",a|b);
printf("a<<3 is %d\n",a<<3);
printf("a>>3 is %d\n",a>>3);
printf("~a is %d\n",~a);
}
a^b is 30
a&b is 0
a|b is 30
a<<3 is 80
a>>3 is 1
~a is -11
Assignment Operators
Assignment operators can save the result of an expression to a variable. The most commonly used assignment operator is (=). Be careful not to mistake the assignment operator (=) for the mathematical equality operator which is indicated by the same symbol.
An expression with assignment operator is of the following form:
<identifier> = <expression>;
When this statement is executed, <expression> is evaluated and the value is stored in the
<identifier>.
There is various assignment operator supported by C language, which is as follows:
|
Operators |
Description |
Example |
|
= |
assigns values from right side operands to left side operand |
a=b |
|
+= |
adds right operand to the left operand and assign the result to left |
a+=b is same as a=a+b |
|
-= |
subtracts right operand from the left operand and assign the result to the left operand |
a-=b is same as a=a-b |
|
*= |
multiply left operand with the right operand and assign the result to the left operand |
a*=b is same as a=a*b |
|
/= |
divides left operand with the right operand and assign the result to the left operand |
a/=b is same as a=a/b |
|
%= |
calculate modulus using two operands and assign the result to the left operand |
a%=b is the same as a=a%b |
Example
#include <stdio.h>
int main()
{
int a = 10, c;
c = a;
printf("c = %d\n", c);
c += a;
printf("c = %d\n", c);
c -= a;
printf("c = %d\n", c);
c *= a;
printf("c = %d\n", c);
c /= a;
printf("c = %d\n", c);
c %= a;
printf("c = %d\n", c);
return 0;
}
c = 10
c = 20
c = 10
c = 100
c = 10
c = 0
Conditional operator
The conditional operators in the C language are known by two more names
1. Ternary Operator
2. ? : Operator
It is the if condition that we use in C language decision making, but using conditional operator, we turn the if condition statement into a short and simple operator.
The syntax of a conditional operator is:
expression 1 ? expression 2: expression 3
Explanation:
-
The question mark "?" in the syntax represents the if part.
-
The first expression (expression 1) generally returns either true or false, based on which it is decided whether (expression 2) will be executed or (expression 3)
-
If (expression 1) returns true, then the expression on the left side of ": " i.e., (expression 2) is executed.
-
If (expression 1) returns false, then the expression on the right side of ": " i.e., (expression 3) is executed.
Example
#include<stdio.h>
void main()
{
float x, y, big;
printf("Enter the two numbers\n");
scanf("%f%f",&x,&y);
big=(x>y?x:y);
printf("Larger number is %8.2f\n",big);
}
Enter the two numbers
10
2
Larger number is 10.00
Special Operators
C language also provides several special operators who have no counterparts in other languages. These operators consist of the comma operator, sizeof operator, pointer Operators (& and *), and member selection operators (. and ->). Pointer operators will be addressed while introducing pointers and member selection operators with structures and union.
We will consider the comma operator and sizeof operator in this section.
Comma Operator
This operator can link the related expressions together.
Example: int x, y, z;
z = (x = 10, y=20, x+y);
Hence, the first statement will generate three integer-type variables – x, y, and z. In the second statement, the right-hand side will be evaluated first. Consequently, 10 will be stored in variable x, then 20 will be stored in variable y, values in x and y will be multiplied, resulting in variable z. Thus, the value stored in the variable z will be 200 at the end of execution.
Sizeof Operator
The sizeof operator works on variables, constants, and even on data types. It restores the number of bytes the operand involves in the memory.
Consider the following C code for illustration.
sizeof(int); //Gives the number of bytes occupied by an
//integer type variable
sizeof(float); //Gives the number of bytes occupied by a
//float type variable
Conclusion
This tutorial was all about Operators in C Programming Language. In C there are Relational Operators, Bitwise Operators, Arithmetic Operators, Logical Operators, Conditional Operators and some Special operators.
We have covered them along with their examples.
Frequently Asked Questions
Ques 1: What is the usage of sizeof operator in C?
The sizeof operator is mainly used to obtain the size of any variable. It can be applied to any data type like float, integer, char, double.
Ques 2: What do you understand by Operator in C?
An operator is a symbol or a letter that is used to indicate a specific operation on the variables in a program
Ques 3: What are unary operators in C?
Mainly unary operators are those operators that take only one value in order to perform their operation.
Ques 4: What is the usage of == operator in C?
In C, == operator is mainly used to compare the values if the value on both sides of this operator matches then it will return true in that case.