C Hello World Program
Learn via video course

Overview
Hello, World! is the very first output on the screen of a programmer. It is a way of saying "Hello" to the "World" of programming. Also, C Language is majorly the very first language of every programmer and the first program that we use to write in C Language is known as a Hello, World! program.
Scope
- This article is all you need to write your very first Hello World Program in C.
- In this article we will understand the process that takes place when we compile our code.
- This article contains different types of program to display Hello, World! in the output console.
Introduction
Hello, World! is a very basic entry-level program that every programmer learns in the beginning of a programming language. It is a program in which we display Hello, World! message on the output screen.
There are so many programming languages serving different purposes that they are made for, for example, C, C++, Java, Python, Scala, R, C# etc.
As we are looking at C Language, It is a high-Level language that a human can easily understand but for converting a human-understandable language into a machine understandable language we need a Compiler in our system. The compiler also helps us to find the errors in a human-written C Program.
Compiling our First C Program
We can write our C Programs in the software called IDE (Integrated Development Environment). There are so many IDEs out there, for example, Visual Studio Code, Code::Blocks, Eclipse, CLion, Netbeans, Dev C++ etc. that supports writing and compiling C Language programs.
Code::Blocks is a user-friendly IDE to write C and C++ Language programs for beginners. You can download it here and perform the basic steps to install the IDE. It is available for Windows, Linux and MacOS.
Compilation is a process of converting Human Understandable (High Level) Code into Machine Understandable (Low Level) Code.
C Programs have an extension of .c, and after compilation of our program we get an executable file with an extension of .exe.
These files are machine-dependent and as there are so many different types of operating systems and compilers, we have to make sure to run our .exe file on the system where it has been compiled.
Let us now look at the process of how the compilation process generally works in a C Program.
Explanation:
- First, we have a written C Program file with an extension of .c.
- There is a pre-processor in the compilation process which is used to add the header files content into our program. Header-files are pre-written code files that we can use directly in our programs using the #include statements.
- All the statements starting with # are replaced during the compilation process with the help of a Pre-processor with some pre-written code present in our header file.
- Now, Pre-processor generates an intermediate file with .i extension including the code of header files, then the file is passed to the compiler.
- Compiler software translates the hello.i to hello.s file having assembly level instructions.
- Assembly level code instructions are converted into a machine-understandable code by the assembler and the file generated is known as the object file (with an extension of .obj).
- There are some unknown statements written in the object file that the Operating System can't understand. You can see this as a book having some words that you don't know, you will use a dictionary to find the meaning of those words, similarly, we use Library Files to give meaning to some unknown statements from our object file.
- In the compilation process, Linker is used to link the library files with the object file to define the unknown statements.
- Now, we get an executable file (.exe) that we can run on our system.
Now, let us see how we can write a program using C Language in our IDE.
C program to Print Hello World
We are going to apply the above procedure to compile our program and display Hello, World! in the output console.
Let us see our first C program to display Hello, World!.
Output:
Explanation:
- First, We have included a stdio.h header file, it enables us to use standard input/output functions in our C Program.
- If we do not include the stdio.h header file in our program, we can not use input / output functions like printf() and scanf().
- main() function is the main block of code where the execution of our program begins.
- Inside the main() function we have two statements, printf("Hello, World!") and return 0.
- printf() function is used to display the string inside it into the output window.
- printf("Hello, World!") will print Hello, World! (without quotes "") in the output window.
- return 0 will return 0 to the operating system and shows that execution went successfully without any errors. This is the last statement of every program.
- Any non-zero value in return statement (Ex. return 1) is considered as an unsuccessful execution of the program.
- main() function is supposed to return a value to the OS and after returning some value, the program finishes its execution.
Fun Fact: printf() function returns the length of the string passed into it for printing in the output console.
Hello World Program in C Using Functions
Functions are the building blocks of code in a program. These are a group of programming statements that can be used again and again easily by just calling the function by its name in some other function.
Syntax to define a function:
Example:
Let us see a program to print Hello, World! in the output console using function.
Output:
Explanation:
- We have defined a hello_world() function with return type void.
- void is a returning type in which hello_world() function do not return any value.
- Inside the hello_world() function, a printf() statement is used to print Hello, World! on the output screen.
- When we call the hello_world() function in the main() function, the program control will shift from main() to hello_world(). It will print Hello, World! on the output screen and after the execution of the hello_world() function, the control will return to the main() again where it left the control.
- main() returns 0 to the system and execution stops.
Hello World Program in C Using char Variables
We can use char data type variables to display Hello, World! in the output. char data type variables are used to store a single character value (ex, a, e, i, o, u, !, @, $, ~ etc.) in them. In this program, We will use format specifiers (%c for char variables) in the printf() function to print the message on the output screen.
A string is just a collection of characters. So, we can say Hello, World! is just a string made of different char variables. We can easily print Hello, World! in the output using char variables.
Syntax to declare variables in C:
Syntax to declare char variables:
or
Now, Let us see the program to display Hello, World! in the output console using the char variables.
Output:
Explanation
- First, we have included the stdio.h header file, so that we can use standard input / output functions like printf(), scanf() in our program.
- Now, we have defined the main() function with the integer return type. In the main() function we have declared 9 different variables with different characters from the string Hello, World!.
- char a = 'H', b = 'e', c = 'l', d = 'o', e = ',', f = 'W', g = 'r', h = 'd', i = '!';
- Format specifiers are used to print variable's value in the output using the printf() function. There are specific format specifier for specific data type like %d for integer values, %f for float values, %c for characters values, %lf for double values etc.
- We have passed 13 arguments in printf() function to print Hello, World!, first is the string with format specifiers and the rest are the names of the variables in a sequence of required output (Hello, World!).
- Format specifiers acquire the values of variables in sequence as passed in the printf() function. So, The above statement will print Hello, World! in the output console.
- At last, main() function returns 0 to the system and execution stops.
How Hello World Program in C Works?
- First, there is a #include <stdio.h> statement, it is a pre-processor command used to add methods written in standard input/output file into the program.
- stdio.h header file contains all the input and output functions like prinf(), scanf(), puts(), gets() etc.
- Execution of every program begins with the main() function.
- The easiest program that we have seen to print Hello, World! is using the printf() function and passing a Hello, World! string that directly prints Hello, World! in the output console.
- Next, we can use a function to print Hello, World! in the output.
- We can also use char variables in our program to print Hello, World! string in the output.
- return 0; is the program exit statement. Execution of the program stops when compiler encounters return 0; statement in main() function.
Conclusion
- We have started our journey in programming and just wrote our first C program.
- We know how the compilation and execution works in a C Program.
- We know different ways to write Hello World Program in C Language on our system.