Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

issue with the conditional operator [duplicate]

#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

like image 750
user2416871 Avatar asked May 04 '26 14:05

user2416871


1 Answers

This is a gcc extension.

x ? : y

is equivalent to

x ? x : y

See here for detail.

like image 82
Yu Hao Avatar answered May 06 '26 04:05

Yu Hao



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!