TExcelTAD
Home › C › C Syntax

C Syntax

Every C program follows the same basic shape. Once you can recognise the pieces, any C file becomes much easier to read. Let's take a small program apart line by line.

c · try it yourself
#include <stdio.h>

int main(void) {
    printf("Learning C syntax\n");
    return 0;
}
Learning C syntax
Line Meaning
#include <stdio.h> A preprocessor directive. It copies the contents of the stdio.h header into your file before compiling, which declares functions like printf.
(blank line) Ignored by the compiler. Use blank lines to make code readable.
int main(void) { Starts the definition of the main function. Code between { and } is the function body.
printf(...); A statement: an instruction that does something. It calls the printf function.
return 0; Ends main and reports success (0) to the operating system.
} Closes the body of main.

Statements and semicolons

A C program is a list of statements, and every statement ends with a semicolon (;). The semicolon is how the compiler knows one instruction has finished and the next begins. Statements run from top to bottom, in order.

c · try it yourself
#include <stdio.h>

int main(void) {
    printf("First\n");
    printf("Second\n");
    printf("Third\n");
    return 0;
}
First
Second
Third

Forgetting a semicolon is the most common beginner error. The compiler reports it, often on the next line, because that is where it noticed something was wrong:

c
printf("Hello\n")      // missing ;
return 0;              // error: expected ';' before 'return'

Whitespace and line breaks

C does not care about indentation or line breaks — only about the tokens and semicolons. These two programs are identical to the compiler:

c · try it yourself
#include <stdio.h>
int main(void){printf("Compact\n");return 0;}
Compact

Of course, nobody wants to read code like that. The convention is one statement per line, with the body of each block indented (4 spaces is common).

Tip: Pick a consistent style and let a formatter such as clang-format apply it automatically.

Case sensitivity

C is case-sensitive. main, Main and MAIN are three different names. printf must be written in lowercase; Printf would be an unknown function.

Blocks and braces

Curly braces { } group statements into a block. A function body is a block, and later you will see blocks after if, for and while. Every { needs a matching }.

c · try it yourself
#include <stdio.h>

int main(void) {
    printf("Outside the inner block\n");
    {
        printf("  Inside a nested block\n");
    }
    printf("Back outside\n");
    return 0;
}
Outside the inner block
  Inside a nested block
Back outside

About main

  • int main(void) is the standard form for a program that takes no command-line arguments. You will also see int main(int argc, char *argv[]) for programs that do.
  • Older code sometimes writes int main() — it works, but (void) states clearly that there are no parameters.
  • Since C99, if main reaches its closing brace without a return, it behaves as if it returned 0. Writing return 0; explicitly is still good practice.

Keywords

C reserves a small set of keywords that you cannot use as names. A few you will meet soon: int, char, float, double, if, else, for, while, return, void, const, struct, switch, break, sizeof.

Test yourself

What character must end every C statement?

  1. :
  2. .
  3. ;
  4. }

Is Printf("Hi"); a valid call to the standard print function?

  1. Yes, case does not matter
  2. No, C is case-sensitive and the function is printf
  3. Yes, but only on Windows
  4. Only if you include Stdio.h

What does the compiler do with indentation and extra line breaks?

  1. Uses them to group statements
  2. Ignores them
  3. Treats them as errors
  4. Converts them to semicolons