Skip to content
Laviyx Learn

Try

C course menu

C Syntax and Program Structure: Statements, Blocks, Keywords and Identifiers

Learn the basic rules of C syntax: the structure of a C program, statements and semicolons, blocks, case sensitivity, keywords, identifiers and naming rules, whitespace, and how compilation works from preprocessing to linking.

Beginner5 min readUpdated
On this page

C has a small, regular syntax. This lesson covers the rules every program follows and what happens when you compile one.

The structure of a C program#

C
#include <stdio.h>          /* 1. preprocessor directives */

#define TAX_RATE 0.18       /* 2. macros and constants */

double with_tax(double p);  /* 3. function declarations */

int main(void) {            /* 4. the main function */
    double price = 500.0;
    printf("%.2f\n", with_tax(price));
    return 0;
}

double with_tax(double p) { /* 5. function definitions */
    return p * (1 + TAX_RATE);
}
Output
590.00

Real programs follow this order: includes, constants and types, declarations, main, then the other functions (or the other functions before main, which avoids the separate declarations).

Statements and semicolons#

Each statement ends with a semicolon ;. Several statements can share a line, but one per line is much easier to read:

C
int a = 5; int b = 7;      /* legal, but harder to read */
int sum = a + b;
printf("%d\n", sum);
Output
12

Blocks#

Curly braces { } group statements into a block, used by functions, if, loops and more:

C
int marks = 82;
if (marks >= 40) {
    printf("Passed\n");
    printf("Well done!\n");
}
Output
Passed
Well done!

Case sensitivity#

C is case-sensitive: total, Total and TOTAL are three different names, and Printf is not printf.

Keywords#

Keywords have special meaning and cannot be used as names. C has a few dozen, including:

auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while

C99, C11 and C23 added a few more, such as inline, _Bool and (in C23) bool, true and false as keywords.

Identifiers#

Names of variables, functions and types:

  • may contain letters, digits and underscores;
  • must not start with a digit;
  • must not be a keyword.
ValidInvalid
total, item_count, _tmp, x22x, item-count, int, my total

Common styles in C are snake_case for variables and functions (item_count) and UPPER_CASE for macros and constants (MAX_SIZE). Avoid names starting with an underscore followed by a capital letter; they are reserved for the compiler and standard library.

Whitespace#

Spaces, tabs and new lines between tokens are ignored by the compiler. Indent code inside each block consistently (four spaces is common) so its structure is clear.

From source code to program#

gcc hello.c -o hello actually runs four steps:

  1. Preprocessing: handles #include, #define and #if, producing pure C code.
  2. Compiling: translates the C code into assembly for your processor.
  3. Assembling: turns the assembly into an object file (.o).
  4. Linking: combines object files and libraries (such as the one containing printf) into the final executable.

Errors can come from any step. "Undefined reference to ..." is a linker error: a function was declared but its code was not found. You will meet multi-file programs in Header Files and Multi-file Programs.

Practice#

Practice exercise

Which of these names are valid C identifiers? price2, 2price, unit_price, unit-price, float, _count, Total. Then write a program that declares three valid ones and prints their sum.

Show solution

Valid: price2, unit_price, _count, Total. Invalid: 2price (starts with a digit), unit-price (hyphen), float (keyword).

C
#include <stdio.h>

int main(void) {
    int price2 = 40;
    int unit_price = 25;
    int Total = 10;
    printf("%d\n", price2 + unit_price + Total);
    return 0;
}
Output
75

Key takeaway#

Finished this lesson?