# C Operators and Expressions

An operator specifies an operation to be performed that yields a value. The variables, constants can be joined by various operators to form an expression. 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 -

1. Arithmetic operators
2. Assignment operators
3. Increment and Decrement operators
4. Relational operators
5. Logical operators
6. Conditional operator

## Special Operators

Special operators available in c are as follows:

#1: Arithmetic Operators

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

1. Binary arithmetic operators
2. Unary arithmetic operators

 Arithmetic Operators Arithmetic Operator Symbol Description Example + Performs addition 1+1=2 – Performs subtraction 1-1=0 * Performs multiplication 2*2=4 / Performs division 10/2=5 % Performs modular division(Gives remainder after performing division) 11%2=1 +(Unary Plus) Gives value of operand +5=5 -(Unary Minus) Changes sign of its operand -5

MIND IT !

%(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.

When both operands are integers, the resulting value is always an integer. Let us take two variables a and b. The value of a is 17 and b is 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 result is only integer part of quotient. After modulus operation the result will be remainder part of integer division. The second operand must be nonzero for division and modulus operations.

```/*Integer arithmetic operations*/
#include<stdio.h>
int main(void)
{
int a=17, b=4;
printf("Sum=%d\n", a+b);
printf("Difference=%d\n", a-b);
printf("Product=%d\n", a*b);
printf("Quotient=%d\n", a/b);
printf("Remainder=%d\n", a%b);
return 0;
}
```

Output:
Sum=21
Difference=13
Product=68
Quotient=4
Remainder=1

When both operands are floating point type, the result is also floating point type. Let us take two variables a and b. The value of a is 12.4 and b is 3.1, the results of the following operations are-

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

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

```/*Program to understand the floating point arithmetic operation*/
#include<stdio.h>
int main(void)
{
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);
return 0;
}
```

Output:
Sum=16.20
Difference=8.60
Product=47.12
a/b=3.26

When one operand is of integer type and the other is of floating type, the resulting value is floating type. If value of a is 12 and of b is 2.5.

Expression Result
a+b 14.5
a-b 9.5
a*b 30.0
a/b 4.8

Sometime mixed mode arithmetic can help in getting exact results. For example the result of expression 5/2 will be 2, since integer arithmetic is applied. If we want exact result we can make one of the operands float type. For example 5.0/2 or 5/2.0, both will give result 2.5.

#2: Assignment Operators

A value can be stored in a variable using assignment operator. The operand on the left hand side should be a variable, while the operand on the right hand side can be any variable, constant or expression. The value of 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*/

The value that is being assigned is considered as value of the assignment expression. For example x=8 is an assignment expression whose value is8.

We can have multiple assignment expression also, for example-
x=y=z=20

Here all the three variables x, y, z will be assigned value 20, and the value of the whole expression will be 20. If we put a semicolon after the assignment expression then it becomes an assignment statement.

For example these are assignment statements-
x=8;
y=5;
s=x+y-2;
x=y=z=20;

When the variable on the left hand side of assignment operator also occurs on right hand side, we can avoid writing the variable twice by using compound assignment operators.

For example-
x=x+5 can also be written as-
x+=5

Here += is a compound assignment operator.

Similarly we have other compound assignment operators-

x - =5 is equivalent to x=x-5
y*=5 is equivalent to y=y*5
sum/=5 is equivalent to sum=sum/5
k%=5+x is equivalent to k=k% (5+x)
a*=b+5 is equivalent to a=a* (b+5)

#3: Increment and Decrement Operators

The increment (++) and decrement (- -) operators are unary operators because they operate on as single operand. The increment operator increments the value of the variable by 1, while decrement operator decrements the value of the variable by 1.

++x is equivalent to x=x+1
- -x is equivalent to x=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.

These operators are two types :

1. Prefix increment/decrement - operator is written before the operand (e.g. ++x or - -x)
2. Postfix increment/decrement - operator is written after the operand (e.g. x++ or x- -)

#4: Relational Operators Operators

Rational operators are used to compare values of two expressions. An expression that contains relational operators is called relational expression. If the relation is true then the value of relational expression is 1 and if the relation is false then the value of expression is 0. The relational operators are-

Operator Meaning
< less than
<= less than or equal to
== equal to
!= Not equal to
> Greater than
>= Greater than or equal to

Let us take two variables a = 9 and b = 5, and form simple relational expressions with them-

Expression Relation Value of Expression
a False 0
a<=b False 0
a==b False 0
a!=b True 1
a>b True 1
a>=b True 1
a==0 False 0
b!=0 True 1
a>8 True 1
2>4 False 0

The relational are generally used in if...else construct and loops.

```/*Program to understand the use of relational operators*/
#include<stdio.h>
int main(void)
{
int a, b;
printf ("Enter the values for a and b : ");
scanf ("%d%d", &a, &b);

if(a<b)
printf ("%d is less 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);
return 0;
}
```

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

It is important to note that the assignment operator(=) and equality operator(==) are entirely different. Assignment operator is used for assigning values while equality operator is used to compare two expressions.

Beginners generally confuse between the two and use one in the place of another and this leads to an error difficult to find out.

For example if in the above program we use = instead of ==, then we will get wrong output.

``` if(a=b)
printf ("%d is equal to %d\n", a, b);
```

Here a=b is treated as an assignment expression, so the value of b is assigned to variable a, ant the value of the whole expression becomes 7 which is non-zero, and so the next statement is executed.