Compilation and Execution of C Program | Hello World Program in C

Compilation and Execution of C Program | Hello World Program in C

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.

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 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.
  • Every C  program execution starts with a 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.
  • As with all other functions, 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, the 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 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.

  • Running executable file.

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!
Compilation and Execution of C Program

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.

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 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.

Where does the header file stdio.h file present in C?

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!

Leave a Reply

Your email address will not be published. Required fields are marked *