# C Programming Operators and Expressions

393 views

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,

1. Arithmetic operators
2. Assignment operators
3. Relational operators
4. Logical operators
5. Bit wise operators
6. Conditional operators (ternary operators)
7. Increment/decrement operators
8. Special operators

### ARITHMETIC OPERATORS IN C

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

#### 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.

```.wp-block-code {
border: 0;
}

.wp-block-code > div {
overflow: auto;
}

.shcb-language {
border: 0;
clip: rect(1px, 1px, 1px, 1px);
-webkit-clip-path: inset(50%);
clip-path: inset(50%);
height: 1px;
margin: -1px;
overflow: hidden;
position: absolute;
width: 1px;
word-wrap: normal;
word-break: normal;
}

.hljs {
box-sizing: border-box;
}

.hljs.shcb-code-table {
display: table;
width: 100%;
}

.hljs.shcb-code-table > .shcb-loc {
color: inherit;
display: table-row;
width: 100%;
}

.hljs.shcb-code-table .shcb-loc > span {
display: table-cell;
}

.wp-block-code code.hljs:not(.shcb-wrap-lines) {
white-space: pre;
}

.wp-block-code code.hljs.shcb-wrap-lines {
white-space: pre-wrap;
}

.hljs.shcb-line-numbers {
border-spacing: 0;
counter-reset: line;
}

.hljs.shcb-line-numbers > .shcb-loc {
counter-increment: line;
}

.hljs.shcb-line-numbers .shcb-loc > span {
}

.hljs.shcb-line-numbers .shcb-loc::before {
border-right: 1px solid #ddd;
content: counter(line);
display: table-cell;
text-align: right;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
white-space: nowrap;
width: 1%;
}
```#include <stdio.h>

int main()
{
sub = a-b;
mul = a*b;
div = a/b;
mod = a%b;
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);
}```Code language: C/AL (cal)```

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: +=, -=, *=, /=, %=, &=, ^= )

#### 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);
}```Code language: C/AL (cal)```

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.

#### 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");
}
}```Code language: PHP (php)```

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).

### 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);
}```Code language: PHP (php)```

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);
}```Code language: PHP (php)```

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++;
}
}```Code language: PHP (php)```

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--;
}
}```Code language: PHP (php)```

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.

#### SPECIAL OPERATORS IN C

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

#### 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;
}```Code language: PHP (php)```

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;
}```Code language: PHP (php)```

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```

0 comment