C if & else
last modified November 14, 2026
The if statement runs a block of code only when an expression is
true, and the optional else supplies the block for the opposite
case. Chaining else if turns the pair into a multi-way decision.
This tutorial covers the three forms, the way C decides what is true, the
bool type from <stdbool.h>, and the mistakes that
if makes easy: the dangling else, assignment where a
comparison was meant, and comparisons that are chained as in mathematics.
The if statement
The condition is written in parentheses and may be any scalar expression: an integer, a character, a floating-point value or a pointer. Zero means false and every other value means true. When the condition is true, the statement or block that follows is executed; otherwise it is skipped.
#include <stdio.h>
int main(void) {
int num = 4;
if (num > 0) {
printf("The number is positive\n");
}
return 0;
}
The comparison num > 0 yields 1 when it is true and 0 when it is
false, and if tests that result. Since num is 4, the
message is printed; with a negative value the program would print nothing, which
is rarely what a user wants.
$ gcc -std=c17 -Wall -Wextra -o if_stm if_stm.c $ ./if_stm The number is positive
if else
Adding else gives the condition a second branch, so exactly one of
the two blocks always runs. The next example decides whether a number is even or
odd.
#include <stdio.h>
int main(void) {
int num = 7;
if (num % 2 == 0) {
printf("The number is even\n");
} else {
printf("The number is odd\n");
}
return 0;
}
The remainder operator % returns 0 for an even number, so the test
is true only then. Because 7 leaves a remainder of 1, the first block is skipped
and the else block runs. Note the double equals sign:
== compares, while a single = assigns.
$ gcc -std=c17 -Wall -Wextra -o if_else if_else.c $ ./if_else The number is odd
Multiple conditions with else if
An else if adds another test to the chain. The tests are evaluated
from top to bottom, and only the block after the first true condition runs; the
remaining tests are skipped. The next program classifies three different numbers
with one helper function.
#include <stdio.h>
static void classify(int n) {
if (n > 0) {
printf("%d is positive\n", n);
} else if (n == 0) {
printf("%d is zero\n", n);
} else {
printf("%d is negative\n", n);
}
}
int main(void) {
classify(-7);
classify(0);
classify(42);
return 0;
}
The order of the tests matters. If the first test were n >= 0,
zero would be reported as positive and the n == 0 branch would never
be reached. The final else catches everything left over, so the
three cases together cover every integer.
$ gcc -std=c17 -Wall -Wextra -o else_if else_if.c $ ./else_if -7 is negative 0 is zero 42 is positive
Truth values in C
C has no separate Boolean type in its if conditions: the condition
is an integer value, and the rule is simply that zero is false. A negative number,
a pointer to an object and the result of a successful comparison are all true.
The next program tests four such values.
#include <stdio.h>
int main(void) {
int zero = 0;
int minus = -1;
int *ptr = &zero;
int *null = NULL;
if (zero) {
printf("zero is true\n");
} else {
printf("zero is false\n");
}
if (minus) {
printf("minus is true\n");
} else {
printf("minus is false\n");
}
if (ptr) {
printf("ptr is true\n");
}
if (!null) {
printf("null is false\n");
}
return 0;
}
Only zero and null are false. The value -1 is true,
and so is any non-null pointer. That is why a pointer can be tested directly
instead of writing ptr != NULL, and why a null pointer is false in
a condition. Writing if (ptr) and if (ptr != NULL)
mean the same thing; the second form is clearer when the pointer is compared on
purpose.
$ gcc -std=c17 -Wall -Wextra -o truth truth.c $ ./truth zero is false minus is true ptr is true null is false
The bool type from stdbool.h
For code that stores a condition rather than testing it immediately,
<stdbool.h> provides the type bool and the
constants true and false. In C17 they are macros: the
type expands to _Bool, and the constants to 1 and 0.
#include <stdbool.h>
#include <stdio.h>
int main(void) {
bool ready = true;
if (ready) {
printf("ready\n");
}
printf("sizeof(bool) = %zu\n", sizeof(bool));
printf("true = %d, false = %d\n", true, false);
return 0;
}
_Bool is the smallest integer type that can hold 0 and 1, and its
size is one byte on the usual platforms. Assigning any scalar to a
bool stores 0 or 1, never another value, which is why the printed
constants are exactly 1 and 0. C23 turns bool,
true and false into keywords, so the header becomes
unnecessary there, but including it keeps the program valid in C17 as well.
$ gcc -std=c17 -Wall -Wextra -o bool_flag bool_flag.c $ ./bool_flag ready sizeof(bool) = 1 true = 1, false = 0
The dangling else
An else belongs to the nearest if that does not already
have one, regardless of indentation. When an inner if has no
else, the indentation can suggest the wrong owner. The next program
looks as if it prints "non-positive" only for a non-positive number, but the
message is printed for a positive one.
#include <stdio.h>
int main(void) {
int x = 5;
if (x > 0)
if (x > 10)
printf("large\n");
else
printf("non-positive\n");
return 0;
}
The else is attached to if (x > 10), the closest
unmatched if, not to the outer test. The outer condition is true, the
inner one is false, and the else runs, so a positive number is
reported as non-positive. GCC warns about the ambiguity with
-Wdangling-else, which is enabled by -Wall.
#include <stdio.h>
int main(void) {
int x = 5;
if (x > 0) {
if (x > 10) {
printf("large\n");
} else {
printf("small\n");
}
}
return 0;
}
Braces on the outer if show that the inner if and its
else form one statement, and the outer body is complete. The same
braces remove the warning. As a rule, write the braces even around a single
statement: the structure then does not depend on the reader's indentation.
$ gcc -std=c17 -Wall -Wextra -o dangling dangling.c
$ ./dangling
dangling.c: In function ‘main’:
dangling.c:7:8: warning: suggest explicit braces to avoid ambiguous ‘else’ [-Wdangling-else]
7 | if (x > 0)
| ^
non-positive
$ gcc -std=c17 -Wall -Wextra -o dangling_fixed dangling_fixed.c
$ ./dangling_fixed
small
Assignment versus comparison
A single = is an assignment, and the value of the assignment is the
value stored, so it can be used as a condition. The program below intends to test
x but overwrites it with zero instead.
#include <stdio.h>
int main(void) {
int x = 5;
if (x = 0) {
printf("assignment: non-zero\n");
} else {
printf("assignment: zero\n");
}
if (x == 0) {
printf("comparison: zero\n");
} else {
printf("comparison: non-zero\n");
}
return 0;
}
The first test assigns 0 to x and then tests the stored value, which
is false, so the else branch runs. By the time the second test is
evaluated, x really is 0. GCC reports the suspicious assignment with
-Wparentheses, which -Wall enables; the warning can be
silenced by writing the extra parentheses when an assignment is intentional, as
in if ((c = getchar()) != EOF).
$ gcc -std=c17 -Wall -Wextra -o assignment assignment.c
$ ./assignment
assignment.c: In function ‘main’:
assignment.c:7:9: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
7 | if (x = 0) {
| ^
assignment: zero
comparison: zero
Chained comparisons
Mathematics allows 0 < x < 10, but C does not read it that
way. The comparison operator is left-associative, so the expression is evaluated
as (0 < x) < 10: the first part produces 0 or 1, and that
result is then compared with 10. The next program shows the difference.
#include <stdio.h>
int main(void) {
int x = 42;
if (0 < x < 10) {
printf("chained comparison: in range\n");
} else {
printf("chained comparison: out of range\n");
}
if (x > 0 && x < 10) {
printf("logical and: in range\n");
} else {
printf("logical and: out of range\n");
}
return 0;
}
For x = 42, the first expression becomes (0 < 42) <
10, that is 1 < 10, which is true, so a number outside
the range is reported as being inside it. The second test combines two
comparisons with the logical AND operator and gives the expected answer. GCC
detects the pattern and warns twice, once about the comparison of a Boolean
expression with a constant and once about the mathematical meaning of the chain.
$ gcc -std=c17 -Wall -Wextra -o range range.c
$ ./range
range.c: In function ‘main’:
range.c:7:15: warning: comparison of constant ‘10’ with boolean expression is always true [-Wbool-compare]
7 | if (0 < x < 10) {
| ^
range.c:7:11: warning: comparisons like ‘X<=Y<=Z’ do not have their mathematical meaning [-Wparentheses]
7 | if (0 < x < 10) {
| ~~^~~
chained comparison: in range
logical and: out of range
Source
cppreference: if statement
cppreference: Boolean type
GCC: warning options
This tutorial covered conditions in C: the if statement, its
else branch and the else if chain, the rule that zero is
false, the bool type from <stdbool.h>, and the
dangling else, assignment and chained comparison mistakes. For the
multi-way alternative built on integer values, see the
C switch tutorial; loops are described in
C for and C while.
Author
List all C tutorials.