Whenever we start to learn any programming language, “Hello World Program” is the first one where we learn the basic syntax. In this article, we will look into “C Hello World Program” and start our journey to learn C programming.
Software Requirement
The following software(s) are required in the computer to begin with C programming.
-
- Text Editor
A “Text Editor” is software to write or modify plain text. We’ll be using it to write the Source Code of C program. There are several “text editors (e.g. Notepad, Code Writer etc.)” and “IDE’s (e.g. Eclipse, Codeblocks etc.)” available and you are free to choose from them.
Though, it is strongly advised to use a “Text Editor” instead of an IDE if you are new to C programming. We recommend Notepad++ or Microsoft Visual Studio Code for beginners.
-
- Compiler
A compiler is a tool which converts the “Source Code” of C program into “Machine Code” (also called as Executable Code). There are several C compilers available. We recommend using MINGW GCC Compiler.
Please refrain from using a very old compiler such as “Turbo C”. Also, note that the availability of multiple compilers of C doesn’t mean there are multiple C language(s). They would compile the C code almost the same way.
You don’t need to go for technical and other differences of compilers and “versions of C” at this point; those are beyond the scope of this tutorial series.
If you do not have “Text Editor” and “Compiler” installed and configured in your computer, refer to this article for the same. Follow steps carefully for your respective OS and configuration.
C Hello World Program
Writing the Source Code
Open the “Text Editor” which you have installed and type Source Code (avoid Ctrl+V or Copy Paste). Do not worry about understanding it at this time. We’ll be diving into every word and symbol of it very soon.
#include <stdio.h>
void main()
{
printf("Hello World");
}
Now save it with name as “HelloWorld” and extension “.c” i.e. “HelloWorld.c” on desktop or any folder/directory you like (e.g. ‘D:Programming_In_C’).
Compiling the Source Code
Assuming that you have the compiler installed and the PATH is configured, following the steps mentioned below.
Open Command Prompt (Windows) or Terminal (Unix/Linux/Mac) and go to the directory where you have saved the file ‘HelloWorld.c’ using cd command. Here we are showing how to do it in Windows.
If the file ‘HelloWorld.c’ is saved in ‘D:Programming_In_C’ folder, the commands would be:
- D: and press Enter/Return key
- cd D:Programming_In_C and press Enter/Return key
Following the steps correctly would show the path of the directory where you have saved your file. To compile our source code, type this command in command prompt:
gcc HelloWorld.c -o HelloWorld
and press Enter/Return key.
If you have typed code and configured the compiler correctly, there should be no output. If there is some output(error) displayed at this point, fix this before proceeding further.
In the command mentioned above;
- gcc is name of the compiler.
- HelloWorld.c is name of file which would be compiled by the compiler.
- -o HelloWorld means that output file (which is Machine Executable Code) is saved by name “HelloWorld” (e.g. HelloWorld.exe in Windows). if -o is not given, file will be saved with name “a”.
Running the C Hello World Program
Now in the same terminal or command prompt window, type HelloWorld and press Enter/Return key. You should see a output saying “Hello, World!”.
C Hello World C Program Run
Congratulations
You have successfully taken the first step of your learning path of C Programming. This is one-time process only and after this, we’ll start understanding and implementing the coding in C. If you facing any trouble in the installation or setting up compiler or editor, ask in comments with the error you are getting.