The Wayback Machine - https://web.archive.org/web/20240721032016/https://www.geeksforgeeks.org/c-hello-world-program/
Open In App

C Hello World Program

Last Updated : 16 Nov, 2023
Improve
Suggest changes
Post a comment
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.



Previous Article
Next Article

Similar Reads

Hello World Program : First program while learning Programming
In this article, I'll show you how to create your first Hello World computer program in various languages. Along with the program, comments are provided to help you better understand the terms and keywords used in theLearning program. Programming can be simplified as follows: Write the program in a text editor and save it with the correct extension
6 min read
OpenMP | Hello World program
Prerequisite: OpenMP | Introduction with Installation GuideIn C/C++/Fortran, parallel programming can be achieved using OpenMP. In this article, we will learn how to create a parallel Hello World Program using OpenMP.STEPS TO CREATE A PARALLEL PROGRAM Include the header file: We have to include the OpenMP header for our program along with the stand
3 min read
Writing First C++ Program - Hello World Example
C++ is a widely used Object Oriented Programming language and is relatively easy to understand. The "Hello World" program is the first step towards learning any programming language and is also one of the most straightforward programs you will learn. The Hello World Program in C++ is the basic program that is used to demonstrate how the coding proc
4 min read
Java Hello World Program
Java is one of the most popular and widely used programming languages and platforms. Java is fast, reliable, and secure. Java is used in every nook and corner from desktop to web applications, scientific supercomputers to gaming consoles, cell phones to the Internet. In this article, we will learn how to write a simple Java Program. Steps to Implem
5 min read
The "Hello World" To Programming
If you have suddenly got interested to get into programming, maybe because your school finally started teaching a programming language (that too most probably during the last years of school life) and/or you surf online to find the cool stuff that people do with programming or you are fascinated by the knowledge and skill that experts have. Either
4 min read
Print "Hello World" in C/C++ without using any header file
Write a C/C++ program that prints Hello World without including any header file. Conceptually it's seems impractical to write a C/C++ program that print Hello World without using a header file of "stdio.h". Since the declaration of printf() function contains in the "stdio.h" header file. But we can easily achieve this by taking the advantage of C p
2 min read
Print Hello World without semicolon in C/C++
Every statement in C++ must end with a semicolon as per basics. However, unlike other languages, almost all statements in C++ can be treated as expressions. However there are few scenarios when we can write a running program without semicolon. If we place the statement inside an if/switch/while/macro statement with a blank pair of parentheses, we d
2 min read
Lex Program to remove comments from C program
Lex is a computer program that generates lexical analyzers. Lex reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in the C programming language. The commands for executing the lex program are: lex abc.l (abc is the file name) cc lex.yy.c -efl ./a.out Let’s see LEX program to accept string starting
1 min read
C Program to Print the Program Name and All its Arguments
The command-line argument (CLA) is the parameter provided in the system upon request. Command-line conflict is an important concept in system C. It is widely used when one needs to control your system from the outside. Command-line arguments are transferred to the main () path. Argc calculates the number of arguments in the command line and argv []
2 min read
C Program for Program to cyclically rotate an array by one
Given an array, cyclically rotate the array clockwise by one. Examples: Input : arr[] = {1, 2, 3, 4, 5} Output : arr[] = {5, 1, 2, 3, 4}Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. C/C++ Code // C++ code for program to // cyclically rotate an array by one # include &lt;iostream&gt; using namespace std; // Meth
2 min read
three90RightbarBannerImg