C · Beginner

C Syntax and Your First Program

8 min readUpdated September 24, 2026Every example verified

In short: A C program is a text file of functions, and execution starts in int main(void). Every statement ends with a semicolon, braces group statements into blocks, #include <stdio.h> makes printf and scanf available, and the file must be compiled (for example with gcc -std=c11) into an executable before it can run.

What a C program is made of

C is a compiled language. You write source code in a .c file, a compiler such as gcc translates it into machine code, and the result is a standalone executable that the operating system runs directly. Nothing interprets your code line by line while it runs, which is why C programs are fast and why a typo stops you at compile time rather than halfway through a run.

A source file is mostly a list of functions. The one the operating system calls is main, written int main(void): it returns an int to the operating system (0 means success, anything else signals a problem) and void says it takes no parameters. The body of main sits between { and }. When the body finishes, or a return statement runs, the program ends.

Inside a body you write statements, and each one ends with a semicolon. The compiler ignores line breaks and indentation, so the semicolon is what separates statements; indentation is for the humans reading the file. Braces group several statements into a block, which is how if, loops and functions know where their bodies start and stop.

Lines that begin with # are preprocessor directives, handled before compilation proper. #include <stdio.h> pastes in the declarations of the standard input and output functions, so the compiler knows what printf and scanf look like. Without it, the compiler has no idea what printf is.

printf writes text to standard output. It prints exactly what you give it and nothing more: there is no automatic line break, so you write \n wherever a line should end. Placeholders such as %d (an int) and %f (a double) are replaced by the arguments that follow the string. scanf is the mirror image: it reads from standard input according to a format and stores the result in the variable whose address you pass with &.

Comments are ignored by the compiler. /* ... */ can span lines; // runs to the end of the line and has been standard since C99. C is case sensitive, so Printf and printf are different names, and main must be spelled in lower case.

Syntax

 C · syntax
#include <stdio.h>          /* declarations for printf and scanf */

int main(void)              /* execution starts here */
{
    statement;              /* each statement ends with ; */
    printf("text\n");       /* \n ends the line */
    return 0;               /* 0 tells the operating system all went well */
}

Everything between the braces of main runs top to bottom. Blank lines and indentation are for readers; the compiler only cares about semicolons and braces.

Anatomy of the skeleton

PieceRole
#include <stdio.h>Brings in the declarations of printf, scanf and the other standard I/O functions
int main(void)The function the operating system calls; returns an int status
{ ... }A block: the statements that belong to main
printf("...")Writes formatted text to standard output
return 0;Ends main and reports success
/* ... */ and // ...Comments, ignored by the compiler

Printing several lines

Four calls to printf produce three lines; notice that the second line is built by two separate calls.

 C
#include <stdio.h>

int main(void)
{
    printf("Ferry timetable\n");
    printf("Harbour -> Island: ");
    printf("07:30\n");
    printf("Island -> Harbour: 18:15\n");
    return 0;
}

Output

Ferry timetable
Harbour -> Island: 07:30
Island -> Harbour: 18:15

printf prints exactly its string. The second call has no \n, so the third call continues on the same line; only the \n at the end of 07:30 moves to a new line. return 0 ends the program with a success status. Every statement, including the return, ends with a semicolon.

Reading a number and printing a result

The program reads one integer from standard input with scanf and prints two lines that use it. The input is the single line 14.

 C
#include <stdio.h>

int main(void)
{
    int loaves;
    if (scanf("%d", &loaves) != 1) {
        printf("Expected a whole number\n");
        return 1;
    }
    printf("Loaves ordered: %d\n", loaves);
    printf("Loaves to bake (2 spare): %d\n", loaves + 2);
    return 0;
}

Input given to the program: 14

Output

Loaves ordered: 14
Loaves to bake (2 spare): 16

int loaves; declares a variable. scanf("%d", &loaves) reads an integer from the input and stores it there; the & passes the variable's address so scanf can write into it. scanf returns how many values it filled, so checking for 1 catches missing or non-numeric input instead of using a variable that was never set. %d in the printf string is replaced by the value of loaves.

Layout is free, punctuation is not

This program is deliberately badly laid out. It compiles and runs exactly like a tidy one, because the compiler reads semicolons and braces, not indentation.

 C
#include <stdio.h>
int main(void){int a=4;int b=9;
printf("%d + %d = %d\n",
       a, b,
       a + b);return 0;}

Output

4 + 9 = 13

Three statements share one line and one printf call spreads over three lines; both are legal. What is not optional is the semicolon after each statement and the braces around the body. Write your own programs one statement per line with consistent indentation, not because the compiler needs it but because the next reader, usually you, does.

Common mistakes

  • Leaving out a semicolon

    Why it goes wrong: The compiler cannot tell where the statement ends and reports an error, often pointing at the line after the one with the problem, with a message such as expected ';' before 'printf'.

    Fix: Read the line above the one the compiler names. End every statement with ;.

     C · fix
    printf("Boarding\n");   /* not: printf("Boarding\n") */
  • Forgetting the newline

    Why it goes wrong: printf does not end the line for you. Two calls without \n print on the same line, and the last line of output may run straight into the shell prompt.

    Fix: End each line you want with \n.

     C · fix
    printf("Gate 3\n");
    printf("Gate 4\n");
  • Writing void main() or main() without a type

    Why it goes wrong: The C standard says main returns int. gcc accepts void main() with a warning, but the exit status becomes meaningless and some compilers reject it.

    Fix: Always write int main(void) and end with return 0;.

     C · fix
    int main(void)
    {
        return 0;
    }
  • Skipping #include <stdio.h>

    Why it goes wrong: printf is not part of the language itself; it is a library function declared in stdio.h. Without the include, gcc 13 warns about an implicit declaration and gcc 14 refuses to build the program.

    Fix: Put #include <stdio.h> at the top of any file that prints or reads.

Compiling and running from a terminal

Every exercise on this site is a complete program that reads standard input and writes standard output, exactly like the examples above. On your own machine the workflow is the same three steps every time: save the source as a .c file, compile it, run the executable. The -Wall flag asks gcc to report suspicious code, such as a variable that is used before it is set; treat those warnings as errors you have not noticed yet. Redirecting a file into the program with < is how you feed it the test input an exercise shows, so you can check your output before submitting.

 C · in practice
gcc -std=c11 -Wall -o timetable timetable.c
./timetable
./timetable < input.txt

Key points

  • Execution begins in int main(void); return 0 reports success to the operating system.
  • Every statement ends with ;; braces group statements into blocks.
  • #include <stdio.h> is needed for printf and scanf.
  • printf prints exactly its string; write \n to end a line and %d to insert an int.
  • scanf reads input into a variable whose address you pass with &, and returns the number of values it read.
  • Whitespace, indentation and comments are ignored by the compiler.
  • C is case sensitive: printf, not Printf.

Try it yourself

Complete the program so it reads one integer, the number of passengers on a 40-seat bus, and prints two lines: Passengers: N and Seats left: M, where M is 40 minus N. For the input 27 it prints Passengers: 27 and Seats left: 13.

Your program
#include <stdio.h>

int main(void)
{
    int passengers;
    /* read passengers with scanf, then print the two lines */
    return 0;
}
Input the program receives: 27
Expected output: Passengers: 27 Seats left: 13

Practise this

Exercises for this lesson are in the C practice set.

Open the C playground

Frequently asked questions

Why does main return 0 in C?

The value main returns is the program's exit status, which the operating system and shell scripts can read. By convention 0 means success and any non-zero value means something went wrong; that is why the examples return 1 when the input is missing. Since C99, reaching the end of main without a return statement counts as return 0;, but writing it out makes the intent clear.

What is the difference between int main(void) and int main()?

Both are accepted for a definition of main in C11. (void) states explicitly that the function takes no parameters. An empty () in a declaration that is not a definition means the parameter list is unspecified, so (void) is the precise form and the habit worth building. The other standard form, int main(int argc, char *argv[]), receives command-line arguments.

Do I have to compile again after every change?

Yes. The executable is a snapshot of the source at the moment you compiled it. After editing the .c file, run gcc again; otherwise you are running the old program and will chase bugs you have already fixed, or miss the ones you just introduced.

Progress is stored only in this browser.

How this page was checked. Every program on it was run with GCC 13 (C11) at build time by the publishing checks, and the output shown is what it printed. Running C inside the browser is not available yet, so the Run button is absent rather than pretending; copy the code and run it with GCC 13 (C11) locally.