The Parts of a C Program

Let's look at our first program again, line by line:

#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}
LineWhat it means
#include <stdio.h>Bring in the standard input/output tools. printf lives here.
int main() {The start of the main function. Every C program starts running here.
printf(...);A statement: print something.
return 0;End the program and tell the computer "everything went fine" (0 means OK).
}The end of main.

Think of it like this: #include is like taking a toolbox off the shelf before you start working. stdio.h is the toolbox with the printing and reading tools.

Curly braces { }

The curly braces group statements together into a block. Everything between { and } belongs to main. We indent the code inside (move it 4 spaces to the right) so it is easy to see where the block starts and ends.

Comments: notes for humans

A comment is a note in your code. The compiler completely ignores it. C has two kinds:

#include <stdio.h>

// This is a one-line comment.

/* This is a comment
   that spans several lines. */

int main() {
    printf("Comments are ignored\n");  // a comment at the end of a line
    // printf("This line is switched off\n");
    return 0;
}

Output

Comments are ignored
  • // starts a comment that goes to the end of the line.
  • /* starts a comment that goes on until */, even over many lines.

Tip: Putting // in front of a line of code is a quick way to switch it off while you test something.

C cares about capital letters

C is case-sensitive: printf works, but Printf or PRINTF do not. The same goes for main and return.

Remember: A C program is: #include lines at the top, then int main() { ... }, with statements inside ending in ;, and return 0; at the end.

HOME LABS COMMUNITY DASHBOARD