Open In App

C Hello World Program

Improve
Improve
Like Article
Like
Save
Share
Report

To begin with, the “Hello World” program is the first step towards learning any programming language and also one of the simplest programs you will learn. All one needs to do is display the message “Hello World” on the screen. Let’s look at the program and try to understand the terminologies involved in it.

C Program to Print “Hello World”

The following C program displays “Hello World” in the output.

C




// Simple C program to display "Hello World"
 
// Header file for input output functions
#include <stdio.h>
 
// main function -
// where the execution of program begins
int main()
{
 
    // prints hello world
    printf("Hello World");
 
    return 0;
}


Output

Hello World

Compiling the First C Program

Before proceeding to write the first program, the user needs to set up a C program compiler, which would compile and execute the “Hello World” program. Here we have used a Windows-based GCC compiler to compile and run the program. To know more on how to set up the local GCC compiler or run using online ide refer to Setting C Development Environment.

Step 1:This requires writing the “Hello World” program, in a text editor and saving the file with the extension .c, for example, we have stored the program in a C-type file HelloWorld.c.

Step 2: This includes opening CMD or command prompt line and navigating to the directory where the file HelloWorld.c is present. Here it is present in C:\Users\Chin\Sample.

Step 3: To compile the code execute the following command:

gcc HelloWorld.c

This would create a C-executable file with a random name given by the compiler itself. We got the executable filename as a.

To give a user-oriented name, run the following command:

gcc -o helloworld HelloWorld.c

This would create a C-executable file by the name helloworld.

Step 4: To run the executable file to get the result, run the following command:

helloworld

Explanation of the Code

Let us now understand the terminologies of the above program:

Line 1:

// Simple C program to display “Hello World”
  1. This is a single comment line. A comment is used to display additional information about the program.
  2. A comment does not contain any programming logic as it is not read by the compiler. When a comment is encountered by a compiler, the compiler simply skips that line of code.
  3. Any line beginning with ‘//’ without quotes OR in between /*…*/ in C is a comment.

More on Comments in C

Line 3:

#include 
  1. In C,  all lines that start with the pound (#) sign are called directives. These statements are processed by preprocessor program invoked by the compiler.
  2. The #include directive tells the compiler to include a file and #include<stdio.h> tells the compiler to include the header file for the Standard Input Output file which contains declarations of all the standard input/output library functions.

More on Preprocessors in C.

Line 6:

int main()
  1. This line is used to declare a function named “main” which returns data of integer type. A function is a group of statements that are designed to perform a specific task. Execution of every C program begins with the main() function, no matter where the function is located in the program. So, every C program must have a main() function and this is the function where the execution of the program begins.
  2. { and }: The opening braces ‘{‘ indicates the beginning of the main function and the closing braces ‘}’ indicates the ending of the main function. Everything between these two comprises the body of the main function and are called the blocks.

More on main() function in C.

Line 10:

printf("Hello World");
  1. This line tells the compiler to display the message “Hello World” on the screen. This line is called a statement in C. Every statement is meant to perform some task. A semi-colon ‘;’ is used to end a statement. The semi-colon character at the end of the statement is used to indicate that the statement is ending there.
  2. The printf() function is used to print a character stream of data on the stdout console. Everything within ” ” is displayed on the output device.

More on Input/Output in C.

Line 12:

return 0;
  1. This is also a statement. This statement is used to return a value from a function and indicates the finishing of a function. This statement is basically used in functions to return the results of the operations performed by a function.
  2. Indentation: As you can see the printf and the return statement have been indented or moved to the right side. This is done to make the code more readable. In a program as Hello World, it does not seem to hold much relevance but as the program becomes more complex, it makes the code more readable and less error-prone. Therefore, one must always use indentations and comments to make the code more readable.

More on return in C.
FAQ on the style of writing programs.



Last Updated : 16 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads