Like in most of the programming languages, program to write the text "Hello, World!" is treated as the first program to learn in C. This step-by-step guide shows you how to create and run your first C program.
Step 1: Setting Up Your Environment
Before proceeding to write the first program, the user needs to set up a C program compiler, which would compile and execute the first 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 2: Creating a Source Code File
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.
HelloWorld.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;
}
Step 3: Navigating to the Source Code Directory in Terminal or Command Prompt
This includes opening CMD or command prompt line and navigating to the directory where the file HelloWorld.c is present.
For Example,
cd C:\Users\Chin\Sample
Step 4: Giving GCC Compiler to Generate the Executable File
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 5: Run the Executable File
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”
- This is a single comment line. A comment is used to display additional information about the program.
- 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.
- Any line beginning with ‘//’ without quotes OR in between /*…*/ in C is a comment.
More on Comments in C
Line 3:
#include
- In C, all lines that start with the pound (#) sign are called directives. These statements are processed by preprocessor program invoked by the compiler.
- 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()
- 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.
- { 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");
- 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.
- 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;
- 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.
- 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.
Similar Reads
Misc C Programs C Program to print environment variablesC Program to Swap two NumbersC program swap two numbers without using a temporary variableC Program to check if a given year is leap yearC Program to sum the digits of a given number in single statement?C program to print numbers from 1 to 100 without using lo
1 min read
C Hello World Program The âHello Worldâ program is the first step towards learning any programming language. It is also one of the simplest programs that is used to introduce aspiring programmers to the programming language. It typically outputs the text "Hello, World!" to the console screen.C Program to Print "Hello Wor
1 min read
String C/C++ Programs C program to swap two StringsC Program to Sort an array of names or stringsC Program to Check if a Given String is PalindromeC/C++ Program for Return maximum occurring character in the input stringC/C++ Program for Remove all duplicates from the input string.C/C++ Program for Print all the duplicate
3 min read
C/C++ Programs sArray C/C++ ProgramsC Program to find sum of elements in a given arrayC program to find largest element in an arrayRecursive C program to linearly search an element in a given arrayC program to multiply two matricesC/C++ Program for Given an array A[] and a number x, check for pair in A[] with sum
15+ min read
C Programs To learn anything effectively, practicing and solving problems is essential. To help you master C programming, we have compiled over 100 C programming examples across various categories, including basic C programs, Fibonacci series, strings, arrays, base conversions, pattern printing, pointers, and
8 min read