This question already has answers here:
What is the correct answer for cout << a++ << a;?
(4 answers)
Undefined behavior and sequence points
(5 answers)
Closed 8 years ago.
I Cant really undersdand suffix. I know it first uses identifier and then increases or decreses , as first shows i and then ++. But now i think im wrong and still don't understand it.
#include <iostream>
using namespace std;
int main()
{
int i = 0;
cout << i << i++ << i;
cout << "\n\n\nPress Enter to close the window . . . ";
cin.clear();
cin.sync();
cin.get();
return 0;
}
Output:
101
Press Enter to close the window . . .
first i is changed before increment readed.Why?
I expected
001
Press Enter to close the window . . .
Can someone explain.
Just never do such a thing, it is undefined
cout << i << i++ << i;
better do
cout << i << i << (i + 1);
i ++;
if you want your expected result.
The case
cout << i++;
is defined and perfectly ok.
I think what is undefined*) here is the order of evaluation of function arguments. What you are actually calling here are function calls to the (overloaded)
std::ostream& operator<< (std::ostream&, int);
and the first argument is the output of another call to the same function, so your
cout << i << i++ << i;
expands to
operator<<( operator<<( operator<<(cout,i), i++), i);
As the order in which function arguments are evaluated is not specified, anything can happen here. You can avoid that by writing separate lines:
cout << i;
cout << i++;
cout << i;
which expands into the harmless
operator<<(cout,i);
operator<<(cout,i++);
operator<<(cout,i);
*) edit: to be more precise, the cout<<i<<i++; is undefined because the order of evaluation of function arguments in unspecified.
Related
This question already has answers here:
How to test whether stringstream operator>> has parsed a bad type and skip it
(5 answers)
How to check if cin is int in c++? [duplicate]
(2 answers)
Closed 29 days ago.
So for a short introduction, im learning c++ and my question is kinda basic, i have been given a simple c++ question to check wether an integer is a positive, negative or zero but there is a fourth condition and its an error or unknown value just in case symbols or characters have been used in the input
#include <iostream>
using namespace std;
int main()
{
int num;
cout << "Enter an integer: " << '\n';
cin >> num;
if (num > 0)
cout << "The number is positive" << '\n';
else if (num < 0)
cout << "The number is negative" << '\n';
else if (num == 0)
cout << "Zero" << '\n';
else
cout << "Error" << '\n';
return 0;
}
As you can see above, everything works fine but until one thing that while when i used a character or any other symbol it reads it as a zero
i know its kinda silly question but i spent hours and nothing worked out, i tried removing the num==0 statement and it worked fine but i also wanted that 0 statement too, any help provided would be appreciated
This question already has answers here:
Undefined, unspecified and implementation-defined behavior
(9 answers)
Closed 23 days ago.
It seems that member function clear() of string does remove its content, but the removed contents still can be accessed by operator[] . Here's the example that makes me confused.
#include <iostream>
using namespace std;
int main()
{
string input = "Weird";
cout << "Your Input: " << input << "\n";
input.clear();
cout << "Your Input: " << input << "\n";
cout << "Your Input: " << input[0] << input[1] << input[2] << input[3] << input[4] << '\n';
return 0;
}
The results are:
Your Input: Weird
Your Input:
Your Input: eird
Why this is happenning? If example above is normal, what should I do to completely remove its content? (accessing by input[1] should be '\000')
Accessing elements of a string after calling the method clear invokes undefined behavior.
It seems in your case the class std::string uses its internal buffer defined within the class itself for short strings.
After the call of clear the class just set the first character of the buffer with the terminating zero character '\0;.
To check that string was cleared just output its length as for example
std::cout << input.length() << '\n';
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.
This question already has answers here:
Delay execution 1 second
(4 answers)
Closed 8 years ago.
I need a timer that does X every second.
I made this, however it doesn't print anything until the program is terminated, I find that weird.
It prints everything after three seconds if you put three as the counter, and 100 if you chose that.
How do make it print every second and not all at once at termination?
int main()
{
using namespace std;
//Number to count down from
int counter = 10;
//When changed, a second has passed
int second = (unsigned)time(NULL);
//If not equal to each other, counter is printed
int second_timer = second;
while (counter > 0) {
second = (unsigned)time(NULL);
while (second != second_timer) {
//Do something
cout << counter-- << ", ";
//New value is assigned to match the current second
second_timer = second;
}
}
cout << "0" << endl;
return 0;
}
Add << flush where you want to flush. I.e. change your printout to:
cout << counter-- << ", " << flush;
endl causes the buffer to 'flush' and be written out to stdout. You can add << endl; to your cout << counter--, manually flush the cout stream using cout.flush();, or append << flush; to the end of your cout expression (thanks #Rob!)
For more info, the answer to this question seems to go into more detail.