retagged by
7,292 views
3 3 votes

The following three 'C' language statements is equivalent to which single statement?

y=y+1;
z=x+y;
x=x+1
  1. z = x + y + 2;
  2. z = (x++) + (++y);
  3. z = (x++) + (y++);
  4. z = (x++) + (++y) + 1;

4 Answers

Best answer
16 16 votes
Here y is incrementing before calculation of z and x incrementing after calculation of z

So y will be preincrement and x is postincrement

So Ans B)
selected by
3 3 votes

Just take x,y,z =1 and apply to all the options .

B and C will give correct answer

But as Y is incremented before X, and we are only using updated value of Y to find value of Z, so we will only choose B)

Answer is B).

0 0 votes
as u can see here the increment of y is written before z expression so preincrement ,

and then x is incremented after z expression so post increment

thats why ans will be x= ++y + x++
0 0 votes

option B is right answer  (Upvote it if it is helpfull) 

B. z = (x++) + (++y);

here we need to perform stack evaluation 

so for the stack evalution the steps are

  1. pre increment/ decrement
  2. substitute the value 
  3. Assign the values
  4. post increment/ decrement

let x=1 y=2 

then by,

y=y+1; → y = 2 + 1 = 3 ;

z=x+y; → z = 1 + 3 = 4 ;

x=x+1; → x = 1 + 1 = 2 ;

after operations x=2,y=3,z=4………………….….,,,,….(1)

option B : x=1,y=2,z=3;

z = (x++) + (++y);

z = 1 + 3

z = 4

after assignment of value in z 

the value of y = 3 , x= 2  ,z=4  ………………………………...(2)

from eqn (1) & (2),

x,y,z values  are same

 

Answer:
Position:
Show:

Related questions

10 10 votes
9 answers 9 answers
12.8k
12.8k views
pooja14 asked Jun 22, 2016
12,766 views
What is the output of the following C program? #include<stdio.h #define SQR(x) (x*x) int main() { int a; int b=4; a=SQR(b+2); printf("%d\n",a); return 0; }14361820
8 8 votes
3 answers 3 answers
10.4k
10.4k views
Sourabh Kumar asked Jun 22, 2016
10,381 views
How many lines of output does the following C code produce?#include<stdio.h float i=2.0; float j=1.0; float sum = 0.0; main() { while (i/j 0.001) { j+=j; sum=sum+(i/j); ...
9 9 votes
8 answers 8 answers
14.3k
14.3k views
ajit asked Sep 2, 2015
14,349 views
What is the output of the following C program?#include<stdio.h void main(void){ int shifty; shifty=0570; shifty=shifty>>4; shifty=shifty<<6; printf("The value of shifty i...
4 4 votes
2 answers 2 answers
6.5k
6.5k views
ajit asked Sep 23, 2015
6,524 views
Which of the following is true with respect to Reference?A reference can never be NULLA reference needs an explicit dereferencing mechanismA reference can be reassigned a...