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 ..

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 ? 33 Yes 11 No

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

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 ? 42 Yes 20 No

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

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

 Is This Answer Correct ? 37 Yes 17 No

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

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 ..

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 ? 13 Yes 6 No

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

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 ? 13 Yes 10 No

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

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 ..

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

More C Code Interview Questions

How we print the table of 2 using for loop in c programing?

#include<stdio.h> main() { int a[2][2][2] = { {10,2,3,4}, {5,6,7,8} }; int *p,*q; p=&a[2][2][2]; *q=***a; printf("%d..%d",*p,*q); }

Set up procedure for generating a wire frame display of a polyhedron with the hidden edges of the object drawn with dashed lines

main() { char *cptr,c; void *vptr,v; c=10; v=0; cptr=&c; vptr=&v; printf("%c%v",c,v); }

What is wrong with the following code? int *foo() { int *s = malloc(sizeof(int)100); assert(s != NULL); return s; }

What is the match merge ? compare data step match merge with proc sql merge - how many types are there ? data step vs proc sql

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

Is the following code legal? typedef struct a aType; struct a { int x; aType *b; };

main() { char *p="hai friends",*p1; p1=p; while(*p!='\0') ++*p++; printf("%s %s",p,p1); }

#include<stdio.h> main() { struct xx { int x; struct yy { char s; struct xx *p; }; struct yy *q; }; }

All the combinations of prime numbers whose sum gives 32

Give a oneline C expression to test whether a number is a power of 2?

Categories