Tuesday, August 8, 2017

C Operator Precedence and Associativity

C Operator Precedence and Associativity

Precedence of Operators in C:

Operator precedence tells the order, in which operations are performed in an expression.

For example:
int n = 200 + 10 * 50;

Would the above expression be evaluated as (200 + 10) * 50 or 200 + (10 * 50). Would the addition be done first or multiplication. We all have studied BODMAS rule in mathematics, thus, we know that multiplication is done before addition. So the above expression would be solved like this given below-

Step 1 : 200 + (10 * 50)
Step 2 : 200 + 500
Result : 700

The precedence can be changed using parenthesis. The sub-expression in parenthesis would be evaluated first, no matter what operator has been used outside the sub-expression.

For example:
int n = (200 + 10) * 50;

This expression would be solved like this as given below-

Step 1 : 210 * 50;
Result : 10500

If nested parenthesis has been used, then innermost parenthesis would be evaluated first.

For example:
int n = 2 + (3 + (5 – 1)) * 4;

This expression would be evaluated like this as given below-

Step 1 : 2 + (3 + 4) * 4
Step 2 : 2 + 7 * 4
Step 3 : 2 + 28
Result : 30

If there is more than one parenthesis at different places in an expression, then sub-expressions in the parenthesis would be evaluated from left to right.

Associativity of Operators in C:

In C programming language, many operators can have same precedence or priority. If, in an expression, more than one operator have same precedence, the problem is resolved using the associativity of operators. All operators in C have either ‘Left to Right’ or ‘Right to Left associativity. It means, in an expression, if two operators have same precedence or priority, and their associativity is ‘Left to Right’, the operator, which comes first from left to right would be evaluated first. Let us understand this with an example given below-

int n = 5 + 3 * 4 / 2 – 1

This expression would be evaluated like this as given below-

Step 1 : 5 + (3 * 4) / 2 – 1
Step 2 : 5 + 12 / 2 – 1
Step 3 : 5 + (12 / 2 ) – 1
Step 4 : 5 + 6 – 1
Step 5 : (5 + 6) – 1
Step 6 : 11 – 1
Result : 10

In the above example, operators ‘*’ and ‘/’ have the same precedence. Their associativity is from left to right. Therefore, multiplication would take place before division. The same way, operators ‘+’ and ‘-‘ have same precedence, and associativity ‘Left to Right’. Hence, addition will be done before subtraction.

Again, precedence can be changed using parenthesis.

The precedence and associativity table of all operators in C has been given below for your quick reference. Operators are given from top to bottom in descending precedence or priority.

Precedence
Operator
Description
Associativity
1
Highest
++
Postfix increment
Left-to-right
--
Postfix decrement
()
Function call
[]
Array subscripting
.
Element selection by reference
->
Element selection through pointer
2
++
Prefix increment
Right-to-left
--
Prefix decrement
+
Unary plus
-
Unary minus
!
Logical NOT
~
Bitwise NOT (One's Complement)
(type)
Type cast
*
Indirection (dereference)
&
Address-of
sizeof
Size-of
3
*
Multiplication
Left-to-right
/
Division
%
Modulo (remainder)
4
+
Addition
Left-to-right
-
Subtraction
5
<< 
Bitwise  left shift
Left-to-right
>> 
Bitwise right shift
6
< 
Less than
Left-to-right
<=
Less than or equal to
> 
Greater than
>=
Greater than or equal to
7
==
Equal to
Left-to-right
!=
Not equal to
8
&
Bitwise AND
Left-to-right
9
^
Bitwise XOR (exclusive or)
Left-to-right
10
|
Bitwise OR (inclusive or)
Left-to-right
11
&&
Logical AND
Left-to-right
12
||
Logical OR
Left-to-right
13
?:
Ternary  conditional
Right-to-left
14
=
Direct assignment
Right-to-left
+=
Assignment by sum
-=
Assignment by difference
*=
Assignment by product
/=
Assignment by quotient
%=
Assignment by remainder
<<=
Assignment by bitwise left shift
>>=
Assignment by bitwise right shift
&=
Assignment by bitwise AND
^=
Assignment by bitwise XOR
|=
Assignment by bitwise OR
15
lowest
,
Comma
Left-to-right

This is all about precedence and associativity in C computer language. All operators will be described in later posts, wherever they are used.

If you are interested in how to create simple graphics in C, please visit my blog Graphics in C.

Tuesday, August 1, 2017

Arithmetic Operations in C

Arithmetic Operators:

There are 5 arithmetic operators in C computer language. These are given below-

+
Addition
-
Subtraction
*
Multiplication
/
Division
%
Modulus

These operators are used in arithmetic operations. Modulus operator (%) is used to get remainder after division between two numbers.

For example:

int a = 10, b = 7;
int sum = a + b;  /* gives 17 */
int sub = a – b;  /* gives 3 */
int mul = a * b;  /* gives 70 */
int div = a / b;  /* gives 1 */
int md = a % b;  /* gives 3 */

Variables or constants which are operated upon by these operators are called operands. In the above examples, variables ‘a’ and ‘b’ are operands. The combination of variables, constants, and operators is called ‘expression’. The equal sign ‘=’ is the assignment operator. It assigns the value of an expression, or variable, or constant written on the right hand side of it to the variable written on the left side of it. There should be only one variable on the left hand side of ‘=’.

There are two types of arithmetic instructions in C language as given below-
All the operands and constants, and result variable are of the same type i.e. all are integers, or all are floats.

For example:

int a = 10, b = 6;
int sum = a + b;   /* All are of integer type */

float a = 5.0, b = 7.0;
float mul = a * b;    /* All are of float type */

Second types of arithmetic instructions are those, which have a mix of integers and floats.

For example:

int basic = 3000;
float da = 0.12 * basic;
float gross = basic + da;

Whenever there is a mix of integers and floats, an automatic conversion occurs and data types are promoted from ‘int’ to ‘float’ or demoted from ‘float’ to ‘int’ depending on the case. In the above case, ‘basic’ is of integer type, when it is multiplied by a floating point number ‘0.12’, it is promoted to ‘float’, and the result, which is of type ‘float’, is assigned to float type variable ‘da’. Same happens with the next statement.

Please note:
  • An arithmetic operation between two integers always results in an integer.
  • An arithmetic operation between two floats always results in a ‘float’.
  • An operation between a ‘float’ and ‘int’ always results in a ‘float’. ‘int’ is, first, promoted to ‘float’, and then the operation is carried out.


For example:

7 / 2 = 3  /* operation between two ints gives the result in ‘int’. */

2 / 7 = 0  /* operation between two ints gives the result in ‘int’. */

7.0 / 2.0 = 3.5 
/* operation between two floats gives the result in ‘float’. */

2.0 / 7.0 = 0.285714
/* operation between two floats gives the result in float. */

7.0 / 2 = 3.5  /* operation between a float and a int results in a float */

7 / 2.0 = 3.5   /* operation between an int and a float results in a float */

If result of the right hand side expression is ‘int’ and left hand side variable of assignment operator is ‘float’, then result of the right hand side expression is promoted to ‘float’ and assigned to left hand side variable.

If result of the right hand expression is ‘float’ and left hand side variable is ‘int’, then result of right hand side expression is demoted to ‘int’ and assigned to left hand side variable.

For example:

int a;
float b;

a = 7 / 2;   /* result = 3 (7 and 2 are integers, result is integer.) */

a = 7.0 / 2   
/* result = 3 (7.0 is ‘float’. 2 is integer, it is promoted to ‘float’, 2.0. Result of operation is float, which is 3.5. But a is ‘int’, it can’t contain ‘float’, so 3.5 is demoted to ‘int’ 3, and stored in ‘a’.) */

a = 7.0 / 2.0   /* result = 3 */

b = 7 / 2   
 /* result = 3.0 (7 and 2, both are integers, result is an integer, 3. But ‘b’ is float, so 3 is promoted to float 3.0 and assigned to ‘b’.) */

b = 7.0 / 2 
 /* result = 3.5 (7.0 is a ‘float’, whereas 2 is an ‘int’. 2 is promoted to ‘float’, 2.0. Result of operation is float, 3.5. ‘b’ is float. 3.5 is assigned to ‘b’.) */

b = 7 / 2.0    /* result = 3.5 */

b = 7.0 / 2.0    /* result = 3.5 */

That’s all about arithmetic operators and automatic type conversion.

If you are interested in creating graphics using C language, please visit the blog Graphics in C.

   

C Operator Precedence and Associativity

C Operator Precedence and Associativity Precedence of Operators in C: Operator precedence tells the order, in which operations are per...