
Line 1: #include <stdio.h>
is a standard header file in C that provides access to input and output functions like printf()
(used in line 4). Including header files allows your program to use additional features.
If you’re unsure about how #include <stdio.h>
works, that’s okay. Just know that it’s commonly found at the beginning of most C programs.
Line 2: This is simply a blank line. While C doesn’t require specific spacing and ignores white space, adding empty lines can improve the readability of your code.
Line 3: Every C program needs a main()
function — it’s the starting point where the execution begins. Any instructions placed within its curly braces {}
will run when the program is executed.
Line 4: The printf()
function displays text on the screen. In this example, it prints “Hello World!”.
Keep in mind: Every instruction or statement in C ends with a semicolon ;
.
Also, the contents of the main()
function could be written more compactly as:int main(){printf("Hello World!");return 0;}
Remember: White space is ignored by the compiler, but using multiple lines helps make the code easier to read and understand.
Line 5: The return 0;
statement marks the end of the main()
function and indicates that the program finished successfully.
Line 6: Don’t forget to include the closing brace }
to properly close the main()
function block.
C Statements
In programming, a computer program is essentially a sequence of instructions that the computer carries out. In the C language, these instructions are known as statements.
For instance, the following statement tells the compiler to display the message “Hello World” on the screen:
cCopyEditprintf("Hello World!");
Each statement in C must end with a semicolon (;
). This punctuation is crucial—if it’s omitted, the compiler will throw an error and the program will fail to run.
Incorrect example (missing semicolon):
cCopyEditprintf("Hello World!")
This will result in an error similar to:error: expected ';' before 'return'
Multiple Statements
A typical C program consists of multiple statements, which are executed sequentially, in the order they appear in the code.
Here’s an example with several statements:
cCopyEditprintf("Hello World!");
printf("Have a good day!");
return 0;
Explanation:
- The first line outputs “Hello World!” to the screen.
- The second line displays “Have a good day!”.
- The third line,
return 0;
, indicates that the program has completed successfully.
As you continue learning C, you’ll explore more types of statements. For now, the key takeaway is: always end your statements with a semicolon to prevent syntax errors.

C Output (Displaying Text)
In C programming, the printf()
function is used to display output on the screen, such as text or values.
Example:
cCopyEdit#include <stdio.h>
int main() {
printf("Hello World!");
return 0;
}
Using Double Quotation Marks
Whenever you want to print text, it must be enclosed within double quotes ("
). Failing to do so will lead to a compilation error.
Correct:
cCopyEditprintf("This sentence will work!");
Incorrect:
cCopyEditprintf(This sentence will produce an error.);
In the incorrect example above, the missing double quotes will cause the compiler to display an error.
Multiple printf()
Statements
You’re free to use multiple printf()
functions in your program. However, it’s important to understand that by default, printf()
does not add a new line after each output, so the text will appear on the same line.
Example:
cCopyEdit#include <stdio.h>
int main() {
printf("Hello World!");
printf("I am learning C.");
printf("And it is awesome!");
return 0;
}
In this case, all the text will be printed on a single line unless you explicitly insert line breaks using \n
, which you’ll learn about in upcoming lessons.
New Lines in C
To move the output to a new line in C, you can use the special character \n
, which represents a line break.
Example:
cCopyEdit#include <stdio.h>
int main() {
printf("Hello World!\n");
printf("I am learning C.");
return 0;
}
In the example above, "Hello World!"
will appear on the first line, and "I am learning C."
will be printed on the next line.
Printing Multiple Lines in One Statement
It’s possible to include several lines of output in a single printf()
statement by inserting multiple \n
characters:
Example:
cCopyEdit#include <stdio.h>
int main() {
printf("Hello World!\nI am learning C.\nAnd it is awesome!");
return 0;
}
Although this approach works, it can reduce readability, especially in longer programs.
Adding Blank Lines
If you use two newline characters (\n\n
) in a row, it will leave a blank line in the output:
Example:
cCopyEdit#include <stdio.h>
int main() {
printf("Hello World!\n\n");
printf("I am learning C.");
return 0;
}
What is \n
?
The \n
symbol is known as an escape sequence. It tells the program to move the cursor to the start of the next line, effectively creating a line break.
Other Common Escape Sequences in C:
Escape Sequence | Description |
---|---|
\t | Inserts a horizontal tab space |
\\ | Displays a single backslash (\ ) |
\" | Prints a double quotation mark (" ) |
Comments in C Programming
Comments are used in C to explain code and improve readability. They’re especially helpful for documenting what certain parts of the code do, making it easier for others (or yourself) to understand the logic later. Comments can also be used to temporarily disable parts of the code during testing or debugging.
C supports two types of comments: single-line and multi-line.
Single-Line Comments
A single-line comment begins with two forward slashes: //
.
Everything after //
on that line is ignored by the compiler and will not be executed.
Example 1: Comment before a line of code
cCopyEdit// This is a comment
printf("Hello World!");
Example 2: Comment at the end of a line
cCopyEditprintf("Hello World!"); // This is a comment
Multi-Line Comments
Multi-line comments begin with /*
and end with */
.
All the text enclosed between these symbols is treated as a comment and is not executed.
Example:
cCopyEdit/* This code will display "Hello World!"
on the screen. It's a simple example. */
printf("Hello World!");
When to Use Each Type?
You can choose either format depending on your needs:
- Use
//
for short, single-line notes. - Use
/* ... */
for longer explanations that span multiple lines.
Note: Prior to the C99 standard (introduced in 1999), only multi-line comments were supported in C. Single-line comments became available with that update.