This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
is i=i++ truly a undefined behavior?
i just want too explain ++ and -- to my students and show them some code about them in visual studio 2010
I just test this code on it
int main(){
int a=3;
int b=3;
a=a++;
cout<<a<<endl;
cout<<b++<<endl;
}
I expect that both of cout print 3 but the first cout print 4!!!!
I test it in g++ and both of couts print 3...
what's wrong???
The behavior of a=a++ is undefined. If you'd like to increment a, use a++ instead.
http://www.slideshare.net/olvemaudal/deep-c
Read about sequence points.
a=a++; is not well defined. Don't use it.
You are only allowed do one assignment within one sequence point in C++ IIRC. So this is undefined. The following presentation discusses this issue in deep http://www.slideshare.net/olvemaudal/deep-c .
Related
This question already has answers here:
When were the 'and' and 'or' alternative tokens introduced in C++?
(8 answers)
Closed 5 years ago.
Is the word and equivalent to the && operator?
if (inner > 10 and !id)
{
std::cout << "idle" << std::endl;
}
This code was originally translated from Python.
I was sure that the 'if' line would result in a compilation error. But it does pass.
Visual studio (2015) marks it as an error, but it does compile with g++ (and also on this site https://www.onlinegdb.com/online_c++_compiler) and seems to run as expected.
Is this correct syntax or did I miss something?
Yes, according to:
https://en.wikipedia.org/wiki/Digraphs_and_trigraphs#C++
and is equivalent to && ... another issue is that it is not really widely used and it is one character longer than && ... and we know that C++ programmers try to optimize everything. Even the length of their source code, so don't expect to find it widespread in production code.
This question already has answers here:
Multiple preincrement operations on a variable in C++(C ?)
(2 answers)
Closed 6 years ago.
I have some code with lines which increment a counter.
++ count;
Sometimes I have an if condition which means I should increment count by 2.
count += 2;
Does "double increment'ing" work in the same way?
++ ++ count;
It would be helpful to know if both C and C++ compilers interpret this the same way.
As this is clearly syntactically correct, the question that remains is: "Is this UB because of unsequenced writes?"
It is not (in C++11 and later) because
5) The side effect of the built-in pre-increment and pre-decrement operators is sequenced before its value computation (implicit rule due to definition as compound assignment)
(From here)
So the code is fine as of C++11.
However, the sequencing rules were different before that, and pre-C++11, the code actually has UB.
In C, that code does not even compile.
The fact that the behavior is different between C and C++ and even between different C++ standards and that this question arises in the first place is a hint that the simple count += 2; is the safer and more readable version. You should prefer it over the "cute and clever" ++ ++count;.
There are two methods. One that obviously works, one where you have to ask a question on StackOverflow. There are comments saying "in C++ 11 or later"... How sure are you that your C++ code runs with C++ 11 and not something older? Even if you use extensions that were not part of an earlier language, your language could be C++0x with some extensions.
Clearly you shouldn't care whether the second method works or not, but use the one that obviously works.
This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Closed 9 years ago.
int i=c++ + c++;
Where c is also a integer and has a value of 5.
I thought the answer for this one is 12 or 11! But as it turns out it is 10.
Can anyone explain?
Thanks in advance..
It seems to be undefined behavior. See this post for further information:
Undefined behavior and sequence points
This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
C++ weird usage of conditional operator (>?=)
C extension: <? and >? operators
When i read a C++ code,i see the following lines:
void add(double v) { min <?= v; max >?= v; StatFig::add(v); }
What does the
>?=
means?
Thanks.
Answered right here: Link.
As explained by the great answer there which i in no way take credit for, it's a deprecated GCC extension, and in no way standard C++. Avoid.
There is a list of operators in C++ on wikipedia, but it's not in here, so maybe it's defined elsewhere in the code you read.
My guess would be that's it's an assignement operator that checks if the value is lower/higher than a certain threshold, and if so assigns that threshold to the variable. Or something
In other words, min <?= v might be equivalent to min=((min<v)?min:v), but that's just a guess.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
C extension: <? and >? operators
Take a look at the top answer (by sclo) to problem D of this Google Code Jam. It's C++ code, it must have compiled, and it contains statements such as this one:
double& ret = F[mask][cur][b];
if(j==cur) {
ret<?=f(tmp,j,b||bad[i])+M[cur][i]; // WTF is <?= ???
}
This doesn't compile in my Visual Studio 2008. What does the <?= mean?
It's a gcc extension: C extension: <? and >? operators
Recent manuals say:
The G++ minimum and maximum operators (‘<?’ and ‘>?’) and their compound forms (‘<?=’) and ‘>?=’) have been deprecated and are now removed from G++. Code using these operators should be modified to use std::min and std::max instead...
It's simply not valid C++. < Might be less than, an open angle bracket for a template argument list, or the start of a digraph however non of those can be followed by ?, then =.
It's a now deprecated g++ extension to the c++ language.
a <? b
is the minimum, returning the smaller of the numeric values a and b;
a >? b
is the maximum, returning the larger of the numeric values a and b.
There are also compound versions
<?=
and
>?=
that do assignment as well.
It doesn't compile also with GCC, and I've never heard of an operator <?=.
Anyway I would hazard a guess that a<?=b could have a semantics like: a = (a<b) ? b : a, but again, this is just a guess.