This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Closed 7 years ago.
The following code gives a weird output. I have tried it on multiple compilers and ended up with the same answer. It will process the statement right to left but print the output left to right however c++ statements are evaulated left to right in general. Can someone explain why this happens when we overload the cout statement.
Output:
15
10
5
However the Output if processed from left to right should be:
8
10
12
#include<iostream>
using namespace std;
int main(){
int a = 5, b = 3, c = 2;
cout<< (a = b + a) << endl << (b = c + a) << endl << (c = b + c);
return 0;
}
This will result in unspecified behavior. The order in which the operations execute isn't specified (since there are no sequence points between them), so the three assignments
a = b + a
b = c + a
c = b + c
can occur in any order. Therefore the output from cout is also unspecified .
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
So I'm trying to print a table of characters with their corresponding integer values. Below is my code:-
#include <iostream>
using namespace std;
int main()
{
char i = 'a';
while (i <= 'z')
{
cout << i << '\t' << 'a' + 1 << '\n';
++i;
}
}
I tried to put char('a' + 1) in the code which gave me a whole lot of wrong answers.
Output(correct output) I get by using static_cast in code:-
a 97
b 98
c 99
....
z 122
Output I'm getting using the above written code:-
a 98
b 98
.....
z 98
So i would like to know what is the difference between char('a'+1) and 'a' + 1 and static_cast(i)?
The type of expression 'a' + 1 is int.
Note that there is no char operator+(char, char), but there is int operator+(int, int). This is why a gets promoted to int and there result of the expression is int. See Integral promotion for more details:
In particular, arithmetic operators do not accept types smaller than int as arguments, and integral promotions are automatically applied after lvalue-to-rvalue conversion, if applicable. This conversion always preserves the value.
Whereas char('a' + 1) is char because it explicitly casts int 'a' + 1 to char.
Please post the code, which does compile.
Let's go through it step by step:
char i = a
You are trying to initialize the variable i with the variable a. The same problem in while (i < z), where you try to compare the variable i with the variable z.
In addition you missed the header <iostream>.
So the correct code would be
#include <iostream>
using namespace std;
int main()
{
char i = 'a';
while (i < 'z')
{
cout << i << '\t' << 'a' + 1 << '\n'; //how can i use char('a'+1)
++i;
}
}
if you want to display 'a' + 1 as a character, you have to explicitly convert it to char, since 'a' + 1 would evaluate to int: char('a' + 1).
I think, you are looking for char(i + 1) though.
You may also convert it to a for-loop:
int main() {
for (char i = 'a'; i < 'z'; ++i) {
cout << i << '\t' << char(i + 1) << '\n';
}
}
There are a few compilation errors in your code. You should understand the difference between char i = a and char i = 'a'.
However, coming to the actual question, I believe what you want to print can be achieved as:
#include <iostream>
using namespace std;
int main()
{
char i = 'a';
while (i < 'z')
{
cout << i << '\t' << static_cast<int>(i) << '\n';
++i;
}
}
If I understand correctly, you wanted to print a table with the alphabet in the first column and the corresponding ASCII value in the second column.
Since you got most of it down, converting the alphabet to its int value is achieved using static_cast<int>(i), which is known as type casting.
A good reading to understand the difference types of casts in C++ and the difference between the C and C++ casts is here.
This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Closed 6 years ago.
int i = 7, j = 3;
int *a = &i, *b = &j;
cout << (*a = *b) << ", " << *(*(&a));
Can someone please explain why is the output 3, 7?
Your code can be simplified:
int i = 7, j = 3;
cout << (i = j) << ' ' << i;
Here the variable i is accessed and changed in the same statement. Since the order of evaluation of different parts of the same statement is not specified in the C++ standard, compiler may calculate them in any order, and the result may be different on different compilers (or even different versions of the same compiler, or different runs of the same compiler on the same source code, or even different runs of the same compiled program).
Don't write code that changes and accesses something in one statement.
This question already has answers here:
Cannot understand for loop with two variables [duplicate]
(3 answers)
Closed 6 years ago.
This compiles and runs, but produces garbage values for "a". Why doesn't "a" increment like "b"? Why is it producing garbage?
for(a,b=0; a,b != 55; a,b++)
{
//outputs garbage
std::cout << "a = " << a << std::endl;
//outputs expected results
std::cout << "b = " << b << std::endl;
}
The comma operator says execute the expression on the left then execute the expression on the right:
a, b=0
first executes a which does nothing, then it executes b=0 which assigns zero to b.
Why does the comma operator exist? The comma operator can be useful when the expressions have side effects.
It also serves a sequence point which tell the compiler "everything on the left must be complete before anything on the right happens. This constrains the optimizations allowed by the compiler, so for example a += 1, b = a + c[a] will always add one to a before using it as an index. Something like b = ++a + c[a] is undefined because the compiler can increment a before or after it uses it as an index.
This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Closed 7 years ago.
Why "n*n" results as 4 at the first instant of the loop? to me it should be 1*1. instead it comes as 2*2.
Please give me a simple answer as i'm still a beginner :)
#include <iostream>
using namespace std;
int main(){
int n =1 , *p;
p = &n;
char aString[] = {"student"};
for (int i = 0; i<5; i++)
cout<< "i = "<< i << "n*n = "<<n*n<< "n++ = "<< n++<< " *p "<<endl;
system ("pause");
return 0;
}
http://ideone.com/nWugmm
The evaluation order of elements in an expression is unspecified, except some very particular cases, such as the && and || etc.
writing:
cout<< "i = "<< i << "n*n = "<<n*n<< "n++ = "<< n++<< " *p "<<endl;
you suppose an order and in particulr that n++ is the last evaluated.
To solve this problem you could split the exression in two parts:
cout<< "i = "<< i << "n*n = "<<n*n<< "n++ = "<< n<< " *p "<<endl;
n++;
Order of evaluation is not specified, it's not left to right as you may think and it's not right to left.
Split the code like Daniele suggested if your code relies on order.
And compile your code with high warning level, the compiler can help you spot this.
This question already has answers here:
Undefined behavior and sequence points
(5 answers)
pre and post increment operations on a variable give different output on TC and gcc [duplicate]
(5 answers)
Closed 9 years ago.
int a = 0;
cout << a << a+1 << a << 1+a << a;
// output is 01010 , ok fine i understand it! :-)
cout << endl;
cout << a << ++a << a << a++ << a;
// output is 22202 ,
// Plzzz help me how compiler interprets my this statement
// i cant understand :-(
int x = 1;
cout << ++x + ++x;
// output is 6
// how ??
Please if anyone could explain it to me how these outputs are coming :-)
Thanks in Advance!
It depends on the type of x. If x is a built-in type (e.g., int) then you have undefined behavior because you're modifying x twice without an intervening sequence point1.
If x is a user-defined type, then you have unspecified behavior.
1. Technically C++11 has change the terminology so "sequence point" is no longer used, bu the effect is the same.