This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Closed 9 years ago.
Why does this code shows the output as "3 2" instead of "2 3" ?
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<string>
#include<map>
#include<vector>
using namespace std;
int main()
{
int i=2;// declare
printf("%d %d\n",i++,i++);//print
return 0;
}
Output is : "3 2"
Why it prints in reverse order
In this statement, the expression
"printf(...)" modifies the variable "i" more than once
without an intervening sequence point.
This behavior
is undefined.
The compiler has detect a case where
the same variable has been modified more than once in
an expression without a sequence point between the
modifications. Because what modification will occur
last is not defined, this expression might produce
different results on different platforms.
Rewrite the expression so that each
variable is modified only once.
even you might get output "2 3" in different compiler
The order of evaluation of printf is from right to left in here
First evaluate
printf("%d %d\n",i++,i++);
^
Then
printf("%d %d\n",i++,i++);
^
So you got the output as 3 2
The behaviour will be definitely undefined due to the undefined evaluation order of parameters.
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 read only to determine the value to be stored.
Related
This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Closed 4 years ago.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v{1,2,3,4,5};
int i = 0;
while (i < 5) {
cout << i+v[i++] << endl;
}
return 0;
}
Why the output of the program is "2,4,6,8,10", instead of "1,3,5,7,9"?
This is undefined behaviour because the read of the "first" i and the other i++ are unsequenced. The output is meaningless.
Further reading: Undefined behavior and sequence points,
Some compilers will warn you that you are doing something that leads to undefined behavior: Relying on the evaluation order of unsequenced expressions.
Example:
<source>:22:22: warning: unsequenced modification and access to 'i' [-Wunsequenced]
cout << i+v[i++] << endl;
The evaluation order of expressions between sequence points is not defined. The only sequence point in the expression i+v[i++] is at the end of the expression, so the compiler is free to order the addition of i and the increment of i however it wants.
This question already has answers here:
How do I use arrays in C++?
(5 answers)
Closed 7 years ago.
I saw the following line of code in C++. I have trouble understanding it. I hope that I can get some help here.
// Example program
#include <iostream>
#include <string>
using namespace std;
int main()
{
int a[] = {1,2,3,4};
cout<<*a+1<<;
cout<<a[1];
}
Question:
I don't understand how *a+1 works. It seems pretty unintuitive - are we adding 1 to the array here?
To understand the expression *a+1 you should consider the operator priority:
the dereference * has higher priority, so a (the name of an array is also the pointer to the first element) is dereferenced, giving the value 1
Then, 1 will be added to the value.
operator priorities are describe here http://en.cppreference.com/w/cpp/language/operator_precedence
In the expression *a + 1, the array a is evaluated in pointer context, so it points to the first value in the array.
Next, *a dereferences that pointer (meaning it gets the value the pointer points to), and that gives you the first element in the array which is 1.
Finally, 1 is added to that value and printed, so it outputs 2.
First of all this statement
cout<<*a+1<<;
^^^^
is syntactically wrong.
I think you mean something like
cout << *a+1 << endl;
Expression *a is equivalent to a[0]. So you could write instead
cout << a[0] + 1 << endl;
Take into account that semantically a[0] + 1 is not the same as a[1] though applied to your example the both expressions will yield the same result.:)
This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Closed 7 years ago.
I have here an equation i can't understand how c++ process this. Can someone explain this operation?
code:
#include <stdio.h>
main(){
int a[10] = {0,1,2,3,4,5,6,7,8,9};
int i = 0;
int num = a[i+++a[++i]]+a[++i+i++];
printf("\nnum1: %d i: %d,num,i);
}
why is the answer num = 9 while index i is just equal to 4;
Using ++ twice in the same expression on the same variable is explicitly undefined by all versions of both the C and C++ standards, and so i does not necessarily equal 4. It could be anything at the whim of the compiler writer.
Never do this. Never use ++ and -- twice in the same expression. There is no way to make any statement about what the resultant value will be, and no experience with what it does with one compiler will mean anything with respect to what another compiler does.
This question already has answers here:
Why does flowing off the end of a non-void function without returning a value not produce a compiler error?
(11 answers)
Closed 8 years ago.
I have observed that if I don’t return any value from an empty function with an int return type is 1. But in the below case it is showing 4 3 2 as o/p(is this the value of the static variable si getting printed here? if I print si I will get o/p as 2 3 4, in the reverse of what I get now. is there something to do with the function's stack push and pop here in this case?). Also I observed that if I use float as return type then it prints nan nan nan as o/p. Is this behavior compiler dependant (I have tried with both gcc and devcpp, observed the same)? What is actually going on here? Please share your thoughts on this.
#include<iostream>
using namespace std;
int f(int i){
static int si = i;
si = si + i;
///return si;
}
int main(){
cout<<f(1)<<" "<<f(1)<<" "<<f(1);
//cout<<" "<<f(1); //if I uncomment this line then the o/p is: 4 3 2 5, it looks like it's printing the value of si.
}
It looks like the behavior of cout causing the reverse printing of static variable si's value?
It is undefined behaviour. You have to return something from a non-void function*
From § 6.6.3 The return statement [stmt.return]
Flowing off the end of a function is equivalent to a return with no
value; this results in undefined behavior in a value-returning
function.
* The main() function has an implicit return 0 so it is not necessary to explicitly return. This is a special case.
This question already has answers here:
cout << order of call to functions it prints?
(3 answers)
Closed 9 years ago.
I have following simple program that initialize values for three variables and then gives output as expression.
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
volatile int a = 10, b = 20, c = 30;
cout << a+b+c << " " << (c=c*2) << " "<< (b =b*2);
getch();
return 0;
}
Output I am getting for above code is
110 60 40
But a=10,b=20 and c=30 so a+b+c should be 10+20+30 = 60
This is because the arguments to the function are processed from right to left but are printed from left to right.
In C++, the order of evaluation of function arguments is undefined. That is, in the statement
std::cout << a+b+c << " " << (c=c*2) << " "<< (b =b*2);
you get different results depending on which subexpressions are evaluated first. A compiler can choose to evaluate the arguments to the output operators from left to right but it is also free to evaluate them in a different order, e.g., right to left, then do the appropriate functions calls.
The output from this code is undefined.
In C++, if assigning a variable, you are only allowed to use in the same statement for purposes of calculating the new value. Any other use has undefined effect.
(Note, you evaluate c for the purposes of printing (the 1st print clause), and for the purposes of calculating a new c (the c=c*2).
The later use is sanctioned, the former isn't.
Most compilers will calculate the first use of c as either the value before OR the value after the assignment, but in fact they arent even obliged to have it evaluate to anything related. And even if related, may not be a value it ever logically held, eg if the assignment were (c=2*c+5), you could just as easily find this mapped to c*=2, c+=5, and the first print clause might get the intermediate state, rather than the starting or end state.
The same problem exists for b. Compilers cant even be assume to be consistent in their handling of this, since what they do may reasonably depend on register allocation, which depends on local code.