Lesson 10 +10 XP

C Syntax

C Syntax

Every C program shares the same skeleton. Once you know it, you can read almost any beginner program.

The classic full program

#include <stdio.h>

int main() {
    printf("Hello World!");
    return 0;
}

The building blocks

  • #include <stdio.h> - a "header" line that pulls in the library for input and output.
  • int main() - the entry point. The computer starts executing here.
  • { } - curly braces wrap code into a block.
  • printf("Hello World!"); - an output statement that prints text.
  • return 0; - an ending signal meaning "all done, successfully".

Two rules to remember

  • Statements end with a semicolon ;. Almost every line of C that does work finishes with one.
  • Code groups into blocks with { }.

Why this order?

The program reads top-down: pull in tools, open main, run statements, say goodbye with return 0;.

TL;DR

  • Skeleton: header, main(), a block, return 0;.
  • #include loads tools; main is where execution starts.
  • { } groups a block of statements.
  • Statements finish with a semicolon ;.