What does >?= mean in c++? [duplicate] - c++

This question already has answers here:
C extension: <? and >? operators
(2 answers)
Closed 7 years ago.
I was looking at some code and saw something like this:
int d = 1;
int somethingbigger = 2;
d >?= somethingbigger;
cout << d << endl;
I think this should output 2. But I can't even compile this with gcc 4.5.2.
The code was written in 2005 and compiled with gcc 3.4.4 (not 100% sure).
Can someone explain how this works and why I can't compile this with a recent compiler.

This is the "maximum" assignment operator, a GCC extension.
If the extension is not enabled, then you will not be able to use this feature.
As of 4.0.1:
The G++ minimum and maximum operators (<? and >?) and their
compound forms (<?=) and >?=) have been deprecated and will be
removed in a future version. Code using these operators should be
modified to use std::min and std::max instead.
It looks like they were gone by 4.0.4.

That's not C++ code.
It's using a gnu-only extension, and is completely non-portable.
Just replace it with standard-compliant code:
if (d < somethingbigger) d = somethingbigger;

IIRC, this is the shorter version of d = max(d, somethingBigger); or
d = (d < somethingbigger) ? somethingbigger : d;
Haven't seen this in a while though and I'm pretty sure thats a GNU extension to GCC.

Related

What is x after 'x += x--'? [duplicate]

This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Closed 4 years ago.
Whats happening x after executing the code below;
int x = 10;
x += x--;
I'm expecting result 19 (adding x to x and then decrease x by 1) but result 20. How does it works behind the curtains? Thank your for your answers.
The behavior in this case was undefined before C++17, see e.g., https://en.cppreference.com/w/cpp/language/eval_order#Undefined_behavior , so if your compiler does not conform to it, it is no use testing or trying to understand it: it will depend on implementation, or even version of the compiler.
If your compiler conforms to C++17, it is guaranteed that in a simple or compound assignment (= or e.g. +=, respectively) all of the side effects of right hand side will be dealt with before evaluating the left hand side.
In your case, x-- is evaluated to be 10 accompanied by setting x=9 as its side effect, then the computer will add 10 to x=9 resulting in x=19.
Thanks to MichaƂ for his correction, which I incorporated into the answer.
Having an older c++ might probably not do the job as the behaviour is literally defined to be undefined, by the older standard. (Thanks to #LightnessRacesinOrbit)
If you just try out an online compiler which will have the latest version, it works just fine and the result is 19 as you expected (x+=x-- is the same as x= x+x--. This means for getting the new "x", it has to sum the old "x"+the old "x" -1. So it will do x+(x--), which is x=10+(9).
Try it out here:
with:
#include <iostream>
using namespace std;
int main()
{
int x = 10;
x += x--;
cout<<x<<endl;
}

Are code blocks inside parenthesis in C/C++ legal and can MSCL compile it?

I have the following code:
int x;
x = ({ 1; 2; 3; });
printf("%d\n", x); // should be 3
(If you're curious why I would ever write disgusting code like that. The answer is I'm not. I'm writing a generator that outputs C code, and having such a statement will make things a lot easier.)
The code compiles and works on Apple LLVM version 7.0.2 (with warnings for unused code of course) but fails with MSCL 10.0 and 14.0 (error C2059: syntax error: '{').
My question is: 1) is there a name for this kind of code(-abuse)? 2) Is it legal in any C/C++ standard? 3) Is there a way to get MSCL to accept it?
Don't know anything about MSCL part of the question since i've always used GCC. And in GCC:
1) this is called compound statement expression;
2) this is a non-standard GCC extension.
Instead of a compound statement expression, why not use the comma operator and just write:
x = ( 1, 2, 3 );

Unexpected compiler behavior when declaring array of variable size [duplicate]

This question already has an answer here:
Does "int size = 10;" yield a constant expression?
(1 answer)
Closed 8 years ago.
So, I was teaching base C programming to a student for a test.
Talking about array declaration, I told him:
"you can do this"
int myArray[10];
-> show him that the code compiles
"you can do this, too"
#define ARRAY_SIZE 10
[...]
int myArray[ARRAY_SIZE];
-> show him that the code compiles
"but you can't do this!"
int arraySize = 10;
int myArray[arraySize];
-> show him that the code won't compile...... but it actually compiles!
myWholeLifeIsALie.jpg
I was using DevC++ with MinGW.
Sweating, I switched on Linux and made a simple test program
#include <stdio.h>
int main()
{
int size;
int i;
scanf("%d", &size);
int array[size];
for(i = 0; i < size; i++)
array[i] = i*2;
return 0;
}
It compiles and run both with g++ and gcc.
Instead, MS Visual Studio 2010 compiler tells me that he "expected constant expression". That's what I was expecting from g++/gcc, too.
I think I'm missing something dumb here, but I can't even...
Variable length arrays (length determined by runtime value of a variable) are not supported in standard C++.
However, they are supported in standard C since 1999. Some C compilers predating 1999 and some C++ compilers support VLAs (or a similar feature) as an extension.
Some compiler products/suites (e.g. gcc) have options for selecting support of particular C or C++ standards, and there are options for how picky they are (e.g. what constructs they issue diagnostics or warnings for).
It is necessary to read your compiler documentation, to work out what standards or vendor-specific language features it supports by default, how to change those, and how it issues diagnostics.
No sir, your WholeLifeIsNotALie.jpg is also there. :-)
Yes, this is possible in C. Given you compiler supports C99, it allows something called VLA [variable length array].
I'm not much knowledgeable in c++, but if it supports officially, it should be there somewhere defined in the latest standards, otherwise the support comes as a compiler extension.
SideNote: You should always check the return value of scanf() to ensure proper input. Otherwise, you may run into strange things.

Weird usage of conditional operator (>?=) [duplicate]

This question already has answers here:
C extension: <? and >? operators
(2 answers)
Closed 7 years ago.
I was looking at some code and saw something like this:
int d = 1;
int somethingbigger = 2;
d >?= somethingbigger;
cout << d << endl;
I think this should output 2. But I can't even compile this with gcc 4.5.2.
The code was written in 2005 and compiled with gcc 3.4.4 (not 100% sure).
Can someone explain how this works and why I can't compile this with a recent compiler.
This is the "maximum" assignment operator, a GCC extension.
If the extension is not enabled, then you will not be able to use this feature.
As of 4.0.1:
The G++ minimum and maximum operators (<? and >?) and their
compound forms (<?=) and >?=) have been deprecated and will be
removed in a future version. Code using these operators should be
modified to use std::min and std::max instead.
It looks like they were gone by 4.0.4.
That's not C++ code.
It's using a gnu-only extension, and is completely non-portable.
Just replace it with standard-compliant code:
if (d < somethingbigger) d = somethingbigger;
IIRC, this is the shorter version of d = max(d, somethingBigger); or
d = (d < somethingbigger) ? somethingbigger : d;
Haven't seen this in a while though and I'm pretty sure thats a GNU extension to GCC.

Twisted C++ code [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Undefined behavior and sequence points
#include< iostream.h>
int main()
{
int i=7,j=i;
j=(i++,++i,j++*i);
cout <<j;
return 0;
}
What will be the output of the C++ code?
It's my homework that my professor gave me.
It sometimes helps to convince people who don't believe this is undefined by actually compiling the program with several compilers and observing the results:
After fixing the iostream.h error,
g++ 4.5.2 prints 64
CLang++ 2.8 prints 63
Sun C++ 5.8 prints 63
MSVC 2010 prints 64
(oh, and, re-written to use C I/O, the original K&R C compiler on Unix 7 prints 63)
[Edited to account for OP's question changing edit]:
It's undefined as to what the output will be.
There are the following errors in the code:
#include <iostream.h> should be #include <iostream>,
j is uninitialized so the value of j++*i isn't known - OK, this got fixed in the edit,
Besides, the assignment itself is improper. The convoluted line can be rewritten as:
i++;
++i;
j = j++ * i;
And the last part is invalid for the reasons described here:
Undefined behavior and sequence points
Essentially, you're incrementing i by 2, multiplying it by the original value of j, and adding one.
In the end, j=64
j = ((7+2)*7) + 1 = (9*7)+1 = 63+1 = 64
At least that's what my Visual Studio 2010 compiler does with it.