Ok #C programmers...

Going through the W3Schools C programming course. I've gotten to the point that gave me pause nearly thirty years ago when reading through the original K&R book:

What's up with the ternary operator? It's kinda fugly. Why use:
( isDay ) ? printf("G'day!\n") : printf("Evening!\n");
when you can just:
( isDay ) && printf("G'day!\n") || printf("Evening!\n");

Doesn't that just make a lot more sense, or is that just my shell and Perl habits peeking through?

Merci. ;)

Follow

@RL_Dane As a PHP developer, I'm pretty used to the ternary operator, and even to the null-coalescing operator (??), which is not ternary, but it's pretty similar.

In normal use, the ternary operator is less verbose than an if/else statement. That said, I don't remember ever seeing, or needing, it in C.

Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.