How does the ternary operator C interpret an empty 1st branch? What for?

It seems to compile and run without warning using gcc -Wall(4.9.2)

#include <stdio.h>
int main(int argc, char **argv){
    printf("%d\n", argc-1?:42);
    return 0;
}

If I run it

  • with 0 args (doing argc-1for evaluation false), it prints 42;
  • with n> = 1 args (doing argc-1for evaluation true), it prints n-1.

Am I right to assume that x?:yin this case it is equivalent x?1:y? Is this the standard expected behavior or just a GCC quirk?

+4
source share
1 answer

gcc .

 argc-1?:42

(argc-1)?(argc-1):42

( )

" , ( ), . . ."

+5

All Articles