Table of contents
1.
Introduction
2.
C Program to Print "Hello World"
2.1.
C
2.2.
Explanation of the Code
2.2.1.
Header File Inclusion
2.2.2.
Main Function Declaration
2.2.3.
Printing to Console
2.2.4.
Return Statement
3.
How "Hello, World!" Program Works?
4.
Compiling the First C Program
4.1.
Save the Program
4.2.
Open the Command Prompt or Terminal
4.3.
Compile the Program
4.4.
Run the Executable
5.
Other Ways to Write a "Hello, World!" Program in C
5.1.
Example 1: Using the puts function
5.2.
Example 2: Using the write function (low-level I/O)
6.
Frequently Asked Questions
6.1.
Why do we start with a "Hello World" program?
6.2.
What does #include <stdio.h> mean?
6.3.
Can I use another function instead of printf() to display messages?
7.
Conclusion
Last Updated: Dec 14, 2024
Easy

Hello World Program in C

Author Sinki Kumari
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Computers have become an essential part of our lives, empowering us to perform complex tasks with simplicity. For anyone venturing into the world of programming, starting with a basic C program like "Hello World" is a common first step. This program, although simple, lays the foundation for understanding the structure and syntax of C programming. 

Hello World Program in C

In this article, we will learn how to write, compile, and understand a "Hello World" program in C. This program is your first baby step into the world of computer programming.

C Program to Print "Hello World"

To begin our journey in C programming, the first task is to create a program that displays the message "Hello World" on the screen. This is a simple program that helps you understand the basic format of a C program. Here’s how you can write it:

  • C

C

#include <stdio.h>  // Include the Standard Input Output library header

int main() {  // Define the main function where the execution of the program begins

   printf("Hello World\n");  // Use printf function to display the message

   return 0;  // Return 0 to indicate that the program ended successfully

}
You can also try this code with Online C Compiler
Run Code

Output

Hello World


This code snippet consists of :

  • #include <stdio.h> tells the compiler to include the Standard Input Output library, which is necessary for using the printf function.
     
  • int main() is the starting point of any C program. This is the main function from where the execution of the program begins.
     
  • Inside the main function, printf("Hello World\n"); is used to print the text "Hello World" on your screen. The \n at the end of "Hello World" is a newline character that moves the cursor to the next line.
     
  • return 0; signals that the program has completed successfully.

Explanation of the Code

Understanding each part of the "Hello World" C program helps you learn the fundamental concepts used in more complex programs. Let's break down the code:

Header File Inclusion

#include <stdio.h>


This line includes the Standard Input Output header file in the program. This header file is necessary because it contains the declaration of the printf() function, which we use to print text to the console.

Main Function Declaration

int main() {


This is the declaration of the main function. In C, the main function is where the execution of the program begins. The int before main indicates that the main function will return an integer value.

Printing to Console

printf("Hello World\n");


Here, printf() is used to output the text "Hello World" to the console. The \n is a newline character that causes the cursor to move to the beginning of the next line, ensuring that any subsequent output starts from a new line.

Return Statement

return 0;


This statement is used to exit the main function and return control to the operating system. return 0 indicates that the program ended without any errors. The value returned is the "exit status" of the program, where 0 typically means success.

Note -: Each element in this program has a specific role, and together they introduce you to the syntax and structure of C programming. By learning these basics, you can tackle more complex programs very easily that use similar elements but perform more complex operations.

How "Hello, World!" Program Works?

  • The #include <stdio.h> preprocessor directive includes the standard input/output header file, enabling functions like printf.
  • The int main() function defines the entry point of the program. Every C program must have a main function.
  • Inside the main function, the printf("Hello, World!"); statement is called to print the string "Hello, World!" to the console.
  • The printf function is part of the C standard library and outputs the text passed to it.
  • The return 0; statement indicates successful program execution, returning control to the operating system.

Compiling the First C Program

After writing the "Hello World" program, the next step is to compile it to see the output. Compiling is the process of converting the program written in human-readable format into a format that the computer can execute. Here is how you can compile and run this program if you are using a C compiler like GCC:

Save the Program

First, save your program in a file named hello.c. Ensure the file extension is .c, which is used for C language source files.

Open the Command Prompt or Terminal

Access your command line interface. On Windows, you can use Command Prompt or PowerShell, and on macOS or Linux, you can use the Terminal.

Compile the Program

Type the following command and press Enter:

gcc hello.c -o hello


This command tells the GCC compiler to take the source code from hello.c, compile it, and output an executable file named hello. The -o hello part of the command specifies the name of the output file.

Run the Executable

Once the compilation is successful, run the program by typing:

./hello


This command executes the hello file, and you should see "Hello World" printed on the screen.

Note -: This process shows how the code you write in C is turned into an executable that the computer can run. Seeing "Hello World" appear on your screen confirms that your setup and your first program are correct.

Other Ways to Write a "Hello, World!" Program in C

Example 1: Using the puts function

#include <stdio.h>

int main() {
    puts("Hello, World!");
    return 0;
}
You can also try this code with Online C Compiler
Run Code

 

  • The puts function is used to print strings to the console.
  • Unlike printf, puts automatically adds a newline after the string.

Example 2: Using the write function (low-level I/O)

#include <unistd.h>

int main() {
    write(1, "Hello, World!\n", 14);
    return 0;
}
You can also try this code with Online C Compiler
Run Code

 

  • The write function is a system call used for low-level output, directly writing to file descriptors.
  • 1 refers to the standard output (stdout), and 14 specifies the number of bytes to write.

Frequently Asked Questions

Why do we start with a "Hello World" program?

The "Hello World" program is simple and demonstrates the basic structure of a C program. It’s used globally as the first program to introduce the basics of syntax and program execution.

What does #include <stdio.h> mean?

This line includes the Standard Input Output library in your program, which contains necessary functions like printf() used for displaying output.

Can I use another function instead of printf() to display messages?

While printf() is the most common, you can also use puts() or fprintf() to display messages. Each function has its own features and uses.

Conclusion

In this article, we have learned how to create, compile, and execute a simple "Hello World" program in C. This initial step into programming helps you understand the basics of writing and running C code, which forms the foundation for learning more complex programming concepts. By understanding this simple program, you are ready to tackle more challenging and complex problems of the programming world.

Live masterclass