Syntax Basics#

Declaration vs. Definition#

In C, declaration and definition are distinct compiler operations governing name resolution and memory allocation.

Declaration#

A declaration informs the compiler of the name and type of an entity without allocating storage.

  • Introduces an identifier to the compiler so it can perform type checking.
  • An entity can be declared multiple times across translation units.
extern int x;    // Variable x is defined elsewhere; no storage allocated here
int Func(int a); // Function prototype declaration; no code generated

Definition#

A definition provides the actual storage (for variables) or executable code (for functions).

  • Every definition is inherently a declaration, but it also allocates memory or defines implementation.
  • An entity must have exactly one definition (One Definition Rule).
int x;          // Allocates storage for integer x
int arr[1000];  // Allocates storage for an array of 1000 integers

int Func(int a) {
    return a * a; // Allocates executable instructions
}

Expression vs. Statement#

Expression#

An expression is the smallest unit of computation that evaluates to a value (or void).

  • Generates a value or performs a state-modifying side effect.
  • Can be nested inside other expressions.
3 + 4   // Evaluates to 7
x = 5   // Assignment expression; evaluates to 5 and sets x
++i     // Increments i and evaluates to the new value

Statement#

A statement is an execution unit that specifies an action and controls program flow.

  • Statements do not possess a value; they are executed for their effects.
  • Most statements end with a semicolon (;).

Categories of Statements#

  • Expression Statement: An expression followed by a semicolon, executed for its side effects:
    x = 5;
  • Selection Statement: Conditional branching:
    if (x > 0) { ... }
    switch (x) { ... }
  • Iteration Statement: Loop structures:
    while (x > 0) { ... }
    for (int i = 0; i < 10; ++i) { ... }
  • Jump Statement: Unconditional flow redirection:
    return 0;
    break;
    continue;
    goto label;
  • Compound Statement (Block): A sequence of statements enclosed in curly braces {}.
    • A block itself is grammatically treated as a single statement.
    • In syntax definitions such as while (condition) statement, the statement can be a block { ... } because a compound statement is a valid statement.