*1.6K*

Table of Contents

In this article, you’ll learn about how to use C Programming Operators and Expressions.

An operator is a symbol that operates on a value or a variable which are used to perform logical and mathematical operations in a C program are called **C operators**.

- These
**C operators**join individual constants and variables to form expressions. - Operators, functions, constants and variables are combined together to form expressions.

Consider the expression

A + B * 5. where, +, * are operators, A, B are variables, 5 is constant and A + B * 5 is an expression.

**TYPES OF C OPERATORS**

**C language offers many types of operators. They are,**

- Arithmetic operators
- Assignment operators
- Relational operators
- Logical operators
- Bit wise operators
- Conditional operators (ternary operators)
- Increment/decrement operators
- Special operators

### Types of Operators and their Description

Types of Operators | Description |

Arithmetic_operators | These are used to perform mathematical calculations like addition, subtraction, multiplication, division and modulus |

Assignment_operators | These are used to assign the values for the variables in C programs. |

Relational operators | These operators are used to compare the value of two variables. |

Logical operators | These operators are used to perform logical operations on the given two variables. |

Bit wise operators | These operators are used to perform bit operations on given two variables. |

Conditional (ternary) operators | Conditional operators return one value if condition is true and returns another value is condition is false. |

Increment/decrement operators | These operators are used to either increase or decrease the value of the variable by one. |

Special operators | &, *, sizeof( ) and ternary operators. |

### ARITHMETIC OPERATORS IN C

C Arithmetic operators are used to perform mathematical calculations like addition, subtraction, multiplication, division and modulus in C programs

Arithmetic Operators/Operation | Example |

+ (Addition) | A+B |

– (Subtraction) | A-B |

* (multiplication) | A*B |

/ (Division) | A/B |

% (Modulus) | A%B |

#### EXAMPLE PROGRAM FOR C ARITHMETIC OPERATORS

In this example program, two values “40” and “20” are used to perform arithmetic operations such as addition, subtraction, multiplication, division, modulus and output is displayed for each operation.

```
#include <stdio.h>
int main()
{
int a=40,b=20, add,sub,mul,div,mod;
add = a+b;
sub = a-b;
mul = a*b;
div = a/b;
mod = a%b;
printf("Addition of a, b is : %d\n", add);
printf("Subtraction of a, b is : %d\n", sub);
printf("Multiplication of a, b is : %d\n", mul);
printf("Division of a, b is : %d\n", div);
printf("Modulus of a, b is : %d\n", mod);
}
```

Output

Addition of a, b is : 60 Subtraction of a, b is : 20 Multiplication of a, b is : 800 Division of a, b is : 2 Modulus of a, b is : 0

### ASSIGNMENT OPERATORS IN C:

In C programs, values for the variables are assigned using assignment operators.

- For example, if the value “10” is to be assigned for the variable “sum”, it can be assigned as “sum = 10;”
- There are 2 categories of assignment operators in C language. They are,

1. Simple assignment operator ( Example: = )

2. Compound assignment operators ( Example: +=, -=, *=, /=, %=, &=, ^= )

Operators | Example/Description |

= | sum = 10; 10 is assigned to variable sum |

+= | sum += 10;This is same as sum = sum + 10 |

-= | sum -= 10;This is same as sum = sum – 10 |

*= | sum *= 10;This is same as sum = sum * 10 |

/= | sum /= 10;This is same as sum = sum / 10 |

%= | sum %= 10;This is same as sum = sum % 10 |

&= | sum&=10;This is same as sum = sum & 10 |

^= | sum ^= 10;This is same as sum = sum ^ 10 |

**EXAMPLE PROGRAM FOR C ASSIGNMENT OPERATORS**

- In this program, values from 0 – 9 are summed up and total “45” is displayed as output.
- Assignment operators such as “=” and “+=” are used in this program to assign the values and to sum up the values.

```
# include <stdio.h>
int main()
{
int Total=0,i;
for(i=0;i<10;i++)
{
Total+=i; // This is same as Total = Toatal+i
}
printf("Total = %d", Total);
}
```

Output

Total = 45

**RELATIONAL OPERATORS IN C**

Relational operators are used to find the relation between two variables. i.e. to compare the values of two variables in a C program.

Operators | Example/Description |

> | x > y (x is greater than y) |

< | x < y (x is less than y) |

>= | x >= y (x is greater than or equal to y) |

<= | x <= y (x is less than or equal to y) |

== | x == y (x is equal to y) |

!= | x != y (x is not equal to y) |

#### EXAMPLE PROGRAM FOR RELATIONAL OPERATORS IN C

- In this program, relational operator (==) is used to compare 2 values whether they are equal are not.
- If both values are equal, output is displayed as ” values are equal”. Else, output is displayed as “values are not equal”.
- Note : double equal sign (==) should be used to compare 2 values. We should not single equal sign (=).

```
#include <stdio.h>
int main()
{
int m=40,n=20;
if (m == n)
{
printf("m and n are equal");
}
else
{
printf("m and n are not equal");
}
}
```

Output

m and n are not equal

### BIT WISE OPERATORS IN C:

These operators are used to perform bit operations. Decimal values are converted into binary values which are the sequence of bits and bit wise operators work on these bits.

- Bit wise operators in C language are & (bitwise AND), | (bitwise OR), ~ (bitwise NOT), ^ (XOR), << (left shift) and >> (right shift).

**TRUTH TABLE FOR BIT WISE OPERATION** & **BIT WISE OPERATORS**

### BELOW ARE THE BIT-WISE OPERATORS AND THEIR NAME IN C

- & – Bitwise AND
- | – Bitwise OR
- ~ – Bitwise NOT
- ^ – XOR
- << – Left Shift
- >> – Right Shift

Consider x=40 and y=80. Binary form of these values are given below.

x = 00101000

y= 01010000

All bit wise operations for x and y are given below.

- x&y = 00000000 (binary) = 0 (decimal)
- x|y = 01111000 (binary) = 120 (decimal)
- ~x = 11111111111111111111111111 11111111111111111111111111111111010111 = -41 (decimal)
- x^y = 01111000 (binary) = 120 (decimal)
- x << 1 = 01010000 (binary) = 80 (decimal)
- x >> 1 = 00010100 (binary) = 20 (decimal)

**NOTE:**

**Bit wise NOT :**Value of 40 in binary is 00000000000000000000000000000000 00000000000000000010100000000000. So, all 0’s are converted into 1’s in bit wise NOT operation.**Bit wise left shift and right shift :**In left shift operation “x << 1 “, 1 means that the bits will be left shifted by one place. If we use it as “x << 2 “, then, it means that the bits will be left shifted by 2 places.

#### EXAMPLE PROGRAM FOR BIT WISE OPERATORS IN C

In this example program, bit wise operations are performed as shown above and output is displayed in decimal format.

```
#include <stdio.h>
int main()
{
int m = 40,n = 80,AND_opr,OR_opr,XOR_opr,NOT_opr ;
AND_opr = (m&n);
OR_opr = (m|n);
NOT_opr = (~m);
XOR_opr = (m^n);
printf("AND_opr value = %d\n",AND_opr );
printf("OR_opr value = %d\n",OR_opr );
printf("NOT_opr value = %d\n",NOT_opr );
printf("XOR_opr value = %d\n",XOR_opr );
printf("left_shift value = %d\n", m << 1);
printf("right_shift value = %d\n", m >> 1);
}
```

Output

AND_opr value = 0 OR_opr value = 120 NOT_opr value = -41 XOR_opr value = 120 left_shift value = 80 right_shift value = 20

### CONDITIONAL OPERATORS IN C

- Conditional operators return one value if condition is true and returns another value is condition is false.
- This operator is also called as ternary operator.

Syntax : (Condition? true_value: false_value);Example : (A > 100 ? 0 : 1);

In above example, if A is greater than 100, 0 is returned else 1 is returned. This is equal to if else conditional statements.

**EXAMPLE PROGRAM FOR CONDITIONAL/TERNARY OPERATORS IN C**

```
#include <stdio.h>
int main()
{
int x=1, y ;
y = ( x ==1 ? 2 : 0 ) ;
printf("x value is %d\n", x);
printf("y value is %d", y);
}
```

Output

x value is 1 y value is 2

### Increment/decrement Operators in C

- Increment operators are used to increase the value of the variable by one and decrement operators are used to decrease the value of the variable by one in C programs.
- Syntax:

Increment operator: ++var_name; (or) var_name++;

Decrement operator: – -var_name; (or) var_name – -;

- Example:

Increment operator : ++ i ; i ++ ;

Decrement operator : – – i ; i – – ;

#### EXAMPLE PROGRAM FOR INCREMENT OPERATORS IN C

In this program, value of “i” is incremented one by one from 1 up to 9 using “i++” operator and output is displayed as “1 2 3 4 5 6 7 8 9”.

```
//Example for increment operators
#include <stdio.h>
int main()
{
int i=1;
while(i<10)
{
printf("%d ",i);
i++;
}
}
```

Output

1 2 3 4 5 6 7 8 9

#### EXAMPLE PROGRAM FOR DECREMENT OPERATORS IN C

In this program, value of “I” is decremented one by one from 20 up to 11 using “i–” operator and output is displayed as “20 19 18 17 16 15 14 13 12 11”.

```
//Example for decrement operators
#include <stdio.h>
int main()
{
int i=20;
while(i>10)
{
printf("%d ",i);
i--;
}
}
```

Output

20 19 18 17 16 15 14 13 12 11

## DIFFERENCE** BETWEEN PRE/POST INCREMENT & DECREMENT OPERATORS IN C**

Below table will explain the difference between pre/post increment and decrement operators in C programming language.

Operator | Operator/Description |

Pre increment operator (++i) | value of i is incremented before assigning it to the variable i |

Post increment operator (i++) | value of i is incremented after assigning it to the variable i |

Pre decrement operator (–i) | value of i is decremented before assigning it to the variable i |

Post decrement operator (i–) | value of i is decremented after assigning it to variable i |

#### SPECIAL OPERATORS IN C

Below are some of the special operators that the C programming language offers

Operators | Description |

& | This is used to get the address of the variable.Example : &a will give address of a. |

* | This is used as pointer to a variable.Example : * a where, * is pointer to the variable a. |

Sizeof () | This gives the size of the variable.Example : size of (char) will give us 1. |

**EXAMPLE PROGRAM FOR & AND * OPERATORS IN C**

In this program, “&” symbol is used to get the address of the variable and “*” symbol is used to get the value of the variable that the pointer is pointing to. Please refer **C – pointer** topic to know more about pointers.

```
#include <stdio.h>
int main()
{
int *ptr, q;
q = 50;
/* address of q is assigned to ptr */
ptr = &q;
/* display q's value using ptr variable */
printf("%d", *ptr);
return 0;
}
```

Output

50

#### EXAMPLE PROGRAM FOR SIZEOF() OPERATOR IN C

sizeof() operator is used to find the memory space allocated for each C data types.

```
#include <stdio.h>
#include <limits.h>
int main()
{
int a;
char b;
float c;
double d;
printf("Storage size for int data type:%d \n",sizeof(a));
printf("Storage size for char data type:%d \n",sizeof(b));
printf("Storage size for float data type:%d \n",sizeof(c));
printf("Storage size for double data type:%d\n",sizeof(d));
return 0;
}
```

Output

Storage size for int data type:4 Storage size for char data type:1 Storage size for float data type:4 Storage size for double data type:8

## Test Your Skill

- Which of the following is invalid in c?

```
1. int a; b=a;
2. int v=3^3;
3. char dt= '21 Dec 2020' ;
```

- What data type will 3.0/8 – 2 return?
- Write a program to check whether a number is divisible 97 or not.
- Explain step by step evaluation of 3*x/y-z +k and Where x=2, y=3, z=3 and k=1.
- 3.0+1 will be:
- Integer
- Floating number
- Character