Summary: in this tutorial, you will learn step-by-step how to develop the first simple but famous program called C Hello World.
A quick introduction to functions
A function is a piece of code that does a specific task. A function accepts an input, processes the input, and returns the result
For example, the add(x, y)
function accepts two numbers and returns the sum of them. If you pass 1 and 2 to the add(x, y)
function, you’ll get 3 back as the result.
The C function concept is the same.
Since C is a procedural programming language, a C program consists of one or more functions. Also, a C program always starts with a special function called main()
.
That’s all about the function you should know for now. Let’s start using functions to develop the Hello World program.
The C Hello World program
The following shows a simple C program that outputs the Hello, world!
message to the screen:
#include <stdio.h>
int main()
{
printf("Hello world!\n");
return 0;
}
Code language: C++ (cpp)
How it works.
First, the program starts with the following directive:
#include <stdio.h>
Code language: C++ (cpp)
Every C directive starts with a hash (#) sign. C uses this directive to load an external function library that is a group of reusable functions.
The program includes the stdio.h
library that provides standard input/output functions. The printf()
function is one of the functions provided by the stdio.h
library.
The printf()
function displays a message to an output, which is the screen in this example.
Second, you see the main()
function:
int main()
Code language: C++ (cpp)
A C program starts with the main()
function. The int stands for integer. It indicates that the main()
function needs to return an integer.
The body of a function starts with the curly brace ({
) that follows the main()
and ends with the curly brace (}
).
Third, inside the main()
function, we use the printf(
) function. The print() function accepts a sequence of characters as an input and displays the text to the screen:
printf("Hello world!\n");
Code language: C++ (cpp)
The text within the quotes (“) is called a literal string in C.
The \n
is a special character that represents the newline character. When the printf()
funtion sees the \n
inside a string, it treats add a new line to the output.
To return a value from a function, you use the return
statement. For example:
return 0;
Code language: C++ (cpp)
This return
statement returns 0, which indicates that the program ends successfully without any error.
Develop the C Hello World program with CodeBlocks IDE
In the following section, we will show you step-by-step running the C Hello World using CodeBlock IDE.
If you haven’t installed the CodeBlocks IDE, you can set it up by following the setting up C development environment tutorial. However, if you use a different IDE, you can skip this section.
Step 1. Launch the CodeBlocks IDE and create a project from the menu File > New > Project…
Step 2. Select the Console application and click the Go button:
Step 3. Check the Skip this page next time checkbox and click the Next button.
Step 4. Select the C language from the list and click the Next button:
Step 5. Enter the Project title as HelloWorld and click the Next button:
Step 6. Select the compiler to use. You need to accept the default options and click the Finish button:
Step 7. The Codeblocks creates a new file called main.c with the following code:
Step 8. From the Build menu, click the Build and Run option to build and run the program. You’ll see the new window on the screen that displays the Hello world! message:
Congratulation! you have successfully developed and run the C Hello World program.
Summary
- Use a directive to load an external library. A directive starts with the
#
sign and is followed by the library name. - The
stdio.h
is the standard input/ouptut library in C. - The
printf()
function belongs to thestdio.h
library. It display a message to the screen. - A C program always starts with the
main()
function. - Use the
return
statement to return a value from a function.