`for` — C Keyword

`for` — C Keyword

The for keyword in C: a counted loop with init, condition, and iteration expressions.

How to use this reference page

Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.

  • Scan the top of the page first to identify the primary types, functions, or algorithm families involved.
  • Use the nearby-page links when your question is really about a companion header, related algorithm family, or broader subsystem.
  • Validate tricky behavior with a small compileable example before relying on memory for details like invalidation, ordering, allocation, or lifetime rules.

for (C)

Executes a body repeatedly using an optional init-statement, condition, and iteration expression.

Syntax

for (init-expression; condition; iteration-expression) statement

Example

#include <stdio.h>

int main(void) {
    /* Sum 1..10 */
    int sum = 0;
    for (int i = 1; i <= 10; ++i) {
        sum += i;
    }
    printf("sum = %d\n", sum);   /* 55 */

    /* Infinite loop (all parts optional) */
    /* for (;;) { ... } */

    /* Iterate array */
    int arr[] = {10, 20, 30, 40};
    int n = (int)(sizeof arr / sizeof arr[0]);
    for (int i = 0; i < n; ++i) {
        printf("%d ", arr[i]);   /* 10 20 30 40 */
    }
    printf("\n");
    return 0;
}

Notes

Example in practice

int main() {
    // Pick one facility from this reference page.
    // Write the smallest program that exercises its main precondition,
    // complexity rule, or lifetime constraint before scaling up.
    return 0;
}