Conversation

C and C++ quiz! What does the preprocessor produce for the snippet below?

A. hello world
B. hello
C. world
D. An error.

(Poll in post below.)

3
0
0

#​if (false ? 1/0ll : -1) < 0
hello
#​endif

#​if (false ? 1/0ull : -1) < 0
world
#​endif

40% hello world
18% hello
5% world
34% An error.
0
0
0
Discussion of answers
Show content

@Mara i hope it's supposed to be undefined behavior because gcc (version 13.2.1) and clang (version 16.0.6) gave me different answers. i didn't guess either of those two options

1
0
0
re: Discussion of answers
Show content

@p2502 I'm pretty sure clang is right!

1
0
0
discussion of answer
Show content

@Mara I think this is UB, since `false` isn't something the preprocessor understands (unless you include stdbool.h).

I also don't think (but may be wrong) the preprocessor is required to understand ternaries, which would mean this expands to (nonzero) tokens, and is treated as truthy.

1
0
0

@Mara Are you sure? I'm pretty confident about the first one, at least if you're compiling as C and not C++

1
0
0

@zuurr Every keyword and identifier that remains in a preprocessor condition after macro substitution is replaced by 0. So `false` will be 0. (And so will `true` in C, but not in C++.)

2
0
0

@Mara Yeah, I caught this a split second after posting (as you can see from my other comment). Tricky.

0
0
0

@Mara this prints "compiling OpenSSL..."

0
0
0

@Mara it produces a letter of resignation

0
1
0
discussion
Show content

@Mara @zuurr I see... but I'm still not able to get from there to why 0ll and 0ull behave differently revblobfoxtableflip

1
0
0

@Mara @p2502 agreed! I can file this in the GCC Bugzilla, if you don't mind?

1
0
0

@amonakov Sure, feel free to file this as a bug if you think anyone cares about this edge case. ^^'

1
0
0
re: discussion
Show content
@aburka @Mara @zuurr My guess is that it assumes both sides of the ternary operator has the same type and -1 in unsigned gets turned into the largest value in a llu which is surely not less than 0 (as in no unsigned values can be less than zero so it is probably safe to assume that always)
0
0
0