In this tutorial, you will learn about C programming operators and how they are used in programs alongside their functionalities.
C programming operators are symbols that tell the compiler to perform certain mathematical or logical manipulation. In simple terms, we can say operators are used to manipulating data and variables.
For example: +
, -
are the operators used for mathematical calculation.
Operators in C programming |
||
1: Arithmetic operator | ||
2: Relational operator | ||
3: Logical operator | ||
4: Assignment operator | ||
5: Increment/Decrement operator | ||
6: Conditional operator | ||
7: Bitwise operator | ||
8: Special operator |
The data items in which any operation is carried out are called operands.
x + y
Here x
and y
are operands and +
is an operator and calculating sum is an operation.
Operators which require two operands are called binary operators and which takes single operand are called unary operators.
Operators used in the arithmetic operation like addition, subtraction, multiplication or division are called arithmetic operators.
Arithmetic operator |
Meaning |
+ | Addition or unary plus |
– | Subtraction or unary minus |
* | Multiplication |
/ | Division |
% | Modulo division |
Relational operators are used to compare two operators depending on their relation.
For example, for comparing the price of two things. The value of a relational operator is either 1
or 0
. If the specified relation is true then 1
else 0
.
a > b
Here, >
is a relational operator and it will return 1
if a
is greater than b
else it will return 0
.
Relational operator |
Meaning |
< | is less than |
<= | is less than or equal to |
> | is greater than |
>= | is greater than or equal to |
== | is equal to |
!= | is not equal to |
==
, >=
, <=
and !=
are separated by the space.Logical operators are used when more than one condition is tested. They are used to combine more than one relational expressions.
For example:
a > b && c == 1
Here &&
is relational operator used to combine two relational expressions a > b
and c == 1
.
Logical operator |
Meaning |
&& | logical AND |
|| | logical OR |
! | logical NOT |
If we need to make sure that both conditions are true before performing a certain task. In this case, logical AND &&
is used:
if ( gender == 1 && age <= 10 )
++childNumber;
In this case, if
condition is true only when both of the conditions gender == 1
and age <= 10
are true.
Finally, if the combined condition is true, then only childNumber
is increased by 1
.
If we wish to ensure that either or both of two conditions are true then we use logical OR (||)
operator.
if (mark < 40 || attendance < 15)
puts("Student is not qualified for exam");
In this case, if any of the conditions is true then if
statement is also true and the message Student is not qualified for exam
is printed.
[adsense1]
The assignment operator is used to assign a value or a result to a data item or a variable.
=
is the assignment operator.
For example:
a = 5
Here, =
is assignment operator which assigns value 5 to variable a
.
Assume:
int c = 2, d = 3, e = 4, f = 6, g = 8
Assignment Operator | Sample expression | Explanation | Assigns |
---|---|---|---|
+= | c += 2 | c = c + 2 | 4 to c |
-= | d -= 1 | d = d – 1 | 1 to d |
*= | e *= 2 | e = e * 2 | 8 to e |
/= | f /= 3 | f = f / 3 | 2 to f |
%= | g %= 4 | g = g % 4 | 2 to g |
Note: Don’t get confused between equality operator ==
and assignment operator =
.
Bitwise operators are used to manipulate data at a bit level. They are used for testing or shifting the bit.
Bitwise operator |
Meaning |
& | bitwise AND |
| | bitwise OR |
^ | bitwise exclusive OR |
<< | shift left |
>> | shift right |
Conditional operator is a ternary operator that takes three operands.
Syntax
x = exp1 ? a : b
Here, if exp1
is non zero i.e. TRUE
then the value of a
will be assigned to x
and if exp1
is zero i.e. FALSE
then the value of b
is assigned to x
.
learn about conditional operator (?:) in detail.
C provides an increment operator ++
and decrement operator --
. The functionality of ++
is to add 1 unit to the operand and --
is to subtract 1 from the operand.
For example
++ a;
-- b;
Here ++a
is equivalent to a = a + 1
and --b
is equivalent to b = b - 1
.
There are two kinds of increment and decrement operator i.e prefix and postfix.
If the operator is used before the variable i.e ++a
then it is called prefix increment operator.
If the operator is used after variable i.e a++
then it is called postfix increment operator.
In the prefix operator, first 1 is added and then the value is assigned to the variable.
In postfix operator, first the value is assigned then only 1 is added and the added value is assigned.
Operator | Sample expression | Explanation |
---|---|---|
++ | ++x | x is increased by 1, then use the value of x |
++ | x++ | Use the current value of x and then increment x by 1 |
– – | – -x | x is decreased by 1, then use the value of x |
– – | x- – | Use the current value of x and then decrement x by 1 |
#include <stdio.h>
int main()
{
int i;
i = 4;
printf( "%d\n", i );
printf( "%d\n", i++ ); //post increment
printf( "%d\n\n", i );
i = 4;
printf( "%d\n", i );
printf( "%d\n", ++i ); //preincrement
printf( "%d\n", i );
return 0;
}
Output
4 4 5 4 5 5
Besides these fundamental operators, C provides some other special operators like comma
operator and sizeof
operator
It is used to combine more than one expression. A comma linked list of expressions are evaluated left to right.
sizeof
operator is an operator which when used with operand returns the number of bytes occupied by the operand.
For example
x = sizeof( a );
Here, the size occupied by variable a will be assigned to x.