Procedence and associativity C

If we have an expression
J = 2 * 3 / 4 + 4 / 4 + 8 -2 + 5 / 8 ;
You may wonder which operation will be carried out first. In order to solve the dilemma, ‘C’ provides precedence i. e. the order of evaluation. The following table gives the precedence and associatively of operators. The precedence decreases from top to bottom i. e. operations with higher precedence (top) are carried out before operations with lower precedence. Using parenthesis can alter the natural order.

Procedence and associativityProcedence and associativity

E.g. In the table
* / % occurabove + and -

Thus multiplication, division and remainder operation in an expression will be carried out before addition and subtraction operation. Again the question arises as to which operation among those with same precedence will be carried out first? The order in which operation within the same precedence group is carried out is called associativity. Most of the operations have associativity from left to right i. e. to solve conflict between *, /, % start the expression from left and perform the operation as the operators are encountered going to right.

The idea can be made clear by evaluating the previous expression stepwise.
j = 2* 3 / 4 + 4 / 4 + 8 - 2 + 5 / 8;
j = 6 / 4 + 4 / 4 + 8 – 2 + 5 / 8; operation *
j = 1 + 4 / 4 + 8 – 2 + 5 / 8 ; operation /
j = 1 + 1 + 8 – 2 + 5 / 8; operation /
j = 1 + 1 + 8 – 2 + 0 operation/ (0 – since it is an integer division.)
j = 2 + 8 – 2; operation +
j = 10 – 2; operation +
j = 8; operation –
Multiplication and division are performed before addition and subtraction.

Procedence and associativityProcedence and associativity
Above is a table of escape sequences. Escape sequences are written as backward slash followed by a character. The backward slash is called the escape character because it makes the character following it to escape from its original meaning and gives a special meaning to it. Though an escape sequence may appear as two characters (a backslash and another character), it is actually a single character. Certain nonprinting characters as well as double quote (“), apostrophe (‘), question mark (?) and backslash ( ) can be printed using escape sequences..

Tabstop: An 80-column screen has 10 tabstop i. e. screen is divided into 10 zones of 8 columns each. Printing a tab takes cursor to beginning of next printing zone.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

C Topics