`do` — C Keyword

`do` — C Keyword

The do keyword in C: a post-test loop that executes the body at least once.

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.

do (C)

Executes the body at least once, then repeats while the condition is non-zero. The condition is checked after each iteration.

Syntax

do statement while (condition);

Example

#include <stdio.h>

int main(void) {
    int n = 0;

    do {
        printf("%d ", n);
        ++n;
    } while (n < 5);
    /* Output: 0 1 2 3 4 */
    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;
}