edited by
8,449 views

8 Answers

0 votes
0 votes
  1. Order of evaluation of the operands of any C operator, including the order of evaluation of function arguments in a function-call expression, and the order of evaluation of the sub-expressions within any expression is unspecified (except at a sequence point). The compiler will evaluate them in any order, and may choose another order when the same expression is evaluated again.

 

  1. There is no concept of left-to-right or right-to-left evaluation in C, which is not to be confused with left-to-right and right-to-left associativity of operators: the expression f1() + f2() + f3() is parsed as (f1() + f2()) + f3() due to left-to-right associativity of operator+, but the function call to f3 may be evaluated first, last, or between f1() or f2() at run time.

 

  1. If a side effect on a scalar object is unsequenced relative to another side effect on the same scalar object, the behavior is undefined.
i = ++i + i++; // undefined behavior
i = i++ + 1; // undefined behavior
f(++i, ++i); // undefined behavior
f(i = -1, i = -1); // undefined behavior

 

  1. If a side effect on a scalar object is unsequenced relative to a value computation using the value of the same scalar object, the behavior is undefined.
f(i, i++); // undefined behavior
a[i] = i++; // undefined bevahior

 

Reference:https://en.cppreference.com/w/c/language/eval_order

0 votes
0 votes
Correct Answer - Option 4 : what is assigned is compiler dependent

 EXPLANATION:
This question can be solved in two different ways by different compilers:
 1. First perform n++ then calculate ++n for index value
2. Calculate ++n for index value initially and then find n++. Therefore different compilers can solve this code in one of the above ways. Hence, the correct answer is "option 4".
Increment operation is of two types:
1. Pre increment (++n): Increase the value first & then substitute the value in variable n.
2. Post increment (n++): Initially substitute the value in n & then increase the value of n.
Answer:

Related questions

0 votes
0 votes
1 answer
2
Ankush Tiwari asked Aug 20, 2016
458 views
#include<stdio.h int main() { char *p1="xyz"; char *p2="xyz"; if(p1==p2) printf("equal"); else printf("unequal"); }Output is equal how??? please explain
1 votes
1 votes
1 answer
3
Prajwal Bhat asked Aug 19, 2016
1,083 views
#include<stdio.h>int main(){int a = 10, b = 20, c = 30, d = 40;printf("%d%d%d",a, b, c);printf("%d%d%d", d);return 0;}What is the Output and when I run it I am getting so...
2 votes
2 votes
1 answer
4
Registered user 7 asked Aug 19, 2016
538 views
#include<stdio.h #include<stdlib.h int main() { int p=9; printf("%d %d",p++,++p); } how it executed and also how printf function executed left to right or right to left?