Course Content
Basic of C programming
0/14
Functions, Array and Structures
0/11
C Programming Pointers
0/1
C Pointers & Arrays
0/1
C Pointers & Functions
0/1
C Memory Allocation
0/1
C Programming String
0/1
C String Functions
0/1
C Files Input/Output
0/1
C Enumeration
0/1
C Preprocessors
0/1
C Standard Library
0/1
C Programming Tutorial

## C Operators

In this tutorial, you will learn about different type of operators in C Language with the help of examples.

## Operators

C offers us a wide variety of operators that we can use to operate on data.

In C language, operators is a symbol which is used to perform a specific mathematical or logical functions.
In particular, we can identify various groups of operators:

Types of Operators in C language

1. Arithmetic Operators
2. Increment and Decrement Operators
3. Assignment Operators
4. Relational Operators
5. Logical Operators
6. Bitwise Operators
7. Other Operators

## Arithmetic Operators

An arithmetic operator performs basic mathematical operations such as addition, subtraction, multiplication, division etc on numerical values (constants and variables).

Assume variableÂ A = 10Â and variableÂ B = 5Â in the examples of table given below :

Operator Meaning of Operator Example
+ Add two operands A + B = 15
Subtract second operand from the first operand. A – B = 5
* Multiplies both operands A * B = 50
/ Divides numerator by denumerator A / B = 2
% Give remainder after division A % B = 0

### Example : Arithmetic Operators

#include <stdio.h>
int main(){
Â Â int a = 10, b = 6, c;
Â Â c = a + b;
Â Â printf("a + b = %d \n", c); Â // Addition Operator
Â Â c = a - b;
Â Â printf("a - b = %d \n", c);Â // Subtraction Operator
Â Â c = a * b;
Â Â printf("a * b = %d \n", c);Â // Multiplication Operator
Â Â c = a / b;
Â Â printf("a / b = %d \n", c);Â // Division Operator
Â Â c = a % b;
Â Â printf("Remainder when a divided by b = %d \n", c);Â // Modulus
Operator

Â Â return 0;
}

Output

a + b = 16
a - b = 4
a * b = 60
a / b = 1
a % b = 4

In the above example,
we observed that 10 /6 = 1 but in normal calculation 10 / 6 = 1.67 while the output in the program is 1. It is because both the variablesÂ aÂ andÂ bÂ are integers . Hence, the output is also an integer. The compiler neglects the term after the decimal point and shows answer 1 instead of 1.67.

Note :Â The Modulus operatorÂ %Â can only be used with integers.

## Increment and Decrement Operators

In C programming, Increment and decrement operators are used to change the value of operand by 1 .

Increment operatorÂ ++Â increase the value of operand by 1 .
Decrement operatorÂ Â decrease the value of operand by 1 .

Note :Â Increment and Decrement operator are unary operator i.e. they can only be used/operate in a single operand.
These operators can be used as suffix and prefix both.

### Example : Increment and Decrement Operators

#include <stdio.h>
int main(){
Â Â int a = 5, b = 100;
Â Â float c = 5.3, d = 100.8;
Â Â printf("++a = %d \n", ++a);
Â Â printf("b++ = %d \n", b++);
Â Â printf("--c = %f \n", --c);
Â Â printf("d-- = %f \n", d--);
Â Â return 0;
}

Output

++a is 6
b++ is 100
--c is 4.300000
d-- is 100.800000

++ and — operator as prefix and postfix

In the above example,
++Â operator is used as prefix with variable a i.e. the value of variable a is incremented by 1 then, it returns the value.
++Â operator is used as postfix with variable b i.e. the original value of variable a is returned first then, the value of b is incremented by 1.
Â operator is used as prefix with variable c i.e. the value of variable c is decremented by 1 then, it returns the value.
Â operator is used as postfix with variable d i.e. the original value of variable d is returned first then, the value of d is incremented by 1.

## Assignment Operators

The assignment operators are used for assigning a value to a variable.

Operator Example Same as
= a += b a = a + b
-= a -= b a = a – b
*= a *= b a = a * b
/= a /= b a = a / b
%= a %= b a = a % b

### Example : Assignment Operators

#include <stdio.h>
int main(){
Â Â int a = 10, b;
Â Â b = a; // Here, b is 10
Â Â printf("b = %d\n", b);
Â Â b += a; // Here, b is 20
Â Â printf("b = %d\n", b);
Â Â b -= a; // Here, b is 10
Â Â printf("b = %d\n", b);
Â Â b *= a; // Here, b is 100
Â Â printf("b = %d\n", b);
Â Â b /= a; // Here, b is 10
Â Â printf("b = %d\n", b);
Â Â b %= a; // Here, b = 0
Â Â printf("b = %d\n", b);
Â Â return 0;
}

Output

b = 10
b = 20
b = 10
b = 100
b = 10
b = 0

### Relational Operators

Relational operators are used to check/define the relationship between two entities. If the relation is true, it return 1; if the relation is false, it return 0.

Note :Â Relational operators are generally used inÂ loopsÂ andÂ decision making.

Now, AssumeÂ a = 5Â for examples in the table given below :

Operator Meaning of Operator Example
Operator Meaning of operator Example
== Equal to a == 5
> Greater than a > 2
< Less than a < 8
!= Not equal to a != 4
>= Greater than or equal to a >= 1
<= Less than or equal to a <= 6

### Example : Relational Operators

#include <stdio.h>
int main(){
Â Â int a = 10, b = 5, c = 10;
Â Â 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;
}

Output

10 == 5 is 0
10 == 10 is 1
10 > 5 is 1
10 > 10 is 0
10 < 5 is 0
10 < 10 is 0
10 != 5 is 1
10 != 10 is 0
10 >= 5 is 1
10 >= 10 is 1
10 <= 5 is 0
10 <= 10 is 1

### Logical Operators

Logical operators are used to make decision (True or False) depending on the given expression.
These operators are commonly used in decision making in C programming.

Operator Meaning Example
Operator Name Meaning
&& Logical AND True only if all the operands are True
|| Logical OR True only of either one operand is true
! Logical NOT True only if the operand is 0

### Example : Logical Operators

#include <stdio.h>
int main()
{
Â Â int a = 5, b = 5, c = 10, 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;
}

Output

(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 special operator set provided in C programming. They are used to convert operands to bit level for basic computation which makes processing faster.

Note :Bitwise Operators are very helpful in programming where we have limited amount of memory and want to run program faster.

Operators Meaning of operators Working
& Bitwise AND Give result 1 (true) if both bits are 1 (true)
| Bitwise OR Give result 1 (true) if any of the two bits is 1 (true)
^ Bitwise XOR Give result 1 (true) if the two bits are different
<< Left Shift Left shifts the bits of the first operand,
the second operand decides the number of places to shift
>> Right Shift Right shifts the bit of the first operand,
the second operand decides the number of places to shift
~ Bitwise NOT Invert all bits of operand

### Example : Bitwise Operator

#include <stdio.h>
int main()
{
Â Â unsigned char a = 5, b = 9;
Â Â 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 = ~a);
Â Â printf("b << 1 = %d \n", b << 1);
Â Â printf("b >> 1 = %d \n", b >> 1);
Â Â return 0;
}

Output

a & b = 1
a | b = 13
a ^ b = 12
~a = 250
b << 1 = 18
b >> 1 = 4

## Other Operators

### The ternary operator

The ternary operator is the only operator in C that works with 3 operands, and itâ€™s a short way to express conditionals.

This is how it looks:

condition ? expression : expression

Example:

a ? b : c

IfÂ aÂ is evaluated toÂ true, then theÂ bÂ statement is executed, otherwiseÂ cÂ is.

The ternary operator is functionality-wise same as an if/else conditional, except it is shorter to express and it can be inlined into an expression.

### Comma Operator

Comma operator is used to link related expressions together or seperate two operands. For example:

int a = 2, c = 3, d = 4;

### The sizeof operator

TheÂ sizeofÂ operator is the unary operator in C used to compute the size of its operand i.e. it return the size of a variable.

In simple words, theÂ sizeofÂ operator returns the size of the operand you pass. You can pass a variable, or even a type.

Example : sizeof Operator

#include <stdio.h>
int main(){
Â Â int a;
Â Â float b;
Â Â double c;
Â Â char d;
Â Â printf("Size of int=%lu bytes\n", sizeof(a));
Â Â printf("Size of float=%lu bytes\n", sizeof(b));
Â Â printf("Size of double=%lu bytes\n", sizeof(c));
Â Â printf("Size of char=%lu byte\n",sizeof(d));
Â Â return 0;
}

Output

Size of int = 4 bytes
Size of float = 4 bytes
Size of double = 8 bytes
Size of char = 1 byte

Note :Â The size of data types are different in different systems.

error: Content is protected !!