# C Operators

## C Operators

An operator is simply a symbol used to perform an operation. There can be many types of operations such as arithmetic, logical, bitwise, etc. There are following types of operators for performing various types of operations in C language.

• Arithmetic Operators
• Relational Operators
• Shift Operators
• Logical Operators
• Bitwise Operators
• Ternary or Conditional Operators
• Assignment Operator
• Misc Operator

#### Precedence of Operators in C

The preponderance of the operator species as to which operator will be evaluated first and next. Specifies the direction of the operator to evaluate unionization; It can be right to left or left to right.

Let’s understand the precedence by the example given here:

int value=10+30*10;

The value variable will contain 310 because * (multiplicative operator) is evaluated before + (additive operator).

### Arithmetic operators

C supports all basic arithmetic operators. The following below given table shows all the basic arithmetic operators.

### Relational operators

The following table shows all the relation operators which are supported by c

### Logical operators

C language supports 3 logical operators. Suppose x = 1 and y = 0,

### Bitwise operators

Bitwise operators manipulate data at the bit level. These operators also shift from right to left. Bitwise operators are not applied to float or double (these are datatips, we will learn about them in the next tutorial).

Now lets take a look on truth table for bitwise &, | and ^

x y x & y x | y x ^ y

0 0 0 0 0

0 1 0 1 1

1 0 0 1 1

1 1 1 1 0

The bitwise shift operator, shifts the bit value. The left operand specifies the value to be shifted and the right operand specifies the number of positions that the bits in the value have to be shifted. Both operands have the same precedence.

Example :

x = 0001000

y = 2

x << y = 0100000

x >> y = 0000010

### Assignment Operators

These are the following assignment operators which are supported by C language.

#### Operator Description Example

= assigns values from right side operands to left side operand x=y

+= adds right operand to the left operand and assign the result to left x+=y is same as x=x+y

-= subtracts right operand from the left operand and assign the result to left operand x-=y is same as x=x-y

= mutiply left operand with the right operand and assign the result to left operand x=y is same as x=x*y

/= divides left operand with the right operand and assign the result to left operand x/=y is same as x=x/y

%= calculate modulus using two operands and assign the result to left operand x%=y is same as x=x%y

### Conditional operator

The conditional operators as the name suggest which is use for some conditions, in C language are known by one more name i.e., ternary operator.

#### Ternary Operator

? : Operator

It is actually the if condition that we use in C language for making decision. But using conditional operator, we turn the if condition statement into a short & simple operator.

Here is the syntax of a conditional operator.

expression a ? expression b: expression c

##### Explanation:

The question mark “?” in the given syntax represents the if part.

The first expression (expression a) generally returns either true or false, based on which it is decided whether (expression b) will be executed or (expression c)

If (expression a) returns true then the expression on the left side of ” : ” i.e (expression b) is executed.

If (expression a) returns false then the expression on the right side of ” : ” i.e (expression c) is executed.

### Special operator

#### Operator Description Example

sizeof use to return the size of an variable sizeof(x) return size of the variable x& it returns the address of an variable &x ; return address of the variable x* Pointer to a variable *x ; will be pointer to a variable x

The precedence & associativity of C operators is given below:

##### Category Operator Associativity

Postfix () [] -> . ++ – – Left to right

Unary + – ! ~ ++ – – (type)* & sizeof Right to left

Multiplicative * / % Left to right

Additive + – Left to right

Shift << >> Left to right

Relational < <= > >= Left to right

Equality == != Left to right

Bitwise AND & Left to right

Bitwise XOR ^ Left to right

Bitwise OR | Left to right

Logical AND && Left to right

Logical OR || left to right

Conditional ?: Right to left

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

Comma , Left to right

Categories C Tags