Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this expression mean, and why does it compile? [duplicate]

Tags:

c

visual-c++

After a typo, the following expression (simplified) compiled and executed:

if((1 == 2) || 0 (-4 > 2))
  printf("Hello");

of course, the 0 shouldn't be there.

Why does it compile, and what does the expression mean?

The original (simplified) should look like this:

if((1 == 2) || (-4 > 2))
  printf("Hello");

none of this does compile:

if((1 == 2) || true (-4 > 2))
  printf("Hello");

if((1 == 2) || 1 (-4 > 2))
  printf("Hello");

if((1 == 2) || null (-4 > 2))
  printf("Hello");
like image 958
huebe Avatar asked Jul 25 '13 12:07

huebe


2 Answers

It looks like this is a Visual C++ extension to support a particular 'no function defined' idiom. From the warning C4353 page:

// C4353.cpp
// compile with: /W1
void MyPrintf(void){};
#define X 0
#if X
   #define DBPRINT MyPrint
#else
   #define DBPRINT 0   // C4353 expected
#endif
int main(){
    DBPRINT();
}

the intention being that DBPRINT is a no-op. The warning suggests #define DBPRINT __noop instead, using VC's __noop extension instead.

If you view the assembly listing for your output you'll see the second clause is omitted, even in debug mode.

like image 88
Rup Avatar answered Oct 13 '22 01:10

Rup


Guess it was interpreted as

if((1 == 2) || NULL (-4 > 2))
  printf("Hello");

where NULL is a function-pointer, by default returning int... What at actually happens in runtime is platform-dependent

like image 41
Lorinczy Zsigmond Avatar answered Oct 13 '22 03:10

Lorinczy Zsigmond