Your first program
- C course
- Beginner
- Lesson 2/116
- 4 min
Learn to write your first C program with the main function, #include and printf.
main and printf
Every C program starts from the main function. The #include <stdio.h> line brings in the printf declaration so you can print text.
#include <stdio.h>
int main(void) {
printf("Hello");
return 0;
}Here int is the return type, main is the entry point, void means no parameters. printf writes text, return 0; signals success.
What does return 0 mean?
The return 0; statement at the end of main tells the operating system the program finished successfully. A non-zero value usually signals an error.
In modern C, writing return 0; in main is common. Other functions return whatever value they compute.
Practice Write your first C program
Write a C program. Start with #include <stdio.h>, define int main(void), and print Hello with printf.
Starter code
// Add main function and printfSample solution
#include <stdio.h>
int main(void) {
printf("Hello");
return 0;
}Expected output
Hello
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What does #include <stdio.h> do?
- Defines a class
- Brings in the printf declaration Correct answer
- Declares a variable
- Defines the main function
Explanation: #include <stdio.h> includes the stdio header, which declares printf.
2 Complete the printf call
printf(____);
printf("Hello");
Explanation: printf("Hello"); prints Hello to the screen.
3 Match the keywords
| Term | Meaning |
|---|---|
#include |
Import |
int |
Integer type |
main |
Entry point |
printf |
Output |