// C program to illustrate the arithmatic operators
#include <stdio.h>
int main()
{
int a = 25, b = 5;
// using operators and printing results
printf("a + b = %d\n", a + b);
printf("a - b = %d\n", a - b);
printf("a * b = %d\n", a * b);
printf("a / b = %d\n", a / b);
printf("a % b = %d\n", a % b);
printf("+a = %d\n", +a);
printf("-a = %d\n", -a);
printf("a++ = %d\n", a++);
printf("a-- = %d\n", a--);
return 0;
}
Output
a + b = 30
a - b = 20
a * b = 125
a / b = 5
a % b = 0
+a = 25
-a = -25
a++ = 25
a-- = 26
2. Relational Operators in C
S. No. Symbol Operator Description Syntax
Returns true if
the left
operand is less
< Less than a<b
than the right
operand. Else
1 false
Returns true if
the left
operand is
> Greater than greater than a>b
the right
operand. Else
2 false
Returns true if
the left
operand is less
Less than or
<= than or equal a <= b
equal to
to the right
operand. Else
3 false
Returns true if
the left
operand is
Greater than
>= greater than or a >= b
or equal to
equal to right
operand. Else
4 false
Returns true if
both the
== Equal to a == b
operands are
5 equal.
S. No. Symbol Operator Description Syntax
Returns true if
both the
!= Not equal to a != b
operands are
6 NOT equal.
// C program to illustrate the relational operators
#include <stdio.h>
int main()
{
int a = 25, b = 5;
// using operators and printing results
printf("a < b : %d\n", a < b);
printf("a > b : %d\n", a > b);
printf("a <= b: %d\n", a <= b);
printf("a >= b: %d\n", a >= b);
printf("a == b: %d\n", a == b);
printf("a != b : %d\n", a != b);
return 0;
}
Output
a < b : 0
a > b : 1
a <= b: 0
a >= b: 1
a == b: 0
a != b : 1
3. Logical Operator in C
Logical Operators are used to combine two or more conditions/constraints
or to complement the evaluation of the original condition in consideration.
The result of the operation of a logical operator is a Boolean value
either true or false.
S. No. Symbol Operator Description Syntax
Returns true if
both the
&& Logical AND a && b
operands are
1 true.
Returns true if
both or any of
|| Logical OR a || b
the operand is
2 true.
Returns true if
! Logical NOT the operand is !a
3 false.
// C program to illustrate the logical operators
#include <stdio.h>
int main()
{
int a = 25, b = 5;
// using operators and printing results
printf("a && b : %d\n", a && b);
printf("a || b : %d\n", a || b);
printf("!a: %d\n", !a);
return 0;
}
Output
a && b : 1
a || b : 1
!a: 0
4. Bitwise Operators in C
he Bitwise operators are used to perform bit-level operations on the
operands. The operators are first converted to bit-level and then the
calculation is performed on the operands. Mathematical operations such as
addition, subtraction, multiplication, etc. can be performed at the bit level
for faster processing.
S. No. Symbol Operator Description Syntax
Performs
bit-by-bit AND
& Bitwise AND operation and a&b
returns the
1 result.
Performs
bit-by-bit OR
| Bitwise OR operation and a|b
returns the
2 result.
Performs
bit-by-bit XOR
^ Bitwise XOR operation and a^b
returns the
3 result.
Flips all the set
Bitwise First
~ and unset bits ~a
Complement
4 on the number.
Shifts the
number in
binary form by
Bitwise
<< one place in a << b
Leftshift
the operation
and returns the
5 result.
Bitwise Shifts the
>> a >> b
6 Rightshilft number in
S. No. Symbol Operator Description Syntax
binary form by
one place in
the operation
and returns the
result.
// C program to illustrate the bitwise operators
#include <stdio.h>
int main()
{
int a = 25, b = 5;
// using operators and printing results
printf("a & b: %d\n", a & b);
printf("a | b: %d\n", a | b);
printf("a ^ b: %d\n", a ^ b);
printf("~a: %d\n", ~a);
printf("a >> b: %d\n", a >> b);
printf("a << b: %d\n", a << b);
return 0;
}
Output
a & b: 1
a | b: 29
a ^ b: 28
~a: -26
a >> b: 0
a << b: 800
5. Assignment Operators in C
Assignment operators are used to assign value to a variable. The left side
operand of the assignment operator is a variable and the right side operand
of the assignment operator is a value. The value on the right side must be
of the same data type as the variable on the left side otherwise the
compiler will raise an error.
The assignment operators can be combined with some other operators in C
to provide multiple operations using single operator. These operators are
called compound
S. No. Symbol Operator Description Syntax
Assign the
value of the
Simple
= right operand a=b
Assignment
to the left
1 operand.
Add the right
operand and
Plus and left operand
+= a += b
assign and assign this
value to the left
2 operand.
Subtract the
right operand
and left
Minus and
-= operand and a -= b
assign
assign this
value to the left
3 operand.
Multiply the
right operand
and left
Multiply and
*= operand and a *= b
assign
assign this
value to the left
4 operand.
Divide the left
operand with
Divide and
/= the right a /= b
assign
operand and
5 assign this
S. No. Symbol Operator Description Syntax
value to the left
operand.
Assign the
remainder in
the division of
Modulus and
%= left operand a %= b
assign
with the right
operand to the
6 left operand.
Performs
bitwise AND
AND and and assigns
&= a &= b
assign this value to
the left
7 operand.
Performs
bitwise OR and
OR and
|= assigns this a |= b
assign
value to the left
8 operand.
Performs
bitwise XOR
XOR and and assigns
^= a ^= b
assign this value to
the left
9 operand.
Performs
bitwise
Rightshift Rightshift and
>>= a >>= b
and assign assign this
value to the left
10 operand.
Performs
Leftshift and
<<= bitwise Leftshift a <<= b
assign
11 and assign this
S. No. Symbol Operator Description Syntax
value to the left
operand.
Example of C Assignment Operators
C
// C program to illustrate the assignment operators
#include <stdio.h>
int main()
{
int a = 25, b = 5;
// using operators and printing results
printf("a = b: %d\n", a = b);
printf("a += b: %d\n", a += b);
printf("a -= b: %d\n", a -= b);
printf("a *= b: %d\n", a *= b);
printf("a /= b: %d\n", a /= b);
printf("a %%= b: %d\n", a %= b);
printf("a &= b: %d\n", a &= b);
printf("a |= b: %d\n", a |= b);
printf("a >>= b: %d\n", a >>= b);
printf("a <<= b: %d\n", a <<= b);
return 0;
}
Output
a = b: 5
a += b: 10
a -= b: 5
a *= b: 25
a /= b: 5
a %= b: 0
a &= b: 0
a |= b: 5
a >>= b: 0
a <<= b: 0
sizeof Operator
● sizeof is much used in the C programming language.
● It is a compile-time unary operator which can be used to compute the
size of its operand.
● The result of sizeof is of the unsigned integral type which is usually
denoted by size_t.
● Basically, the sizeof the operator is used to compute the size of the
variable or datatype.
Comma Operator ( , )
● The comma operator (represented by the token) is a binary operator that
evaluates its first operand and discards the result, it then evaluates the
second operand and returns this value (and type).
● The comma operator has the lowest precedence of any C operator.
● Comma acts as both operator and separator.
Conditional Operator ( ? : )
● The conditional operator is the only ternary operator in C++.
● Here, Expression1 is the condition to be evaluated. If the
condition(Expression1) is True then we will execute and return the result
of Expression2 otherwise if the condition(Expression1) is false then we
will execute and return the result of Expression3.
● We may replace the use of if..else statements with conditional
operators.
// C Program to demonstrate the use of Misc operators
#include <stdio.h>
int main()
{
// integer variable
int num = 10;
int* add_of_num = #
printf("sizeof(num) = %d bytes\n", sizeof(num));
printf("&num = %p\n", &num);
printf("*add_of_num = %d\n", *add_of_num);
printf("(10 < 5) ? 10 : 20 = %d\n", (10 < 5) ? 10 : 20);
printf("(float)num = %f\n", (float)num);
return 0;
}
Output
sizeof(num) = 4 bytes
&num = 0x7ffe2b7bdf8c
*add_of_num = 10
(10 < 5) ? 10 : 20 = 20
(float)num = 10.000000
Unary, Binary and Ternary Operators in C
Operators can also be classified into three types on the basis of the
number of operands they work on:
1. Unary Operators: Operators that work on single operand.
2. Binary Operators: Operators that work on two operands.
3. Ternary Operators: Operators that work on three operands.