# Operators and Expression in C

An operator is a symbol that specifies an operation to be performed that gives value. The variables, constants can be joined by various operators to form an expression that could be any, mathematical or logical. An operand is a data item on which an operator acts. Some operators require two operands, while others act upon only one operand. C includes a large number of operators that fall under several different categories, which are as-

- Arithmetic Operators
- Assignment Operators.
- Increment and Decrement Operators
- Relational Operators.
- Logical Operators
- Conditional Operator
- Comma Operator
- Sizeof Operator
- Bitwise Operators
- Other Operators

## Arithmetic operators

Arithmetic operators are used for numeric calculations. They are of two types –

**Unary arithmetic operators****Binary arithmetic operators**

### Unary Arithmetic Operators

Unary operators require only one operand. For example-

+x -y

Here ‘-‘ changes the sign of the operand y

### Binary Arithmetic Operators

Binary operators require two operands. There are five binary arithmetic operators. Operator Purpose + addition – subtraction * multiplication / division % gives the remainder in integer division-

**Operator Purpose****+** addition**– ** subtraction*** ** multiplication**/** division**%** gives the remainder in integer division

% (modulus operator) cannot be applied with floating-point operands. There is no exponent operator in C. However, there is a library function pow( ) to carry out exponentiation operation. **Note that unary plus and unary minus operators are different from the addition and subtraction operators.**

### Integer Arithmetic

When both operands are integers then the arithmetic operation with these operands is called integer arithmetic and the resulting value is always an integer. Let us take two variables **a** and **b**. The value’ of **a = 17** and **b = 4,** the results of the following operations are

**Expression ** **Result****a+b ** – 21**a-b** – 13**a*b** – 68**a/b** – 4 (decimal part truncates)**a%b** – 1 (Remainder after integer division)

After division operation, the decimal part will be truncated and the result is only integer part of quotient. After modulus operation, the result will be the remainder part of the integer division. The second operand must be nonzero for division and modulus operations.

```
/*Program to understand the integer arithmetic operation*/
#include<stdio.h>
main()
{
int a=17, b=4;
printf("Sum = %d \ n",a+b);
ptintf("Difference = %d\n",a-b);
printf("Product = %d\n",a*b);
printf("Quotient = %d\n",a/b);
printf("Remainder = %d\n",a%b);
}
Output:
Sum = 21
Difference = 13
Product = 68
Quotient = 4
Remainder =1
```

### Floating-Point Arithmetic

When both operands are of float type then the arithmetic operation with these operands is called floating point arithmetic: Let us take two variables a and b. The value of a=12.4 and b=3.1 the result of the following operations is as-

** ****Expression Result****a+b** 15.5**a-b ** 9.3**a*b** 38.44**a Ib ** 4.0

The modulus operator % cannot be used with floating-point numbers.

```
/*Program to understand the floating-point arithmetic operation */
#include<stdio.h>
main ()
}
float a=12.4, b=3.8;
printf("Sum=%. 2f \n", a+b);
printf("Difference = %.2f\n", a-b);
printf("Product = %. 2f\n", a *b);
printf("a/b=%.2f\n",a/b);
Output
Sum=16.20
Difference=8.60
Product=47.12
a/b=3.26
```

## Assignment Operators

A value can be stored in a variable with the use of the assignment operator. This assignment operator **“= “**, is used in assignment expressions and assignment statements.

The operand on the left-hand side should be a variable, while the operand on the right-hand side can any variable, constant, or expression. The value of the right-hand operand is assigned to the left-hand operand. Here are some examples of assignment expressions-

```
x = 8 /* 8 is assigned to x*/
y = 5 /* 5 is assigned to y*/
s = x+y-2 /* Value of expression x+y-2 is assigned to s*/
y = x /* Value of x is assigned to y*/
x = y /* Value of y is assigned to x*/
```

### Increment and Decrement Operators

C has two useful operators increment **(++)** and decrement **(- -)**. These are unary operators because they operate on a single operand. The increment operator **(++)** increments the value of the variable by **1** and decrement operator **(- -)** decrements the value of the variable by **1**.**++x** is equivalent to **x= +1**** –x ** is equivalent to ** x= -1**

These operators should be used only with variables; they can’t be used with constants or expressions.

For example, the expressions **++5 or ++(x+y+z)** are invalid.

### Prefix Increment / Decrement

Here first the value of the variable’ is incremented/decremented then the new value is used in the operation

Let us take a variable **x** whose value is **3**.

The statement **y =++x;** means first increment the value of **x** by **1**, then assign the value of **x** to** y** This single statement is -equivalent to these two statements-

**x=x+1;****y=x;**

Hence now value of **x** is **4** and value of y is **4**.

The statement y **= – -x;** means first decrement the value of **x** by **1** then assign the value of **x** to **y**

This statement is equivalent to these two’ statements.**x=x-1;****y=x;**

```
/*Program to understand Prefix increment/decrement Operator in C*/
#include<stdio.h>
int main(void)
{
int x=8;
printf(“x=%dt“,x);
printf(“x=%dt“,++x); /*Prefix increment*/
printf(“x=%dt“,x);
printf(“x=%dt“,–x); /*Prefix decrement*/
printf(“x=%dn“,x);
return 0;
}
Output:
x = 8 x = 9 x = 9 x = 8 x = 8
```

In the second printf(); statement, first the value of x is incremented and then printed; similarly in the fourth printf() statement first the value of x is decremented and then printed.

### Postfix Increment / Decrement

Here first the value of the variable is used in the operation and then increment/decrement is performed.

Let us take a variable whose value is **3**.

The statement **y = x++;** means first the value of **x** is assigned to **y** and then **x** is incremented. This

the statement is equivalent to these two statements-

**y=x;****x=x+1;**

Hence now the value of **x** is **4** and the value of **y** is **3**.

The statement **y=x- -;**

means first the value of **x** is assigned to **y** and then **x** is decremented.

This statement is equivalent to these two statements-

**y=x;****x=x-1;**

Hence now the value of **x** is **3** and the value of **y** is **4**.

```
/*Program to understand Postfix increment/decrement Operator in C*/
#include<stdio.h>
int main(void)
{
int x=8;
printf(“x=%dt“,x);
printf(“x=%dt“,x++); /*Postfix increment*/
printf(“x=%dt“,x);
printf(“x=%dt“,x--); /*Postfix decrement*/
printf(“x=%dn“,x);
return 0;
}
Output:
x = 8 x = 8 x = 9 x = 9 x = 8
```

In the second printf(); statement, first the value of x is printed and then incremented; similarly in the fourth printf(); statement first the value of x is printed and then decremented.

## Relational operators

Relational operators are used to comparing the values of two expressions depending on their relations. An expression that contains relational operators is called relational expression. If the relation is true “then the value of the relational expression is 1 and if the relation is false then the value of the expression is 0.

```
/* Program to understand the use of relational operators*/
#include<stdio.h>
main()
{
int a,b;
printf ("Enter values for a and b ");
scanf("%d%d",&a,&b) ;
if(a<b)
printf ("%d isless than %d\n", a, b);
if(a<=b)
printf ("%d is less than or equal to %d\n", a/ b);
if(a==b)
printf("%d is equal to %d\n",a,b);
if(a!=b)
printf("%d is not equal to %'d\n",a,b);
if(a>b)
printf("%d is greater than %d\n",a,b);
if(a>=b)
printf("%d is greater than or equal to %d\n",a,b);
}
Output:
Enter values for a and b: 12 7
12 is not equal to 7
12 is greater than 7
12 is greater than or equal to 7
```

### Logical Operators or Boolean Operators

An expression that combines two or more expressions is known as a logical expression. For combining these expressions, we use logical operators. These operators return 0 for false and 1 for true. The operands may be constants, variables, or expressions. C has three logical operators.

### Conditional Operators : Ternary Operators

the conditional operator is a ternary operator (? and:) which requires three expressions as operands. This is written as-**Test Expression? expression 1: expression 2**

1- Firs the Test Expression is evaluated. If Test Expression is true(nonzero), then expression 1 is evaluated and it becomes the value of the overall conditional expression.

2- If the test expression is false(zero), then expression 2 is evaluated and it becomes the value of the overall conditional expression.

For example, consider this conditional expression

**a > b? a: b**

```
/* Program to print the larger of two numbers using conditional operator */
#include <stdio.h>
int main()
{
int a, b, big;
printf("Enter 2 numbers\n");
scanf("%d%d", &a, &b);
(a > b) ? (big = a) : (big = b);
printf("Biggest of %d and %d is %d\n", a, b, big);
return 0;
}
Input:
Enter values for a and b 12 7
Larger
of 12 and 7 is 12
```

### Sizeof Operator

Sizeof is a unary operator. This operator gives the size of its operand in terms of bytes. The operand can be a variable, constant, or any data type (int, float, char, etc ). For example, sizeof(int) gives the bytes occupied by the int data type ie.2.

```
/* Program to understand the sizeof operator */
#include<stdio.h>
main() {
int var;
float floatType;
double doubleType;
char charType;
printf("Size of int: %d\n", sizeof(int));
printf("Size of float: %d\n", sizeof(float));
printf("Size of var: %d\n", sizeof(var));
printf("Size of an integer constant : %d byte\n", sizeof(45));
return 0;
}
Output:
Size of int: 2
Size of float: 4
Size of var :2
Size of an integer constant: 2
```

Generally, Sizeof operator is used to making portable programs i.e. programs that can be run on different machines. For example, if we write our program assuming int to be of 2 bytes, then it will not run correctly on a machine on which int is of 4 bytes. So, to make a general code that can run on all machines we can use Sizeof operator.