# Operators and Expressions in C

## What is an Operator?

An Operator is a function which works on one or more operands to produce a new value, e.g. +,-,* ,/ are some operators. Operators in C differs at some points from mathematics. So C has its own system to manipulate operators.

## What is operands?

operand is the data on which operator operates, it can be constant or variable or any other identifier.

For Example

In the expression x=y+2; // x,y,2 are operands and = and + are operators.

Types of operators based on number of operands.

Unary Operators :- Unary operator works only on one operand e.g -, ++, –, & , * etc. are unary operators.

Binary Operators :- Binary operators work on two operands. e.g. +-*/ etc.

Ternary Operator :- Ternary operator is the only operator which works on three operands. This operator is also called conditional operator. e.g. (?:) is ternary operator.

## Expression

An expression is a combination of one or more of variables, constants, operators and function calls that results in some useful value after computation.

x=2;

sum=x+y;

printf(“%d”,x);

are expressions. expressions in c are always terminated by semi colon.

## Kinds of operators as per their functions

Assignment Operator | = |

Arithmetic Operator | + – * / % |

Arithmetic Assignment Operator | += -= *= /= %= |

Comma Operator | , |

Increment /Decrement Operator | ++ — |

Logical Operator | && || ! |

Comparison Operator | == != <= >= < > |

Compile-time Operator | Sizeof |

Typecast Operator | (type) |

Bitwise Operator | & | ^ |

Bitwise Assignment Operator | &= |= ^= |

Shift Operator | << >> |

Shift Assignment Operator | <<= >>= |

Member Selection Operator | . -> |

Address Operator | & |

Pointer Operator | * |

Conditional Operator | ?: |

Array Index Operator | [] |

Function Call Operator | () |

Stringization Operator | # |

Token Pasting Operator | ## |

**Precedence and Associativity of Operators**

**Precedence**

When more than one operators are involved in an expression, then the precedence determines the order in which the operands are evaluated. Eg a+b*c in this case multiplication will be evaluated first then followed by addition operation.

**Associativity**

If an expression have more than one operator with same precedence level then associativity determines the direction of grouping of operators. It is of two types-

Left Associative (Left to Right)

When two or more operators having same precedence are encountered in an expression and are evaluated starting from left towards right. Then such operators are called left associative.

Right Associative (Right to Left)

These operators are evaluated starting from right towards left.

So combination of precedence and associativity determines the order of execution of execution of operators. Which is being shown in the table below-

SNo | Operator Precedence | Associativity |

1 | () [] -> . | Left to right |

2 | ! ~ + – * & sizeof (type) ++ — | Right to left |

3 | * / % | Left to right |

4 | + – (Binary Operator) | Left to right |

5 | < <= >= > | Left to right |

6 | == != | Left to right |

7 | & | Left to right |

8 | ^ | Left to right |

9 | | | Left to right |

10 | && | Left to right |

11 | || | Left to right |

12 | ?: | Right to left |

13 | = *= /= %= += -= &= ^= |= <<= >>= | Right to left |

14 | , | Left to right |

### **1) Assignment Operator (=)**

Assignment operator is used to assign a value to a variable. e.g x=5; here value 5 has been assigned to variable x (It must not be confused as equal to in mathematics). You can also do multiple assignment like this x=y=z=0; It means that all the three variables has been assigned zero in a single expression.

```
#include
void main()
{
int x;
x=5;
printf("The value of x=%d",x);
}
```

**2) Arithmetic Operators (+ – * / % )**

These are the basic arithmetic operators

+ Addition To add two or more numbers 2+2=4

– Subtraction To Subtract two or more numbers 4-2=2

* Multiplication To Multiply two or more numbers 2*3=6

/ Division To Divide two Numbers, It works in two ways

**Integer division** – If both the operators are integers then any fractional part in the result is truncated, e.g. 5/2 will result in 2.

**Floating point division** – If any of the operands of division operator is floating point value then it will result in it will have fractional part as well. e.g. 7/3.5 = 2.0

% Modulus Operator gives the remainder as output when applied on two integer values. It can’t be applied on floating point numbers. e.g. -10%3 = -1, 10%-3= 1, -10%-3= -1, etc

```
#include
void main()
{
int x=5,y=3,sum;
printf("The sum of x and y=%d",x+y);
printf("The subtraction of x and y=%d",x-y);
printf("The Multiplication of x and y=%d",x*y);
printf("The division of x and y=%d",x/y);
printf("The modulus of x and y=%d",x%y);
sum=x+y;
printf("The sum of x and y=%d",sum);
}
```

### 3) **Increment and Decrement Operators**

To increase or decrease the value of a variable by one, C uses special operators called increment and decrement operators respectively. Increment is denoted by ++ and decrement by –.

Characteristics :

- More efficient and faster , shorter to write and faster to execute.
- Can be used as prefix like ++x/–x. It signifies that the value of x is to be incremented /decremented before evaluating the expression.
- Can be used as postfix like x++/x–. It signifies that the value of x will be incremented /decremented after the expression has been evaluated.

```
#include
void main()
{
int x=5;
y=++x;
z=x++;
printf("The value of y =%d\n",y);
printf("The value of z =%d",z);
}
```

Result

The value of y=6

The value of z=5

### 4) **Comma Operator**

Comma operator is used to group pair of sub-expressions. Each sub-expression is evaluated from left to right. e.g x=5,y=7;

### 5) **Comparison Operators**

Comparison operators compares the values of their operands. The result of these operators is of boolean type means either it is true or false.

Relational Operators | Meaning | Example |

< | Less Than | a<b, 4<7<=”” span=””></b,> |

> | Greater Than | b>a , 6>3 |

<= | Less Than equal to | A<=b |

>= | Greater than equal to | a>=b |

Equality Operators | ||

== | Equal to | A==b, x==5 |

!= | Not equal to | A!=b |

### 6) **Logical Operators**

C Language has three types of Logical operators which are evaluated as boolean values zero is taken as FALSE and non-zero as TRUE.

Operators | Meaning | Examples |

&& | Logical And | (a<b)&&(a<c), (4<5)&&(6<7)<=”” span=””></b)&&(a<c),> |

|| | Logical or | (a<b)||(a<c), (4<5)||(6<7)<=”” span=””></b)||(a<c),> |

! | Logical Not | !x, !a |

**Evaluation of AND Operator **

Result of AND operator is TRUE only when both/all the inputs are true (1). In the below mentioned example 0 stands for FALSE and 1 Stands for TRUE

A |
B |
Result |

0 | 0 | 0 |

0 | 1 | 0 |

1 | 0 | 0 |

1 | 1 | 1 |

**Evaluation of OR Operator **

Or operator evaluates to zero/FALSE when it’s all inputs are zero as shown in table below.

A |
B |
Result |

0 | 0 | 0 |

0 | 1 | 1 |

1 | 0 | 1 |

1 | 1 | 1 |

**Evaluation of NOT Operator **

NOT is a unary operator and evaluates the opposite of the input. If input is TRUE result will be FALSE.

A |
Result |

0 | 1 |

1 | 0 |

```
#include
void main()
{
int English,Math;
printf("Enter the Marks in English and Math");
scanf("%d%d",&English,&Math);
if(English>=50 && Math>=50)
{
printf("Pass");
}
else
printf("Fail");
}
```

**Short Circuiting**

If the left operand of && is FALSE or left operand of || is TRUE then it is unnecessary to evaluate the right operand because if any of the input of && are FALSE then result will always be FALSE and in case of or if any of the input is TRUE then it’s result will always be TRUE. This process is called Short Circuiting.

**7) Bitwise Operators**

As discussed above C Language have bitwise AND, OR, XOR, COMPLEMENT, Left shift and right shift operators. As the name suggest these operators work on bit level and work only on integers. Out of these complement is unary operator rest other are binary.

**Bitwise AND Operator**

It will produce bitwise AND result of two operands

A = 01110

B= 10111

A&B= 00110

**Bitwise OR Operator**

It will produce bitwise OR result of two operands

A = 01110

B= 10111

A|B= 11111

**Bitwise Complement Operator**

It will produce bitwise 2’s Complement of an operand. in C complement of a number N is equal to -(N)+1 means 1’s complement+1.

A=11001

~A = 00111

Bitwise Exclusive OR Operator

It will produce 0 is both the inputs are equal and 1 if both inputs are unequal.

A =101010

B =111011

A^B =010001

**Left Shift Operator**

Left shift operator shifts specified number of bits towards left

A=000010

A<<2 =001000

**Right Shift Operator**

Right shift operator shifts specified number of bits towards right.

A=100000

A>>2 =001000

Bitwise Assignment Operator

a&=b

a|=b

a^=b

a<<=b

a>>=b

**8) Conditional Operator**

?: is the conditional operator which takes three operands. We may write it like exp1?exp2:exp3. This operator is just short notation of if-else statement.

e.g. if you wish to compare two numbers a,b then it can be solved as under:

```
#include
void main()
{
int a=5, b=4;
int c;
c=(a>b)?a:b;
printf("The greater value is =%d",c);
}
```

**9) Typecast Operator**

In order to convert one type of data to another, typecast operator is used. To typecast the int value to double here is an example

```
#include
void main()
{
int a=5;
double d;
d=(double)a;
printf("The double value of a =%f",d);
}
```

**10) Compile time operator**

The sizeof operator is a unary operator also called compile time operator. It returns the size in bytes of its operands. Basically its main purpose is to allocate memory during compile time.

**11) Address of Operator**

Address of operator evaluates the memory address of the operand, denoted by &. It is a unary operator.

e.g. if you have x variable which has the value 5 then we can write it x=5; to know it’s memory location use address operator as &x.

**12) Indirection Operator**

Denoted by * and is a unary operator. It points to the value at the address. Is called indirection operator and reverse of address operator.

**13) Array Index Operator**

An array index operator is used to access the elements of the array, denoted by opening and closing brackets []. Would be discussed in coming topics.

**14) Function Call Operator**

The pair of opening and closing parenthesis pair is called function call operator. It is followed by function name and encloses the arguments or parameters of the function.

e.g int sum(); int sum(int x, int y);

**15) Stringization Operator**

It is a stringization operator which causes it’s operands to be surrounded by double quotes

e.g #define friends(x,y) printf(#x “and “#y” are friends”)

**16) Member Selection Operator**

The . and -> are called member selection operator and are used to access the members of structure and unions.

To Download Official TurboC Compiler from** here**

**You may also like :**

**C Language Introduction Elements of C Language Variables in C**

### What is an operand ?

operand is the data on which operator operates, it can be constant or variable or any other identifier.

### What are Arithmetic Operators ?

**Arithmetic Operators (+ – * / % )**

These are the basic arithmetic operators

+ Addition To add two or more numbers 2+2=4

– Subtraction To Subtract two or more numbers 4-2=2

* Multiplication To Multiply two or more numbers 2*3=6

/ Division To Divide two Numbers, It works in two ways

**Integer division** – If both the operators are integers then any fractional part in the result is truncated, e.g. 5/2 will result in 2.

**Floating point division** – If any of the operands of division operator is floating point value then it will result in it will have fractional part as well. e.g. 7/3.5 = 2.0

% Modulus Operator gives the remainder as output when applied on two integer values. It can’t be applied on floating point numbers. e.g. -10%3 = -1, 10%-3= 1, -10%-3= -1, etc

### What is an Expression ?

An expression is a combination of one or more of variables, constants, operators and function calls that results in some useful value after computation.

### What is Precedence and Associativity ?

**Precedence**

When more than one operators are involved in an expression, then the precedence determines the order in which the operands are evaluated. Eg a+b*c in this case multiplication will be evaluated first then followed by addition operation.**Associativity**

If an expression have more than one operator with same precedence level then associativity determines the direction of grouping of operators. It is of two types-

Left Associative (Left to Right)

When two or more operators having same precedence are encountered in an expression and are evaluated starting from left towards right. Then such operators are called left associative.

Right Associative (Right to Left)

These operators are evaluated starting from right towards left.