in Programming in C retagged by
1,961 views
0 votes
0 votes

If a= 10 then what will be the value of b in a C language code
.
If b= a++ + a++ + a++;
.

  1. 30.
  2. 23.
  3. 33.
  4. Undefined behaviour
in Programming in C retagged by
2.0k views

1 comment

I think option D Undefined Behaviour
1
1

4 Answers

2 votes
2 votes
Best answer
x = y++;

Here, y++ is post increment operator which returns the value of y, and increments y. What happens to the returned value? It is assigned to x. So, this assignment or the increment happens first? This is not specified by precedence/associativity rules and is internal to the compiler implementation as C standard does not fix a strict order. To help the programmer C standard says to avoid writing codes where the order of these executions changes the output. That is, never to modify a variable more than once in an arithmetic expression. (When we have logical && and || due to short circuit rules, we may skip undefined behaviour). 

Here, a++ is done 3 times in an arithmetic expression - crying

selected by
by

4 Comments

problem is these types of questions are asked in different exam without the choice of undefined behaviour then what is your advice to students  @ArjunSuresh
0
0
Not to give those exams. From 1991-2016 there has been not even 1 such question in GATE and there is a reason for that.. Same with TIFR/CMI/ISI - never asked. ISRO - even if asked gives mark in answer debate. I guess same with NET exam. So, what different exams? - if you mean some placement tests, just toss a coin and mark an answer.
4
4
Lol      this was question of NET ( i added the choice D here myself), similiar question was asked in some IT officer post and also in central govt lect. post and acc to their anskey ans was 30

anyway thanks , you advice is accepted
0
0
0 votes
0 votes

answer is 30 reason is there is a space between unary ++ and a binary + 

4 Comments

Then it would be option D i tried in online editor and i got 33
1
1

ok shivani..tysmiley i got it

1
1
for some compiler it will give 33 and for some other compiler answer will be different so it is undefined. Ordering and evaluation will depends on compiler.
1
1
0 votes
0 votes
0 votes
0 votes
Its 33..

This is Cascading... and it is evaluated from right to left..

Related questions