1)
int i=5;
j=i++ + i++ + i++;
printf("%d",j);This code gives the answer 15.But if we
replace the value of the j then anser is different?why?

2)int i=5;
printf("%d",i++ + i++ + i++);
this givs 18.

Answers were Sorted based on User's Feedback



1) int i=5; j=i++ + i++ + i++; printf("%d",j);This code gives the answer 15.But if we ..

Answer / venu

i don't know the answer.
but here is the proble.
int i=5;
j=++i + ++i + ++i;
printf("%d",j);This code gives the answer 22.

How and What is the process?

Is This Answer Correct ?    62 Yes 32 No

1) int i=5; j=i++ + i++ + i++; printf("%d",j);This code gives the answer 15.But if we ..

Answer / raghu

In the first case
initially j=15 is assigned that is (5+5+5).then i gets incremented thrice.if we try to print i then i will be
8 (5 is incremented thrice).

In the second case
as we know that printf gets evaluated 4m right to left
that is pf("%d", 5++ + 5++ +5++);
that means 4m right to left 7+6+5=18
firstly i's value is 5 then it gets incremented 6 then 7.

Is This Answer Correct ?    42 Yes 14 No

1) int i=5; j=i++ + i++ + i++; printf("%d",j);This code gives the answer 15.But if we ..

Answer / gireesh

I have tried the second case in linux with gcc. But, the
output is 15 and not 18.

Is This Answer Correct ?    40 Yes 21 No

1) int i=5; j=i++ + i++ + i++; printf("%d",j);This code gives the answer 15.But if we ..

Answer / divakar

in the 1st case post increment will be done at the end of
the statement that is 15 is assigned to 'j' then 'i' will
increment 3 times bcoz 3 post increments r there.
in the later case first 15 assign to 'i' then increment
3 times and assign to 'i'

Is This Answer Correct ?    26 Yes 18 No

1) int i=5; j=i++ + i++ + i++; printf("%d",j);This code gives the answer 15.But if we ..

Answer / prasoon

both the codes possess undefined behaviour because the value
of i is changing more than once between two sequence points...

hence my answer is undefined behaviour

Is This Answer Correct ?    14 Yes 6 No

1) int i=5; j=i++ + i++ + i++; printf("%d",j);This code gives the answer 15.But if we ..

Answer / satyasaran

Remember increment operator is compiler depended.
1.
if u compiled in TC++ then u will get j=18
if u compile in VC++ then u will get j=15.
2.
if u compiled in TC++ then u will get j=18
if u compile in VC++ then u will get j=15.

Is This Answer Correct ?    14 Yes 11 No

1) int i=5; j=i++ + i++ + i++; printf("%d",j);This code gives the answer 15.But if we ..

Answer / amey

in the 1st case post increment will be done at the end of
the statement that is 15 is assigned to 'j' then 'i' will
increment 3 times bcoz 3 post increments r there.
in the later case first 15 assign to 'i' then increment
3 times and assign to 'i'

Is This Answer Correct ?    5 Yes 4 No

1) int i=5; j=i++ + i++ + i++; printf("%d",j);This code gives the answer 15.But if we ..

Answer / keerthan

Here, in first case, i=5, j=(i++)+ (i++) + (i++)............ i is added three times without incrementing because it is post incremented so... 5 is added with 5 and again with 5 answer is 15.
where as in second case... execution from right to left when it is done... initially i=5, first i++ this value is 5 then incremented so that 6 is added to 5... now is incremented again so 7 now 7+6+5 =18.

Is This Answer Correct ?    0 Yes 1 No

Post New Answer

More C Code Interview Questions

main ( ) { static char *s[ ] = {“black”, “white”, “yellow”, “violet”}; char **ptr[ ] = {s+3, s+2, s+1, s}, ***p; p = ptr; **++p; printf(“%s”,*--*++p + 3); }

1 Answers  


main() { register int a=2; printf("Address of a = %d",&a); printf("Value of a = %d",a); }

3 Answers  


#define SQR(x) x * x main() { printf("%d", 225/SQR(15)); } a. 1 b. 225 c. 15 d. none of the above

3 Answers   HCL,


main() { float me = 1.1; double you = 1.1; if(me==you) printf("I love U"); else printf("I hate U"); }

1 Answers  


main(int argc, char *argv[]) { (main && argc) ? main(argc-1, NULL) : return 0; } a. Runtime error. b. Compile error. Illegal syntax c. Gets into Infinite loop d. None of the above

4 Answers   HCL, LG,






#define f(g,g2) g##g2 main() { int var12=100; printf("%d",f(var,12)); }

3 Answers  


#define clrscr() 100 main() { clrscr(); printf("%d\n",clrscr()); }

2 Answers  


char *someFun() { char *temp = “string constant"; return temp; } int main() { puts(someFun()); }

1 Answers  


posted by surbhi just now main() { float a = 5.375; char *p; int i; p=(char*)&a; for(i=0;i<=3;i++) printf("%02x",(unsigned char) p[i]); } how is the output of this program is :: 0000ac40 please let me know y this output has come

2 Answers   GATE,


main() { int *ptr=(int*)malloc(sizeof(int)); *ptr=4; printf("%d",(*ptr)+++*ptr++); }

3 Answers   GNITC,


void main() { unsigned giveit=-1; int gotit; printf("%u ",++giveit); printf("%u \n",gotit=--giveit); }

1 Answers  


Write a program that find and print how many odd numbers in a binary tree

1 Answers  


Categories