Question

#include<stdio.h>
int main()
{
  printf("%d\n", 4 ?: 8);
}

According to the C standard this program is invalid because it is missing an expression between the ? and :.But The interesting thing is that there is when I compile the code it is printing 4.how come it will print 4 rather than showing any compile error

Was it helpful?

Solution

This is a gcc extension.

x ? : y

is equivalent to

x ? x : y

See here for detail.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top