`if` — C Keyword

`if` — C Keyword

The if keyword in C: conditionally executes a statement when a condition is true.

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.

if (C)

Conditionally executes a statement or block when the condition evaluates to non-zero (true).

Syntax

if (condition) statement
if (condition) statement else statement

Example

#include <stdio.h>

int main(void) {
    int x = 10;

    if (x > 5) {
        printf("x is greater than 5\n");
    } else if (x == 5) {
        printf("x is exactly 5\n");
    } else {
        printf("x is less than 5\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;
}