- C programming language supports various operators to perform various operations like logical, mathematical, relational, arithmetic, bitwise operators etc. In this tutorial we will learn about various C operators in brief with sample programs.

An operator in C is a symbol used to perform logical and mathematical operations in a C program.

A statement containing operators and variables is called an Expression. C operators connects constants and variables to form expressions.

*For Example*

**2 x (length + breadth);**

- x and + are operators
- 2 is a constant
- length and breadth are variables
- "2x(length + breadth)" is an expression, performing one logical task

## Special Operators in C

Below are some special operators supported by C language.Operators | Description | Example |
---|---|---|

* | It represents a pointer to a variable. | int *ptr; ptr is a pointer to variable of data type int. |

& | Returns address of the variable. | &Val will give address of Val variable. |

Sizeof() | Returns the size of a variable or data type. It is not a function. | Sizeof(int); Returns the size of integer data type. |

**Address of Operator(&)**

The & is a unary operator in C which returns the memory address of the passed operand. This is also known as "address of" operator.

A pointer contains the memory address of some object.

**Value of Operator(*)**

The * is a unary operator which returns the value of object pointer by a pointer variable. It is known as **value of** operator.

It is also used for declaring pointer variable.

*For Example*

int A = 100;

int *ptr = &A;

In the first statement, we first declare an integer variable and initialize it with value 100. In second statement, we are declaring a pointer to a variable of type int and initializing it with address of A.

**Sizeof Operator**

The sizeof is a compile time operator not a standard library function. The sizeof is a unary operator which returns the size of passed variable or data type in bytes.

As we know, that size of basic data types in C is system dependent, So we can use sizeof operator to dynamically determine the size of variable at run time.

## C program to show use of Sizeof, & and * Operator

#include<stdio.h> int main() { int A = 10; /* Use of & and * Operator */ int *ptr = &A; printf("Value of A is %d\n", *ptr); /* Use of Sizeof Operator to dynamically determine the size of data types and variables */ printf("Size of variable A is %d\n", sizeof(A)); printf("Size of an Integer variable is %d\n", sizeof(int)); printf("Size of long int variable is %d\n", sizeof(long int)); printf("Size of char variable is %d\n", sizeof(char)); printf("Size of float variable is %d\n", sizeof(float)); return(0); }

Output

Value of A is 10 Size of variable A is 4 Size of an Integer variable is 4 Size of long int variable is 4 Size of char variable is 1 Size of float variable is 4

## Operator Precedence

An expression in C may contains more than one variables and operators. Not every operator in C is of same precedence, some operators are of higher priority than others; for example increment operator(++) is of higher precedence than addition operator(+). Operator precedence defines the sequence of evaluation for operators of an expression, to resolve and ambiguity.

*For Example*

X = 1 + 2 * 3;

Without any Operator precedence, above expression can be evaluated in two different ways producing two different values for X.

**Performing addition before multiplication**

X = 3 * 3 = 9

**Performing multiplication before addition**

X = 1 + 6 = 7

To resolve this confusion, we assign different precedence to different operator. As the precedence of multiplication is more than addition, Correct value of X is 7.

The associativity specifies the operators direction to be evaluated, it may be left to right or right to left.

Below table lists the operators in decreasing order of their precedence.

Operator | Associativity |
---|---|

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

! ~ -- ++ (type)* & sizeof() | Right to left |

/ * % | Left to right |

- + | Left to right |

<< >> | Left to right |

< <= > >= | Left to right |

== != | Left to right |

& | Left to right |

^ | Left to right |

| | Left to right |

&& | Left to right |

|| | Left to right |

?: | Right to left |

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

, | Left to right |

#### Points to Remember

- If two operators of same priority appear at a same level, more priority is given to the operator appearing first.

For Example :

X = A*B + C/D;

In above expression, * and / both have the same priority but * will be evaluated first as it appears first. - In any expression containing parentheses, innermost parentheses is given more priority then the outer one and so on.

For Example :

X = 2 * (3 + 4);

X = 2 * 7; /* + gets evaluated first because of parentheses */

X = 14;