C is one of the basic languages for a beginner. Dennis Ritchie invents it. 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 the C program with the Hello World program.
Writing the Hello World program is very easy and everyone can code it. However, it is important to understand how the C program runs and what is 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 the 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.
Table of Contents
Step-by-steps…
#include<stdio.h>
is a preprocessor. stdio.h
is a header file that includes multiple functions defined in it such as scanf()
, and printf()
functions. Adding this header file to our program, allows us to use functions defined in the header file. Using existing functions makes our job pretty easy.main()
function. Your program may have multiple functions, the code inside the main()
function block is executed first. Here, the return type of the main()
function is int.main()
function block starts with a {
and end with a }
delimiter.printf()
statement by passing a string as an input. It will print the string message on the output console.return 0;
It returns a SUCCESS message to the operating system.\n
in string, the message moves the cursor to a new line.Follow the steps given below.
helloWorld.c
(with .c extension).cd
command to change the current directory.gcc helloWorld.c
It will compile the program and create an executable file.
It creates sample.exe
if you are running a program on the Windows system and sample.out
for Linux system.
For Windows,
a.exe
For Linux,
./a.out
The output of the Program after execution:
It will print “Hello, World!” on the output console.
Hello, World!
In the above program, we are passing the “Hello world” string as input to the printf() statement. You can save this string as a character array and then pass this character array (str_msg
) as input.
#include <stdio.h> int main() { char str_msg[] = "Hello, World!"; printf("%s\n", str_msg); return 0; }
Don’t worry about the char array (str_msg
) if you are new to the C programming. I have explained that in the upcoming tutorial.
Covering some important questions every newbie program faces.
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 follows…
#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 the program runs successfully.
The stdio.h
header file is located in the GCC directory at .../include/stdio.h
You can find the location of all the header files by running the following command in your system’s console.
gcc -H -fsyntax-only helloWorld.c
It list-downs 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 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 essential to clear your doubts before heading to your next program. So feel free to write in the comment section below.
Happy Programming!