C & C++ fundamentals / 01
Output with printf
Write a first C program, print Hello World, and use newline characters to format terminal output.
On this page
Let's start with one of the most important operations in C: output. C uses the printf() function to print text. To display Hello World!, write:
printf("Hello World!");
This prints Hello World! in the terminal or Command Prompt.
Headers and the main function
Header files are another important part of C programming. Examples include <stdio.h> and <math.h>, which provide declarations for standard-library features. For the input and output examples in this series, include <stdio.h> with #include <stdio.h>.
The program also has a main function, int main() {}. The statements inside its braces are executed when the program runs. After completing its work, the main function can return 0 with return 0;.
Think of a semicolon, ;, as the period at the end of a statement. Remember to include it where a statement requires one.
Editorial note: the original introduction described
<stdio.h>as necessary for every C program. It is needed for the standard input/output functions used here, not for every possible C program.
Line breaks
The escape sequence \n inserts a newline, similar to pressing Enter when typing text.
printf("Hello World!\n");
Let's add another sentence:
printf("Hello World!\n Welcome!");
The terminal displays Hello World! on the first line and Welcome! on the next. The space before Welcome! in this particular string is also printed.
You can separate the two messages into two calls to make the code easier to read:
printf("Hello World!\n");
printf("Welcome!");
You still need \n: writing two printf() statements on separate source-code lines does not automatically insert a line break into their output.
Complete examples
#include <stdio.h>
int main()
{
printf("Hello World!");
return 0;
}
#include <stdio.h>
int main()
{
printf("Hello World!\n");
printf("Welcome!");
return 0;
}
Moved here from Tistory
I migrated this post from my Korean Tistory blog, I am Jason Lee, to this website and translated it into English. My writing and projects now live together in one place.
The original publication date, code examples, and screenshots are preserved. Editorial notes clarify known issues in the original material.
Original post on Tistory English edition · Aug 30, 2026