Compilation and Execution of C Program | Hello World Program in C
C is one of the basic languages fo a beginner. It is invented by Dennis Ritchie. In this article, you are going to write your first program. You will be educated with the step-by-step procedure for compilation and execution of C program with hello world program.
Writing Hello World program is very easy and everyone can code it. But it is important to understand how C program runs and what are the purpose of each line of code in the program.
In this program, we are adding stdio.h
file and using printf()
statement to print the string message “hello World” on output console.
Writing Hello World program in C:
#include <stdio.h> int main() { //print output string on console printf("Hello, World!\n"); return 0; }
This program runs all the operating systems including Windows, Linux, Mac OS. The only prerequisite as you should have GCC compiler installed on your system.
How does Hello World program run?
step by steps…
- The first line of code
#include<stdio.h>
is a preprocessor. stdio.h is a header file which includes multiple functions defined in it such asscanf()
,printf()
functions. Adding this header file in our program, allow us to use functions defined in the header file. Using existing function makes our job pretty easy. - Every C program execution starts with
main()
function. Your program may have multiple functions, the code inside themain()
function block is executed first. Here, the return type of themain()
function is int. - As like all other function,
main()
function block starts with a{
and end with a}
delimiter. - Write
printf()
statement by passing a string as an input. It will print the string message on the output console. - As we have defined the main with return int value, write
return 0;
It returns a SUCCESS message to the operating system. \n
in string message moves the cursor to a new line.
Compilation and Execution of C Program
Follow the steps given below.
- Save the program as
helloWorld.c
(with .c extension). - Open a command prompt.
- Go to the current directory where the program is saved using. You can use
cd
command to change the current directory. - Compile the program with the following command.
gcc helloWorld.c
It will compile the program file and create an executable file.
(a.exe if you are running program on the window system. a.out for Linux system).
- Running executable file.
For Windows,
a.exe
For Linux,
./a.out
The output of the Program after execution:
It will print “Hello, World!” on output console.
Hello, World!
In the above program, we are passing “Hello world” string as input to the printf()
statement. You can save this string as a character array and then you can pass this character array (strMsg) as input.
#include <stdio.h> int main() { char strMsg[] = "Hello, World!"; printf("%s\n", strMsg); return 0; }
If you ar new to the C programming, don’t worry about char array (string).
Frequently asked questions:
Covering some important questions every newbie program faces.
Why do we use return 0?
The main() is the first function that runs. “return <vlaue>” returns value to the OS.
This value can be 0 or 1.
As a part of good practice, you can use EXIT_SUCCESS
instead of 0 and EXIT_FAILURE
instead of 1.
EXIT_SUCCESS and EXIT_FAILURE are defined in the stdlib.h header file as follow…
#define EXIT_SUCCESS 0 #define EXIT_FAILURE 1
If you want to use EXIT_SUCCESS
or EXIT_FAILURE
, you have to add stdlib.h header file.
In our hello world program, we are using “return 0”. It means program runs successfully.
Where does the header file stdio.h file present in C?
The stdio.h header file is located in GCC directory at .../include/stdio.h
You can find the location of all the header files by running the following command.
gcc -H -fsyntax-only helloWorld.c
It lists complete path for all the header files included in helloWorld.c.
Wrapping Up…
This is all about running your first program in C. Now you are free to go ahead and start learning C.
I tried my best to describe each and every aspect of the compilation and execution of the C program. As this is the first program you are writing, you may have doubts.
It is very important to clear your doubts before heading to your next program. So feel free to write in a comment.
Happy Programming!