What does this expression mean and why does it compile?

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

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

Of course, 0 should not be.

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 compiles:

 if((1 == 2) || true (-4 > 2)) printf("Hello"); if((1 == 2) || 1 (-4 > 2)) printf("Hello"); if((1 == 2) || null (-4 > 2)) printf("Hello"); 
+51
c visual-c ++
Jul 25 '13 at 12:48
source share
6 answers

In fact, this is Microsoft .

For debugging purposes, you can use __noop intrinsic, it indicates that the function and parameters will not be evaluated.

In your case, the Microsoft compiler thinks that you are trying to use 0 to do the same thing, why it works, but, for example, on VS2012 it gives a warning:

 warning C4353: nonstandard extension used: constant 0 as function expression. Use '__noop' function intrinsic instead. 

See this for more information: http://msdn.microsoft.com/en-us/library/2a68558f(v=vs.71).aspx

+8
Jul 25 '13 at 12:58
source share

This seems to be a Visual C ++ extension to support a specific idiom without a function. On warning page C4353 :

 // 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(); } 

DBPRINT is DBPRINT to be non-op. The warning suggests instead of #define DBPRINT __noop , use the VC __ noop extension instead .

If you look at the assembly list for your output, you will see that the second sentence is omitted even in debug mode.

+26
Jul 25 '13 at 12:56 on
source share

Suppose this has been interpreted as

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

where NULL is a pointer to a function, it returns int by default ... What actually happens at runtime is platform dependent

+14
Jul 25 '13 at 12:53 on
source share

Visual Studio 2012 gives the following warning:

warning C4353: non-standard extension used: constant 0 as an expression of a function. Use the __noop function instead

this is a non-standard way to insert an β€œno operation” assembler statement at this point in evaluating an expression

+9
Jul 25 '13 at 12:55
source share

Ubuntu displays error

 int main() { if((1 == 2) || 0 (-4 > 2)) printf("Hello"); } 

o / r

 niew1.c:3:19: error: called object Γ’0Γ’ is not a function 
+1
Jul 25 '13 at 12:53 on
source share

Probably 0 cast to a function pointer here. A clear tide might look like this:

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

However, I have no clue as to which function 0 is implicitly executed in your example.

0
Jul 25 '13 at 12:55
source share



All Articles