Example 1 — Max and parity with ?:
Replace short if-else blocks with expressions.
#include <stdio.h>
int main(void) {
int a = 12, b = 20;
int max = (a > b) ? a : b;
for (int n = 1; n <= 3; n++) {
printf("%d is %s\n", n, n % 2 == 0 ? "even" : "odd");
}
printf("max = %d\n", max);
return 0;
} How It Works
a > bis false, somaxtakesb.- The ternary supplies the string argument to
printf. - Only the chosen branch is evaluated.
