This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 7 years ago.
Explain the output
. Program has been compiled on gnu (gcc 4.9.2 compiler)
#include <stdio.h>
main()
{
int i=1;
int arr[]={2,3,4,5};
int x=(i++<3);
printf("%d %d %d %d %d",i,x,++i[arr],i++[arr],i);
}
output :3 1 6 4 3
What you have here is a "side effect". Depending on the underlying architecture, the parameters to printf() are evaluated from left-to-right or from right-to-left (f.e. HPUX).
So you cannot say, what is "THE" result. You can only specify the result on your operating system with your compiling chain.
This behaviour is called Undefined behavior and sequence points
Related
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Explain the order of evaluation in printf [duplicate]
(5 answers)
Parameter evaluation order before a function calling in C
(7 answers)
Closed 4 years ago.
If C Compiler works from left to right then why is the following code's output in a way as if it is executing from right to left?
int x = 15 ;
printf("%d %d %d", x!=15, x=20, x<30);
Output :
1 20 1
First of all, those are expressions and not statements.
Secondly, the order of argument evaluation is unspecified. You can't tell in what order the arguments will be evaluated, and your code would lead to undefined behavior.
For more information about evaluation order and sequencing see e.g. this reference.
This question already has answers here:
Order of operations for pre-increment and post-increment in a function argument? [duplicate]
(4 answers)
Undefined Behavior of Postfix or Prefix Increment in Function Calls in C [duplicate]
(4 answers)
Is the output of printf ("%d %d", c++, c); also undefined?
(6 answers)
Closed 7 years ago.
print(int*a,int*b,int*c,int*d,int*e){
printf("\n%d %d %d %d %d %d",*a,*b,*c,*d,*e);
}
main(){
static int arr[]={97,98,99,100,101,102,103,104};
int *ptr=arr+1;
print(++ptr,ptr--,ptr,ptr++,++ptr);
return 1;
}
O/P: 100 100 100 99 100 (some -ve values) on GCC & turbo C
According to my understanding, the ptr points to 98 first, then ++ptr is passed which makes it to point 99.How in the world it prints 100? Even then, it does not decrements the ptr till 3rd argument and prints it again.What's going on?
Whow ! Your print function has 6 %d, and you only pass 5 int values => undefined behaviour
You are modifying more than once ptr in a single call (*) => undefined behaviour
I would say that the compiler can do anything without you can say it is wrong ...
(*) the order of the operations in a single instruction is undefined (except some explicit cases) : 6.5 ยง2-3 : Between the previous and next sequence point an object shall have its stored value
modified at most once by the evaluation of an expression. and Except as specified
later (for the function-call (), &&, ||, ?:, and comma operators), the order of evaluation
of subexpressions and the order in which side effects take place are both unspecified.
The above code will result multiple warnings which may lead to undefined behaviour in multiple systems
print(++ptr,ptr--,ptr,ptr++,++ptr);
multiple unsequenced modifications to 'ptr'
[-Wunsequenced]
because in my compiler it is giving output as below :
99 99 98 98 100 0
This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Closed 8 years ago.
Here is a code
#include<stdio.h>
int main()
{
int i=5;
printf("%d%d",++i,++i);
}
I am not understand, why the output is 77?
++i and i++ are expressions that have side effects. Using two of these in the same expression results in undefined behavior. Basically, anything goes.
Specifically, I'm guessing the compiler says you want to increment i twice and then use the result, so it evaluates ++i twice, resulting in 7, and then sends that to printf.
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Postfix and prefix operators as function arguments - why is this happening? [duplicate]
(3 answers)
Closed 9 years ago.
When I compile following code it gives different output in different environment.
int a=4;
a = ++a + ++a;
printf("%d",a);
compiling this in Dev-C++ gives 12 while in xcode LLVM compiler it gives 11 as output.
Also when I compile following code
int a=4;
a = ++a + ++a + ++a;
printf("%d",a);
it gives 19 in Dev-C++ and 18 in xcode LLVM compiler.
Can anyone explain me more about this?
The following code:
a = ++a + ++a;
and
a = ++a + ++a + ++a;
are both examples of Undefined Behaviour, so the result is dependent on compiler, platform etc.
Please look at "The C Programming Language" by K&R, Section 2.12
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 5 years ago.
I came across a very interesting fact while executing the following a simple c program:
#include<stdio.h>
int main( )
{
int k=0;
printf("%d%d%d", k++, k, ++k);
return 0;
}
in windows it was showing output as: 1 1 1
but in linux(ubuntu) it was showing as: 1 2 2
why is it so?
It's undefined behaviour. When there are no / ambiguous sequence points. See this wikipedia article:
http://en.wikipedia.org/wiki/Sequence_point
There are two problems. First is that the order in which the expressions k++, k, and ++k in the printf call are evaluated is not specified; the compiler is free to evaluate them in any order it sees fit. Second is that an object may not have its stored value updated more than once between sequence points by the evaluation of an expression. Both k++ and ++k attempt to update the value stored at k, and there is no sequence point between those expressions, so the behavior is undefined; any result is permitted.
The standard does not specify an ordering in which the arguments of a routine are evaluated.
Writing code that depends on the ordering is not portable.