top

Search

C Tutorial

.

UpGrad

C Tutorial

C Hello World Program

Introduction

"Hello, World!" program is an easy, introductory program that kick-starts your journey of learning C programming language. Usually, it is used to introduce the fundamental syntax of the language and verify that the development environment is correctly set up. The program displays the “hello world” text as the output by using the standard output function of C. 

Before going ahead with learning how to create a C Hello World Program, let’s first understand how to compile it.

Compiling the First C Program

You need a C compiler to run an error-free C program. A GCC compiler is used in the following steps to compile the first C program.

Step-1: First, you need to download and install the GCC compiler into your system from here.

Step-2: Post setting up your compiler, you will require a text editor to write the C program of your choice. You can also work with the Notepad to do the same.

Step-3: Then, type the following C program.

#include<stdio.h>
 
int main() 
{
printf("hello world");

return 0;

}

Step-4: If you are using a Notepad, save your file within a folder and name it ‘Helloworldprogram.C’ or give any other name of your choice. Make sure you use .C as your file extension. 

Step-5: Open the terminal in the same place where your files are located. Right-click on the folder with your saved file. Now, click on 'Open in Terminal'.

Step-6: In the terminal, type gcc .\helloWorld.C and hit Enter. This step will enable the compiling process to start in the helloWorld.C program and create a file to execute known as a.exe in your folder containing helloWorld.C.

Step-7: Run the a.exe file by writing .\a.exe. "Hello World" will be printed as an output!

Easy C Program to Print "Hello World"

Looking for an easy Hello World program? Here’s how you can do it!

// The following is the header file that includes the definition of the printf() function.
#include<stdio.h>
 
int main() 
{
printf("Hello World"); // The printf function prints the text written within it.

return 0;  
}

Output:

Hello World

As seen in the above sample program code, the header file is included. The main function contains the printf() function to display the text in the output terminal.

 "Hello, World!" Program: How does it work?

Comprehending the explanation and reason for each statement behind the C Hello World Program is vital to understand how it functions. Let’s dive in to comprehend the distinct features of each statement.

1) Comment line:

You can use a single comment line to display information about the program. Note that it is not mandatory to add a comment line in your C program. It doesn’t contain any programming language because the compiler doesn’t read it.

When a compiler encounters a comment, it skips that code line. Any line starting with ‘//’ without quotes OR enclosed between /*…*/ in C is considered a comment.

2) Header file and pre-processor directive:

The above statement #include works as a pre-processor directive here that processes the functions present within the header file, i.e., stdio.h. One of these functions is printf(), which is used to display the output.

All lines starting with the pound (#) sign in C are called directives. The preprocessor program invoked by the compiler process these statements. The #include directive informs the compiler to include a file. The #include<stdio.h> informs the compiler to include the header file for the Standard Input Output file that consists of declarations of all the standard input/output library functions.

3) Main function:

int main()
{
...
...
}

The execution of all C programs starts with the main() function, irrespective of the location of the function in the program. Hence, every C program should include a main() function. The C program’s execution begins from the main() function.

Any C program begins with the main function’s first line and terminates at its last. The compiler executes whatever you write within the main() function.

4) printf() function:

printf("Hello World");

In the C programming language, the standard output function is printf(). Whatever you type in the printf() function will be printed as an output on the console. If you write 'printf("Hello World")' in the main() function, then the compiler prints the string "Hello World" on the output (stdout console). In short, everything written within “ ” inside the printf() function is displayed in the output.

5) Return 0:

This statement Return 0; in a Hello World program implies that the program has been executed successfully, without any problems or errors. This statement is relevant to highlight that any program has been successfully executed. Including Return 0; is also considered good practice in C to reinforce that the program has been successfully executed.  

6) Indentation:

The above hello world program shows that the return and the printf statement are moved or indented towards the right side. The indentation increases the code’s readability. The use of indentation makes sure your code stays readable and less error-prone. This is applicable even though your program becomes complex. So, you should always use comments and indentations to increase your code’s readability.

Using Functions to Write Hello World Program 

You can write the hello world program in C programming using a function. Here is the hello world program written using a function named printHelloWorld(). Note that we wrote this hello world program in C using void main.

#include <stdio.h>
 
void printHelloWorld() 
{
printf("Hello, World!\n");
}
 
int main() 
{
printHelloWorld();
return 0;
}

Output:

Hello, World!

In the above program, we define a function printHelloWorld() that displays "Hello, World!" to the output console. It does this using the printf() function contained in the <stdio.h> library. The main() function begins the program’s execution. It calls the printHelloWorld() function. 

Lastly, the program returns 0, which suggests successful execution. If you run this program in a C compiler, you get "Hello, World!" as the output.

C Hello World Using Character Variables

Besides using functions, character variables can also be used to create the “Hello World” message as your output. These variables print the individual string of the text “Hello World”. In this approach, users can allocate every “hello world” string character to an individual character variable. Here is the hello world program in C# that displays the “Hello World” program with character variables. 

#include <stdio.h>
 
int main() 
{
char H = 'H';
char e = 'e';
char l1 = 'l';
char l2 = 'l';
char o = 'o';
char space = ' ';
char W = 'W';
char o2 = 'o';
char r = 'r';
char d = 'd';
 
printf("%c%c%c%c%c%c%c%c%c%c\n", H, e, l1, l2, o, space, W, o2, r,l1, d);
 
return 0;
}

Output:

Hello Worl

As seen from the above program, we separately declare character variables to indicate each letter and punctuation mark in the string. The printf() function prints these characters in the appropriate order. The %c format specifier prints individual characters. These characters are passed as arguments to printf() in the proper sequence. 

Lastly, the program returns 0 to represent successful execution. If you run this program, you get "Hello World" as the output. 

Hello World in C Indefinitely

The following hello world program in C++ or C uses an infinite for loop to generate the "hello world" string infinite times. 

#include <stdio.h>
 
int main() 
{
for (;;) 
{
    printf("Hello, World!\n");
}
 
return 0;
}

Output:

Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
.
.
.
.
.

In the above program, we use a for (;;) loop to generate an infinite loop. The loop parentheses have an empty expression. It suggests that there is no initialization, condition, or update expression involved. This type of for loop leads to an infinite loop that continues executing the code indefinitely.

In the loop, the printf() function is written to print "Hello, World!" to the output. Hence, the program will continue printing the message continually until it is interrupted or manually terminated.

Conclusion

It is easy to get started with C programming by first learning how to write the hello world program in C. It familiarises you with the basics of the header file, main() function, printf() function, and return statement. You can gradually work on other programming examples once you thoroughly understand the working of the C Hello World Program.

Going through the tutorial is one of the most effective ways to get started with learning C programming. In addition, you can pursue upGrad’s Executive Post Graduate Programme in Software Development by IIIT-B, which ensures career advancement in the field of software development. It covers the fundamentals of computer science and various other advanced topics related to software development. It assists you in unlocking exciting job opportunities like full-stack developer, frontend developer, backend developer, and UI developer. Once you complete this course, you can move a step ahead toward embarking on your career in the field of STEM!

Enrol now to start your journey!

FAQs

1. Why is "Hello, World!" text printed on the console?

In programming, it is a common convention to print the "Hello, World!" text to the console.  It employs an easy approach to verify that your program is successfully running and can display text at the output. Also, it illustrates how to use fundamental output functions like printf() in C.

2. Can you use a different message rather than "Hello, World!" in the program?

Yes, you can let your program print any message you want by simply modifying the text written within the printf() function. For instance, you can replace the “Hello, World!” text with printf("Let’s learn C programming!"); to display a different message.

3. Is the printf() the only function to write the "Hello, World!" program?

No, you can use a different function to print the "Hello, World!" text. Note that printf() is a commonly used function to display output in C since it offers versatility to format and print your text. You can define a custom function printHello() that opens a file named "output.txt" in write mode by using the fopen() function from the header file <stdio.h>. After the file successfully opens,  you can use the fputs() function to display the "Hello, World!" message to the file. 

Leave a Reply

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