Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
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.
Compiling the First C Program
3.1.
Save the Program
3.2.
Open the Command Prompt or Terminal
3.3.
Compile the Program
3.4.
Run the Executable
4.
Frequently Asked Questions
4.1.
Why do we start with a "Hello World" program?
4.2.
What does #include <stdio.h> mean?
4.3.
Can I use another function instead of printf() to display messages?
5.
Conclusion
Last Updated: May 29, 2024
Easy

Hello World Program in C

Author Sinki Kumari
0 upvote
Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

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

}

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.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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.

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.

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure andAlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry.
 

Live masterclass