Our first C program is going to be the Hello, World! program. In this program we will cover an overview of the components of the basic C program.


If you are a beginner then there is always a question that where should we start from. Most of us get confused what should one have to write while creating his first C program. So in this tutorial I will try to explain what should we have to do while writing our first C program.


NOTE: Before proceeding ahead on this tutorial kindly read the article on Basic Structure of C program otherwise you will not be able to understand this article.


You must have a C compiler already installed and a text editor for writing and compiling this C program. If you don’t have one then kindly read my previous article on C Environment setup in C programming Tutorial series.

NOTE: In this tutorial I am using macOS terminal so the terminal commands may differs from yours.


First of all I will write the program and after that I will explain it. OK to do so open the text editor and write the following program.

//Hello, world! program

#include<stdio.h>

int main()
{
    printf("Hello, world!");
    return 0;
}


After writing the above program save it as hello.c in the desktop. We will place our file in the desktop so that it will be easy for us to access it later.


Now next step is to compile it using terminal commands. For this open the terminal if you have UNIX/Linux or macOS and command prompt if you have windows OS.


Now type the following command to compile.

$ gcc /Users/ojha/Desktop/hello.c -o /Users/ojha/Desktop/hello


In above command hello.c is the C source code filename that we created earlier and hello will be the compiled or output file. And ojha is my username, kindly replace with it with yours.


NOTE: If you don’t want to place the file in desktop then change the current directory to the location where the file is present or provide full path of the file while executing the above command.


Once you have executed the above command you will see the new file named hello created in the desktop. It means that our program is compiled.

Now we need to run it by executing the following command:

$ /Users/ojha/Desktop/hello

Once you have executed the above command you will see the following output in the terminal.

Hello, world!

If you see above message it means our program doesnot have any error.

Explanation

If you compare above program with the Basic Structure of C program you will find it almost similar except for few changes because we want it be simpler.


//Hello, world! program


Ok the first line in the program is the Documentation Section which includes the comments and the comments begin with two forward slashes. It is not compulsory to write this part but it is a good practice to have the documentation part. You can write anything like title of program, author name, date etc.. in this part.


#include<stdio.h>


Next we have the Link Section in the second line where we have linked the stdio.h file via the preprocesser directive command. For the Input and Output operation we need to include the stdio.h (Standard Input Output) header file in our program.


int main()
{

  ...

}


Next in the third line We have our main() function section. This is the part from which the C compiler starts executing the program.


Here main is the name of the function. And it can’t be changed.


After that it has two parentheses () these indicates that the main is a function. In C program every function have these braces.


The main() function contains two braces { } open and close, these two braces are the start and end point of the main() function and everything that we need to execute will be contained inside these two braces.


The main() function starts with a keyword int. This is the return type of the function, right now in this program it will return 0 integer value if our program does not fails.


Next, inside our main() function between two braces { } there are two lines.

printf("Hello, world!");
return 0;

These are the Executable statements.


The first line is a printf() function, it is the part of the stdio.h header file. The printf() function will print our message in the console.

Here printf is the name of the function and after its name there are two parentheses () which indicates that it is a function. Main important thing is that inside those braces there is our message Hello, world! enclosed inside double inverted commas "". Everything inside those double inverted commas will be printed on the console screen.


Now the next line is return 0 this line returns 0 integer value if the program doesnot have any errors.


And If you noticed, every line inside the main() function are ended with a semicolon ; this is called as the statement terminator which indicates the end of the statement.


If you compared the Hello, world! program and Basic Structure of C program then you will observe some sections like Definition Section, Global Declaration Section and the Sub program Section are missing. Don’t worry, we don’t need them in this program, we will use them whenever they are required.


Don't skip any line while reading the article and most important thing is that reading only is not enough, you need to execute it by yourself also just don't copy and paste the program instead type it by yourself.


I hope you enjoyed it. Thank you.