How post-increment & pre-increment both are evaluated in function argument?
While surfing internet i came to following scenario. The behavior of bellow’s function i can not understand .Can you know why this output comes ?
#include<stdio.h> int main() { int a=5; printf("%d %d %d",a++,a++,++a); return 0; }
the output of this program is like
In LINUX
7 6 8
You may like to read this also....
3 Comments to “How post-increment & pre-increment both are evaluated in function argument?”
Post comment
Search in this website
our sponsors
latest comments
- Motasim billah on List of all standard version of c language
- kamal9211 on Top/Best C/C++ IDE for Windows & Linux
- kamlesh khot on Structure of a C-Program in Memory | How Heap,Stack,Data and Code segments are stored in memory?
- neha on List of all standard version of c language
- vamsi on List of all standard version of c language
Find us on Facebook
Top Authors
Find us on stackoverflow
Polls
Loading ...
My Bookmarks
- Audio/video Recorder & player application based on MATLAB
- check dependency of your binary
- defination of all standard c programming language function
- Great Question-Answer on c programming
- know what your c code means
- Limition of c programming language
- List of all version of c programming language
- Online c compiler
- php freelancing work
- some more stuff on C programming language
- Volatile Keyword in Embedded System
- Write Android application in c language
compile it with gcc -Wall filename.c
compiler will give warning as its undefined behaviour. for operation on a.
output of this program will differ on different system ..!!
but how differ?? what is the logic behind this on Linux
As per c standard
“Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored.”
There is a sequence point before evaluating the arguments to a function, and a sequence point after all the arguments have been evaluated (but the function not yet called). Between those two (i.e., while the arguments are being evaluated) there is not a sequence point (unless an argument is an expression includes one internally, such as using the && || or , operator).
That means the call to printf is reading the prior value both to determine the value being stored (i.e., the a++) and to determine the value of the second argument (i.e., the a++) and also 3rd argument (i.e the ++a). This clearly violates the requirement quoted above, resulting in undefined behavior.