C for
last modified November 14, 2026
The for statement repeats a block of statements a known number of
times or, more generally, for as long as a condition holds. All the bookkeeping
of the loop fits in its header: the starting value, the test, and the update.
This tutorial covers the mechanics of the statement, the cases where parts of
the header are omitted, iteration over arrays and strings, nested loops, and the
scope of a counter declared in the header.
The for statement
The header has three parts separated by semicolons: an initialization, a condition, and an update. The body is the statement or block that follows.
for (init; condition; update) {
statement(s);
}
The initialization runs once, before the first test. The condition is evaluated before every iteration, including the first one; if it is false, the loop ends without running the body. After each iteration the update runs, and then the condition is tested again. Any of the three parts may be empty, and each of them may contain several expressions separated by commas.
Counting up
The first example prints the numbers 0 through 9. The counter is declared in the header, started at zero, tested against 10, and incremented after each iteration.
#include <stdio.h>
int main(void) {
for (int i = 0; i < 10; i++) {
printf("%d\n", i);
}
return 0;
}
The loop runs ten times, for the values 0 to 9. When i becomes 10,
the condition i < 10 is false and the body is not entered again.
The i++ update runs after the ninth iteration, which is why the
last printed value is 9 rather than 10.
$ gcc -std=c17 -Wall -Wextra -o count_up count_up.c $ ./count_up 0 1 2 3 4 5 6 7 8 9
Counting down
The same loop can run backwards. Here the counter starts at 9, the condition tests for a value greater than or equal to zero, and the update decrements the counter.
#include <stdio.h>
int main(void) {
for (int i = 9; i >= 0; i--) {
printf("%d\n", i);
}
return 0;
}
The loop stops after printing 0, because the next test evaluates
-1 >= 0, which is false. With an unsigned counter the same test
would be dangerous: 0 - 1 would wrap around to the largest value of
the type and the loop would never end.
$ gcc -std=c17 -Wall -Wextra -o count_down count_down.c $ ./count_down 9 8 7 6 5 4 3 2 1 0
Omitting the parts
Any part of the header may be left empty, and leaving all three produces an
endless loop. The next program multiplies a number by three until it passes 50
and leaves through break, since the header no longer contains a
condition.
#include <stdio.h>
int main(void) {
int n = 1;
for (;;) {
n *= 3;
if (n > 50) {
break;
}
}
printf("n = %d\n", n);
return 0;
}
The sequence is 3, 9, 27, 81, so the loop ends when n reaches 81.
A missing condition is the same as a condition that is always true, and omitting
the initialization or the update is useful when the counter already exists or is
changed inside the body. The empty header is still written with two semicolons:
for (;;).
$ gcc -std=c17 -Wall -Wextra -o omit_parts omit_parts.c $ ./omit_parts n = 81
Multiple counters
The initialization and the update may each hold several expressions separated by commas. The next example declares two counters of the same type and advances both of them.
#include <stdio.h>
int main(void) {
for (int i = 0, j = 0; i <= 10 && j <= 10; i++, j++) {
printf("%d\n", i * j);
}
return 0;
}
The declaration int i = 0, j = 0 gives both variables the same type,
and the update uses the comma operator, which evaluates i++, then
j++, and discards both results. Because the counters move together,
the printed values are the squares from 0 to 100.
$ gcc -std=c17 -Wall -Wextra -o multi_init multi_init.c $ ./multi_init 0 1 4 9 16 25 36 49 64 81 100
Iterating over an array
A for loop is the usual way to visit every element of an array. The
length is computed from the array itself, so the loop adapts when the initializer
changes.
#include <stdio.h>
int main(void) {
int vals[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
size_t length = sizeof vals / sizeof vals[0];
int sum = 0;
for (size_t i = 0; i < length; i++) {
sum += vals[i];
}
printf("The sum of values is: %d\n", sum);
return 0;
}
sizeof vals is the size of the whole array in bytes, and
sizeof vals[0] is the size of one element, so the division gives
the number of elements. The index is a size_t, the unsigned type
that sizeof returns, which avoids a signed-to-unsigned comparison
warning. The trick works only where the array itself is visible; inside a
function that receives a pointer, sizeof returns the size of the
pointer instead.
$ gcc -std=c17 -Wall -Wextra -o array_sum array_sum.c $ ./array_sum The sum of values is: 55
Iterating over a string
A string in C is an array of characters ending with a null character, so the
same kind of loop walks through its characters. The length comes from
strlen here, which counts the characters before the terminator.
#include <stdio.h>
#include <string.h>
int main(void) {
const char *word = "loop";
for (size_t i = 0; i < strlen(word); i++) {
printf("%c\n", word[i]);
}
return 0;
}
The index type is again size_t, the return type of
strlen. Calling strlen in the condition recomputes the
length on every iteration, which does not matter for four characters; for a long
string, store the length in a variable before the loop, or stop at the
terminator with word[i] != '\0'.
$ gcc -std=c17 -Wall -Wextra -o string_chars string_chars.c $ ./string_chars l o o p
Nested loops
One for loop can contain another. The inner loop completes all of
its iterations for each single iteration of the outer loop, which is how a
two-dimensional array is usually printed.
#include <stdio.h>
enum { ROWS = 3, COLS = 5 };
int main(void) {
int vals[ROWS][COLS] = {
{1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15}
};
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
printf("%3d", vals[i][j]);
}
printf("\n");
}
return 0;
}
The outer counter i selects a row and the inner counter
j selects a column. The dimensions come from enumeration constants,
which are integer constant expressions and therefore valid array sizes. A
const int declared inside a function would not work here: in C it
is a read-only variable, not a constant expression, so the array would become a
variable-length array, and C17 does not allow a variable-length array to have an
initializer.
$ gcc -std=c17 -Wall -Wextra -o nested nested.c $ ./nested 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Scope of the loop variable
Since C99 the counter may be declared in the initialization part, and its scope is then the loop itself. The name does not exist before the loop or after it.
#include <stdio.h>
int main(void) {
for (int i = 0; i < 3; i++) {
printf("%d ", i);
}
printf("\n%d\n", i);
return 0;
}
The loop itself is fine, but the printf after it refers to
i outside its scope, so the file does not compile. Declare the
counter before the loop when its final value is needed afterwards. The same rule
applies to a counter declared inside the body of the loop: it is recreated, and
reinitialized, on every iteration.
$ gcc -std=c17 -Wall -Wextra -o scope scope.c
scope.c: In function ‘main’:
scope.c:10:22: error: ‘i’ undeclared (first use in this function)
10 | printf("\n%d\n", i);
| ^
scope.c:10:22: note: each undeclared identifier is reported only once for each function it appears in
Source
cppreference: for statement
cppreference: statements
GCC: warning options
This tutorial covered the for statement in C: the three parts of
the header and their order of execution, loops that count up and down, empty
parts, multiple counters, iteration over arrays and strings, nested loops, and
the scope of a counter declared in the header. To leave a loop early, see the
C break tutorial; to skip a single iteration, see
C continue. The C
switch statement is the other way to choose between several branches.
Author
List all C tutorials.