Writing the First Hello World Program in C

Writing the First Hello World Program in C


Even the huge buildings, shopping complex etc. are built brick by brick. Isn’t that so?

Similarly to learn the basics of C language you need to start from the very first program in C which prints Hello World.

When you start learning to ride a bicycle, you do not learn the nuts and blots! You just sit on that and start riding it.

And the same goes here too.

I will not teach you to learn the variables, constants etc. in C in the first place. I will first write the Hello World program in C and then take you through an explanation line by line.

If you are wondering …

What is this Hello, World! thing?

When one starts teaching about any kind of Computer language, they all start with printing a Hello, World! text.

So, it is a no-brainer to understand that this just a common practice that almost everyone follows in the programming world.

So, we will just start to write the actual C code for this program.

The Hello, World! C Program

Writing the code is not enough in C. There are many other things involved from creating the code to run it as a program. Below is a sequece of actions need to be performed:

  • Writing the C Program
  • Compiling the code to create an executable binary file
  • Execute or Run the binary executable file

So, the sequence goes like this: C program > Compilation > Execution

After executing the file you will see the output of your program.

I hope things are quite clear till now. If so, let’s look at the below C program that prints Hello, World! on the display output.

#include <stdio.h>

/* The entry point function in C */
int main ()
    /* The actual print statement */
    printf ( "Hello, World!" );

    return 0;

Depending your system and the development setup you are using, create and save this code in a filename called let’s say helloworld.c

You can simply copy this text but I would strongly suggest you to look at this code and write the same code by yourself. It will be helpful for you in the long run.

Compile the Code

Once the code is written and a C file is created for the same, it needs to be compiled to generate an executable code.

Compilation of C code itself has multiple level of processing in it which needs to be explained in a separate post.

If you are using an IDE like Code Blocks or C Eclipse IDE, then you need to click on the compile button.

If you are using Linux, MacOS, or FreeBSD you can compile the same using gcc compiler. You can execute the below command to compile the above Hello World code:

gcc helloworld.c

The above command will generate a filename a.out

If you want to modify the name of the output executable file, then execute the below command:

gcc helloworld.c -o helloworld.out

Note the -o option and then followed by the output file name.
The above command will generate a filename helloworld.out

Execute or Run the binary file

Once you have compiled using an IDE or the command line, you need to run or execute the output file to see the output of your program.

Depending on your IDE, click on the Run / Execute button.

If you are using Linux, MacOS, or FreeBSD, run the below command based on your output filename:





The Output

After executing the program, you will see the below output on your IDE console window or the command prompt window:

Hello, World!


Understanding the Hello World C Program

Now that you compiled and executed the code, let’s go through the code line by line.

Line number 1, # include:

#include <stdio.h>

It has 3 parts in it which is as following: #, include, and the <stdio.h>

# is a preprocessor directive in C programming (more on this in a separate post).
include <stdio.h> tells the compiler to include the header file stdio.h in our C code before the code is compiled.

(actually this is the job of the preprocessor but to simplify your understanding let’s think that compiler does the job of including the specified file)

Of course there is more explanation to the above statement but that would be an extra burden for you to understand at this moment.
But if you are curious to know more on this #include thing then just let me know in a comment and I will get back with a more detailed post on this topic.

For now you just need to understand that wherever there is some text that starts with # will be processed by the preprocessor (the first phase of the compiler).

The stdio.h header file contains the function declaration of printf ( ) that is why we added the above line of code in our program.

Execute the below command to see the expanded or preprocessed c code:

gcc -E helloworld.c

Line number 2 and 5, Comments in C:

/* The entry point function in C */


/* The actual print statement */

These statements are comments.
Comments are ignored by compiler and are only written for humans to understand.

Line number 3, the main function:

int main ()

Main function is where the actual user written code resides.

To be able to perform anything in C, we need to do it inside this main () function.

A function in C has 3 parts in it as following:

  • Function declaration: int main ( void );
  • Function definition: int main ( ) { some code inside this }
  • Calling the function: main (); to perform some action which is defined inside two curly braces { }

A function in C is something that performs a specific task. The specific task is defined by the instructions or statements written inside two curly braces { }.

Line number 6 and 7, printf and return:

    /* The actual print statement */
    printf ( "Hello, World!" );

    return 0;

This is the actual main ( ) function definition. Inside the function and after the comment, you see there are two statements.

Every statement ends with a semicolon ;. That’s how the compiler understand the end of one statement.
If you miss this semicolon ; in the end of a C statement, the compiler will through error.

printf (); is a function call. Inside the main ( ) function we call the printf ( ) function and pass the value “Hello, World!” to process it.

Basically printf ( ) function’s job is to print anything that is passed between the double quotes " " (excluding the double quotes).

The Hello, World! print on the command prompt or on the output window comes from this statement.

You rewrite the program by inserting any text inside these two double quotes and you will see the magic in the output.

For every change you make in the code you need to re-compile it and then re-execute the new binary output file.

Otherwise it will be like you are changing somewhere and testing something else.

The return value

Imagine, you handed over some money to Mr. X to accomplish a task.

How do you know if Mr. X has performed the task successfully or not?

So, Mr. X has to return to you and has to report you back with the status, right?

The same is done by this return 0; statement in our code. It is to tell the caller of main ( ) function that we are returning successfully.

Every function has different values of returning successfully. If you see the return value of printf ( ), it returns the number of characters printed and not 0.

You can experiment this to get more clarity.

That’s All

This is a very minimal C program to start with and understand the basics of C programming.

I tried to keep it as simple as possible and explain as much as possible. But, if you feel like there is something that you could not understand, just leave a comment so that I can help you understand better.

Previous Python Dictionary Data Type
Next Understanding C Programming Basics

About author

shibaa987 274 posts

Linux kernel developer and a firmware developer with an experience of 10+ years.

View all posts by this author →

You might also like

C Programming 0 Comments

C Style coding standard: All you want to know about

This post was last updated on November 3rd, 2020 at 05:11 pmCoding standard makes code writing cleaner and increases readability. C style is one such coding standard that standardizes the

C Programming 0 Comments

How To Compile And Run C Or C++ Code In Linux/Unix

This post was last updated on August 3rd, 2020 at 03:14 pmProgramming in Linux has various advantages over other operating systems. It provides a vast repository of open source tools

C Programming 0 Comments

How to Compile and Run C Program on a Linux Terminal

This post was last updated on April 17th, 2021 at 11:08 pmLinux is the best platform to learn C programming because it is an open source platform and you will


No Comments Yet!

You can be first to comment this post!

Leave a Reply