You are on page 1of 9

Shift Operators:

There are two shift operators in C programming:

 Right shift operator


 Left shift operator.

Right shift operator:

Right shift operator shifts all bits towards right by certain number of specified bits.
It is denoted by >>.

Left shift operator:

Left shift operator shifts all bits towards left by a certain number of specified bits.
The bit positions that have been vacated by the left shift operator are filled with 0.
The symbol of the left shift operator is <<.

For Example:
Output:

Special Operators:

Comma Operator:

Comma operators are used to link related expressions together.

For example:

int a, c = 5, d;

The sizeof operator:

The sizeof is a unary operator that returns the size of data (constants, variables,
array, structure, etc).

For Example:
Output:

Precedence and Associativity of Operators:

Precedence of Operators:

The precedence of operators determines which operator is executed first if there


is more than one operator in an expression.

int x = 5 - 17* 6;

In C, the precedence of * is higher than - and =. Hence, 17 * 6 is evaluated first.


Then the expression involving - is evaluated as the precedence of - is higher than
that of =

Operators Precedence and Associativity table:


Associativity of Operators:

The associativity of operators determines the direction in which an expression is


evaluated. For example,

b = a;

Here, the value of a is assigned to b, and not the other way around. It's because
the associativity of the = operator is from right to left.

Another Example:

1 == 2 != 3

Here, operators == and != have the same precedence. And, their associativity is
from left to right. Hence, 1 == 2 is executed first.

The expression above is equivalent to:

(1 == 2) != 3
If a statement has multiple operators, you can use parentheses () to make the
code more readable.

Arithmetic Expressions:

An arithmetic expression is an expression that consists of operands and arithmetic


operators. An arithmetic expression computes a value of type int, float or double.

The expressions are evaluated by performing one operation at a time. The


precedence and associativity of operators decide the order of the evaluation of
individual operations.

When individual operations are performed, the following cases can be


happened:

 When both the operands are of type integer, then arithmetic will be
performed, and the result of the operation would be an integer value. For
example, 3/2 will yield 1 not 1.5 as the fractional part is ignored.
 When both the operands are of type float, then arithmetic will be
performed, and the result of the operation would be a real value. For
example, 2.0/2.0 will yield 1.0, not 1.
 If one operand is of type integer and another operand is of type real, then
the mixed arithmetic will be performed. In this case, the first operand is
converted into a real operand, and then arithmetic is performed to produce
the real value. For example, 6/2.0 will yield 3.0 as the first value of 6 is
converted into 6.0 and then arithmetic is performed to produce 3.0.

For Example:
Type Conversions in C:

A type cast is basically a conversion from one type to another. There are two
types of type conversion:

1. Implicit Type Conversion:

Also known as ‘automatic type conversion’.

 Done by the compiler on its own, without any external trigger from the
user.
 Generally takes place when in an expression more than one data type is
present. In such condition type conversion (type promotion) takes place to
avoid loss of data.
 All the data types of the variables are upgraded to the data type of the
variable with largest data type.

For Example:

Output:

2. Explicit Type conversion:


This process is also called type casting and it is user defined. Here the user can
type cast the result to make it of a particular data type.

Syntax:

(type) expression

For Example:

Output:

You might also like