`else` — C Keyword

`else` — C Keyword

The else keyword in C: provides the alternative branch of an if statement.

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.

else (C)

Provides the alternative branch of an if statement, executed when the condition is zero (false).

Syntax

if (condition) statement1 else statement2
if (condition) statement1 else if (cond2) statement2 else statement3

Example

#include <stdio.h>

int classify(int n) {
    if (n > 0)       return  1;
    else if (n < 0)  return -1;
    else             return  0;
}

int main(void) {
    printf("%d\n", classify(5));    //  1
    printf("%d\n", classify(-3));   // -1
    printf("%d\n", classify(0));    //  0
    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;
}