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.
#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.
#include <stdio.h>
int main() {
printf("My First C Program\n");
return 0;
}My First C ProgramThe #include line brings in printf, and main() is where execution begins.
#include <stdio.h>
int main() {
printf("Line 1\n");
printf("Line 2\n");
return 0;
}Line 1
Line 2Multiple 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.
