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 / gireesh

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

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

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 ?    38 Yes 18 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 ?    13 Yes 5 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 ?    13 Yes 10 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 ?    4 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

char *someFun1() { char temp[ ] = “string"; return temp; } char *someFun2() { char temp[ ] = {‘s’, ‘t’,’r’,’i’,’n’,’g’}; return temp; } int main() { puts(someFun1()); puts(someFun2()); }

2 Answers  


How to read a directory in a C program?

4 Answers  


create a login program that ask username and password. if you input username or password 3 times wrong, the program will terminate else the program will prompt a message "congratulations"

1 Answers  


#include<stdio.h> main() { register i=5; char j[]= "hello"; printf("%s %d",j,i); }

2 Answers  


main(int argc, char **argv) { printf("enter the character"); getchar(); sum(argv[1],argv[2]); } sum(num1,num2) int num1,num2; { return num1+num2; }

1 Answers  






# include <stdio.h> int one_d[]={1,2,3}; main() { int *ptr; ptr=one_d; ptr+=3; printf("%d",*ptr); }

1 Answers  


union u { struct st { int i : 4; int j : 4; int k : 4; int l; }st; int i; }u; main() { u.i = 100; printf("%d, %d, %d",u.i, u.st.i, u.st.l); } a. 4, 4, 0 b. 0, 0, 0 c. 100, 4, 0 d. 40, 4, 0

1 Answers   HCL,


void main() { if(~0 == (unsigned int)-1) printf(“You can answer this if you know how values are represented in memory”); }

1 Answers  


main() { char c=' ',x,convert(z); getc(c); if((c>='a') && (c<='z')) x=convert(c); printf("%c",x); } convert(z) { return z-32; }

1 Answers  


Write out a function that prints out all the permutations of a string. For example, abc would give you abc, acb, bac, bca, cab, cba. You can assume that all the characters will be unique.

5 Answers   Microsoft, Nike, IITR,


What is the main difference between STRUCTURE and UNION?

13 Answers   HCL,


#include<stdio.h> main() { char s[]={'a','b','c','\n','c','\0'}; char *p,*str,*str1; p=&s[3]; str=p; str1=s; printf("%d",++*p + ++*str1-32); }

2 Answers   CNSI,






Categories