what does "a<?=b" in C++ mean? [duplicate] - c++

This question already has answers here:
C extension: <? and >? operators
(2 answers)
Closed 9 years ago.
I saw this code
a<?=b; // (a and b are int)
from the solution of Google Code Jam.
but my VS shows an error on '?'
I only know the following:
a>b?a=0:b=0;
Thanks.

Old operator; it's a (since removed) gcc extension for 'minimum'. That is:
a <?= b;
is the same as:
a = a < b ? a : b;

A nonstandard GCC extension to C++ allows <? as an operator which is equivalent to min. I haven't seen <?= before, but presumably it's an in-place version; that is, a <?= b is equivalent to a = min(a,b).
Note that the GCC developers woke up the next morning and realized what a bad idea that was. The operator is now deprecated.

If a happens to be larger than b, it would set a to b.
Essentially the same as:
a = a < b ? a : b;
Example:
int a = 5;
int b = 2;
a<?=b; //a is now 2!
I wouldn't advocate actually using such a solution though, it is horrible.
As others have said, it's part of a nonstandard GCC extension, but please don't use it.

Related

Why is int a[0] allowed in c++? [duplicate]

This question already has answers here:
What happens if I define a 0-size array in C/C++?
(8 answers)
Closed 6 years ago.
I find that in the declaration of an array, we cannot specify the size of it like this
int a[0];
I know, empty size array illegal in C++, but In my code, empty size array compiler allowed and give the output.
My code is here :
#include <iostream>
using namespace std;
int main()
{
int a[0];
a[0] = 10;
a[1] = 20;
cout<<a[0]<<endl;
cout<<a[1]<<endl;
return 0;
}
Output:
10
20
Online compiler link : http://code.geeksforgeeks.org/TteOmO
So, My question is, Why is int a[0] allowed GCC compiler?
It issues a warning, for example clang outputs:
warning: zero size arrays are an extension [-Wzero-length-array]
this is undefined behaviour:
a[0] = 10;
a[1] = 20;
Zero length arrays are extensions for gcc, why - you can read on it here:
https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html
They are very useful as the last element of a structure that is really a header for a variable-length object:
This is actually C extension but it looks like it also is used in C++, probably to make it easier to use existing structures from C that uses this extension.
Please Look Into This
What happens if I define a 0-size array in C/C++?
If It's a good compiler it will catch that and give a warning.
but with pedantic option compiler can catch it.

How does C++ process this [duplicate]

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.

Some C/C++ syntax [duplicate]

This question already has answers here:
What does the comma operator , do?
(8 answers)
Closed 8 years ago.
I have done some searching, but didn't find the answer
The code:
char b = 'b';
char c = 'c';
char a[5] = "";
a[0] = b, c;
What last line means? The b, c part?
Thank you all
That uses the elusive comma operator to cause confusion.
It evaluates b, and result of that is then asssigned to a[0]. After that, c is evaluated but its value thrown away. At least this is the case in C.
The comma has lower precedence than assignment (see this handy table) which is extra confusing.

Obscure C++ syntax [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
C++ Comma Operator
About a year ago I noted some obscure syntax in a coding project I was working on:
table_value = table_index += 2, valueFromTable(table_index);
Does anyone recognise this?, it's like an assignment with an additional statement. This compiled in our entire suite of cross-platform compilers, so I'm pretty certain its valid C++ but I've never seen anything like it.
Any insight would be appreciated.
Gearoid
EDIT: heres some working code:
#include <iostream>
using namespace std ;
int valueFromTable(int a) { return a ; }
int main()
{
int table_index = 0 ;
int table_value = table_index += 2, valueFromTable(12);
cout<<table_value<<endl;
return 0 ;
}
This is the Comma operator.
It's standard C and C++ but heavily frowned upon.
It evaluates both arguments, and returns the result of the second.

What prevents C++ from being a strict superset of C? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
“C subset of C++” -> Where not ? examples ?
I'm aware that C++ is not a strict superset of C. What language features prevent C++ from being a superset of C?
The elephant in the room: the following is valid C but not valid C++.
int typename = 1;
Substitute your favorite C++ reserved word.
C++ also does not support variable-length arrays, where:
int array[n];
is valid in C, but not C++. A C++ version of the above would be:
int *array = new int[n];
...
delete [] array;
There is a special wiki entry that summarizes a lot of issues.
Simple example, consider this declaration:
int f();
This is valid C, but invalid C++: f(3, 2, -5, "wtf");
Explanation: in C, int f() is treated like int f(...) (at least at the first call site). Declare as int f(void) if you don't want f to take parameters.
One from top of my head - C++ does not support default int.