Skip to content
  • Conditionals: if, else & ?: · C

Conditionals: if, else & ?:

LearnViaNotes @learnvianotes· 2 min read

C has no boolean type in conditions — any scalar value is tested against zero: 0 (or NULL) is false, everything else is true.

if / else if / else

C
#include <stdio.h>

const char *grade(int score) {
    if (score >= 90) {
        return "A";
    } else if (score >= 75) {
        return "B";
    } else if (score >= 50) {
        return "C";
    } else {
        return "F";
    }
}

int main(void) {
    int scores[] = {95, 80, 50, 12};
    for (int i = 0; i < 4; i++)
        printf("%d -> %s\n", scores[i], grade(scores[i]));
    return 0;
}
Output
95 -> A
80 -> B
50 -> C
12 -> F

Truth in C

C
#include <stdio.h>
#include <stdbool.h>

int main(void) {
    int n = -3;
    char *p = NULL;
    if (n) printf("-3 is true\n");
    if (!p) printf("NULL is false\n");
    if (0.0) printf("never\n"); else printf("0.0 is false\n");

    bool done = 5;                  /* any non-zero converts to true (1) */
    printf("done = %d\n", done);
    return 0;
}
Output
-3 is true
NULL is false
0.0 is false
done = 1

The conditional (ternary) operator

C
#include <stdio.h>

int main(void) {
    int a = 7, b = 12;
    int max = a > b ? a : b;
    printf("max = %d, %s\n", max, a % 2 ? "a is odd" : "a is even");
    return 0;
}
Output
max = 12, a is odd

Pitfalls

BugWhat happensPrevention
if (x = 5)Assigns 5, always true-Wall warns (-Wparentheses); some write if (5 == x)
if (x > 0); { … }Empty statement — block always runsBrace style, -Wempty-body
Dangling elseelse binds to the nearest ifAlways use braces
if (0.1 + 0.2 == 0.3)False — floating roundingCompare with a tolerance
if (a < b < c)Parsed as (a < b) < c → compares 0/1 with ca < b && b < c
C — rejected: -Wparentheses under -Werror
#include <stdio.h>

int main(void) {
    int x = 0;
    if (x = 5)                 /* assignment used as condition */
        printf("always\n");
    return 0;
}

What is the dangling else problem?

Free for students and teachers

Write notes like this one

Cards for every idea, quizzes and flashcards, read aloud — on the web and your phone.

Something wrong with this note? Open it to report