This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
I am confused between True and False .. does a True value stands for Non-Zero and False value stands for Zero? [duplicate]
(4 answers)
Closed 9 years ago.
Recently I faced an issue understanding the behaviour for printf() function.
This is what I was working with
#include<stdio.h>
int main(){
int a=5;
printf("%d %d %d",a++,a++,++a);
return 0;
}
When I ran this code snippet on gcc (linux) I got output as 7 6 8.
But while running it on turbo (windows) I got output as 7 6 6.
What I understood is in turbo the parameters are passed in right to left order.
Can anyone explain how it works in linux using gcc.
Your code contains several modifications of the same variable without any sequence points between the modifications. Thus, the code is incorrect, and results are unpredictable.
Also, order of evaluation of function parameters is implementation-defined.
Different compilers may give different results in this situation. The question is not only about printf but also about parameter evaluation sequence.
What is an implementation defined behaviour?:
A language standard defines the semantics of the language constructs. When standard doesn't include the specifications of what to do in some case. Compiler designers may chose the path they think is correct. So, these constructs become implementation defined.
Since, that is not defined in standard, it's called undefined behaviour.
Unfortunately, these questions were given blindly given by many instructors in exams by merely testing in a compiler to set the question.
Example:
What is the output of following statement? But options don't include
undefined behaviour
#include<stdio.h>
int main(){
int a=5;
printf("%d %d %d",a++,a++,++a);
return 0;
}
Related
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 2 years ago.
Why n++==--n always equal to 1? The following code gives output as 1.
#include <stdio.h>
int main(){
int n=10;
printf("%d\n",n++==--n);
}
The output is always 1, no matter what n is.
If compiled with gcc -Wall, the following warning is obtained:
a.c: In function ‘main’:
a.c:4:20: warning: operation on ‘n’ may be undefined [-Wsequence-point]
printf("%d\n",n++==--n);
~^~
There is a good explanation in the gcc manpage about this, the section which begins:
-Wsequence-point
Warn about code that may have undefined semantics because of violations of sequence
point rules in the C and C++ standards.
[...etc...]
which is well worth a read, because it discusses the issues in more detail, although the basic point is that the result depends on the ordering of operations whose ordering is not fully constrained by the standards, and is therefore undefined.
I probably can't reproduce it in full here without also adding a lot of licence information in order to comply with the GFDL. There is a copy at https://gcc.gnu.org/onlinedocs/gcc-3.3.6/gcc/Warning-Options.html
(Take-home message: always compile your code with compiler warnings switched on, but especially if you are seeing possibly unexpected behaviour.)
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 3 years ago.
CODE
#include <stdio.h>
int main()
{
int a =1;
printf("%d%d%d%d\n",a,++a,++a,a);
a=1;
printf("%d%d%d%d\n",a,a++,++a,a);
a=1;
printf("%d%d%d\n",a,++a,a++);
return 0;
}
Output
3333
3233
331
This is "undefined behavior". You cannot rely on the order of evaluation of function arguments in C. When we say "undefined behavior" we mean that anything might happen: it might work on one compiler, it might not work on another compiler; it might work on one compiler with optimizations disabled, and not work on the same compiler with optimizations enabled; it might not work at all; it might work flawlessly; it might dump core; it might send sperm whales and bowls of petunias falling from the sky.
(See https://www.quora.com/What-is-the-passage-on-the-whale-and-the-bowl-of-petunias-about)
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 9 years ago.
I want to know on what criterias,gcc compiler decides to optimize the values of variables.?
Here is sample
int a=2;
printf("%d %d\n",a++,++a);
It gives output
3 4
Why does gcc optimizes and gives latest value of ain pre-increment and not in post increment?On which basis it takes decision?
It's undefined behavior. There is no specified order in which arguments are evaluated.
The code has two problems.
You change the value of a twice in the same expression, with no so-called "sequence point" between them. This is undefined behavior and anything can happen. See the FAQ for more information.
You have side effects in the parameters passed to a function, the side effect being a ++ increment. The order of evaluation of function parameters is unspecified behavior, meaning that the compiler has implemented it in some way, but we can't know how. It may be different from function to function, and certainly different from compiler to compiler.
One should never write code that relies on undefined or unspecified behavior. Even more info in the FAQ.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…)
I have heard that printf function executes from right to left and prints from left to right. By that convention, the following program should give 7,6,5. But it's giving 7,7,7 on my linux gcc compiler. Is it compiler dependent?
int i=5;
printf("%d %d %d\n",++i,++i,i);
Also, can we use cdecl/pascal keyword to change the order of execution of printf? If yes, how do we do that? I have been trying to do this but without success. Thanx in advance!
The evaluation order in your code is undefined, as there are no sequence points. You cannot relay on the evaluation order of function arguments, and you cannot change it either.
Check http://www2.research.att.com/~bs/bs_faq2.html#evaluation-order
There is no order dictated by the standard in function calls, so the arguments can be evaluated in any order the compiler seems fit. So if you have side effects in the evaluation of the parameters, you get undefined behavior.
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 5 years ago.
int a=5;
printf("%d %d %d\n",a++,a++,++a);
Output on Gcc :
7 6 8
Can someone please explain the answer.
I apologize if this question has been repeated but i wasn't able to find it.
Thanks!!
The behaviour is undefined because there are no sequence points between the increment operators.
Explaining why the code does what it does is a pointless exercise. You should not write code that has undefined behaviour, even if it appears to work for you.
To address the point raised in the comments: It is true that the comma operator acts as a sequence point, however the comma here is not a comma operator. From Wikipedia:
The use of the comma token as an operator is distinct from its use in function calls and definitions, variable declarations, enum declarations, and similar constructs, where it acts as a separator.