Program output

S

Sharwan Joram

Hello Everyone,
I need help in understanding of how the output is calculated in this program.

#include<stdio.h>
int main(){
int a=5;
printf("%d %d %d",a++,a++,++a);
return 0;
}

Output : 7 6 8

Regards,
Joram
 
G

Guest

Hello Everyone,
I need help in understanding of how the output is calculated in this program.

#include<stdio.h>
int main(){
int a=5;
printf("%d %d %d",a++,a++,++a);
return 0;
}

Output : 7 6 8

your program has undefined behaviour. in other words your program is incorrect and the compiler can generate any code it likes.

In particular using and modifying the same variable (eg. "a") multiple times is not defined by the language standard.

See the comp.lang.c FAQ for more information http://c-faq.com/
FAQ 3.1 "Why doesn't this code: a = i++; work?"

then read the other FAQs int he same section. then read the whole FAQ (perhaps not all in one hit!)


Happy Programming.
 
J

James Kuyper

Hello Everyone,
I need help in understanding of how the output is calculated in this program.

No, you don't.
#include<stdio.h>
int main(){
int a=5;
printf("%d %d %d",a++,a++,++a);
return 0;
}

That expression changes the value of "a" three times without any
intervening sequence points. The behavior of such code is undefined.
What you do need is to learn that you should never write such code.
 
K

Keith Thompson

Sharwan Joram said:
Hello Everyone,
I need help in understanding of how the output is calculated in this program.

#include<stdio.h>
int main(){
int a=5;
printf("%d %d %d",a++,a++,++a);
return 0;
}

Output : 7 6 8

Apart from the undefined behavior that results from modifying a
multiple times without an intervening sequence point, you should
print a newline at the end of your output, and "int main()" should be
"int main(void)" (the latter is a relatively minor point).

But the real point is that whatever the program is intended to
accomplish, there is certainly a clearer and more reliable way to
express it. (This is true even for languages in which the behavior
is well defined.)
 
S

Sharwan Joram

Apart from the undefined behavior that results from modifying a
multiple times without an intervening sequence point, you should
print a newline at the end of your output, and "int main()" should be
"int main(void)" (the latter is a relatively minor point).

But the real point is that whatever the program is intended to
accomplish, there is certainly a clearer and more reliable way to
express it. (This is true even for languages in which the behavior
is well defined.)

--
Keith Thompson (The_Other_Keith) (e-mail address removed) <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Thanks a lot everyone . I am clear on this now.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top