C ยท Chapter 3 of 45

C Get Started

Every C program starts execution from the main() function. Before you can use standard library features like printf, you must include the relevant header file using #include.

A C program is compiled into machine code, then executed as a standalone program. This two-step process (compile, then run) is different from interpreted languages that run source code directly.

Syntax
#include <stdio.h>

int main() {
  // code goes here
  return 0;
}

Anatomy of a C program

A basic C program includes headers, defines main(), contains statements ending in semicolons, and returns an integer status code to the operating system.

Compiling and running

Save your code in a file ending in .c, compile it with a compiler like GCC, then run the resulting executable file from the terminal.

Example 1 (c)
#include <stdio.h>

int main() {
  printf("My First C Program\n");
  return 0;
}
Output
My First C Program

The #include line brings in printf, and main() is where execution begins.

Example 2 (c)
#include <stdio.h>

int main() {
  printf("Line 1\n");
  printf("Line 2\n");
  return 0;
}
Output
Line 1
Line 2

Multiple statements execute in the order they appear.

Key points

  • Every C program needs a main() function.
  • #include brings in standard library features.
  • Statements end with a semicolon.
  • return 0; tells the OS the program finished successfully.
๐Ÿ’ก Note: Forgetting a semicolon is one of the most common beginner mistakes in C.

๐Ÿ“ Quick Quiz

1. Where does execution start in a C program?

2. What does #include <stdio.h> provide?

3. What does `return 0;` at the end of main mean?