Precedence Order in C/C++ [duplicate] - c

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 8 years ago.
I wish to know that why this code shows different Output on different compilers.
Here's the Code:-
int a = 5;
printf("%d %d",a++,++a);
Output:- 6 7 (Visual Studio)
Output:- 6 6 (CodeBlocks)

This is undefined behavior and it depends on the compiler which you are using. And to add it has nothing to do with the order of precedence.
You may check Precedence and Order of Evaluation
Also from C99 standard:
The order of evaluation of the operands is unspecified. If an attempt
is made to modify the result of an assignment operator or to access it
after the next sequence point, the behavior is undefined.

Related

C language order of precedence [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 6 years ago.
a = 5;
c = (b =a+2) - (a=1);
In book c programming a modern approach by kn king it is written that the effect of executing the second statement will result in 6 or 2 as it is undefined behavior of c but in other books like c by Dennis it is written that it will be executed from left to right. Which one is correct?
In the above case,
c=(b=a+2) -(a=1);
the value of a is being changed and being read without a sequence point in between, so it is undefined behavior.
Quoting C11, Annex §J.2, Undefined behavior
A side effect on a scalar object is unsequenced relative to either a different side effect
on the same scalar object or a value computation using the value of the same scalar
object.
Also related, from chapter §6.5
The grouping of operators and operands is indicated by the syntax.85) Except as specified
later, side effects and value computations of subexpressions are unsequenced.86)
So, there's no guarantee which subexpression will get evaluated first.

The result of (a=a+1)+(a=a+1)+(a=a+1) in c? [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 6 years ago.
When I compile this code with gcc and run
int a=1;
printf("%d",(a=a+1)+(a=a+1)+(a=a+1));
I expect the result to be 2+3+4=9, but the output is 10.
I know that there is undefined behavior in (++a)+(++a)+(++a) , because the three ++ side effect can be run before all (++a) is evaluated.
But I think the value of a=a+1 is exactly what a is after the assignment is evaluated. So the compiler cannot process three a=a+1 first and use the value in variable a as the value of a=a+1 after all a=a+1 evaluated.
I want to know where is wrong according to the c standard.
The assignment operator is not a sequence point. Assuming a is just an identifier (e.g. not a macro argument that might expand to something more complicated) and does not have an atomic type (C11), a=a+1 is identical in semantics to ++a.
I wanted to mark this question as a duplicate of one of the hundreds of ++ operator sequence-point questions, but it seems your question is about why this form isn't different.

gcc optimization in pre-increment & post-increment [duplicate]

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.

Parameter evaluation sequence in printf [duplicate]

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;
}

Expected output for this code [duplicate]

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.

Resources