Hello World Program in C

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

"Hello, World!" is the very first message most programmers see when they start coding. It's like saying "Hi" to the world of programming. This message usually comes from a simple program called the "Hello World in C" program, which is often the first thing people learn to code in the C language.

C Program to Print Hello World

We will apply the above procedure to compile our program and display how to print Hello World in c.

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 forget to include the stdio.h header file in our program, we won't be able to use basic 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.

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.

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.

Compilation of C program

Let us now look at the process of how the compilation process generally works in a C Program.

C Program Compilation Process

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.
  • 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).
  • 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.

Hello World code in C Using Functions

Functions serve as the fundamental components of a program, forming its structural framework.. 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 code in c 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 code in C Using char Variables

We can use char data type variables to display hello world in c. 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'll utilize format specifiers (%c for char variables) within the printf() function to display the message on the screen.

Syntax to declare variables in C:

Syntax to declare char variables:

or

Now, Let us see the program to which shows how to print hello world in C using the char variables.

Output:

Explanation

  • 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!).
  • printf("%c%c%c%c%c%c %c%c%c%c%c%c", a, b, c, c, d, e, f, d, g, c, h, i);.
  • 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.

How Hello world in C Program 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 in C 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

  • "Hello, World!" marks the beginning for many programmers, symbolizing their entry into the world of coding.
  • The "Hello World in C" program is often the initial code learners encounter, introducing them to the basics of C language syntax.
  • Hello world in C can be printed using a simple printf() statement within the main() function.
  • Understanding the compilation process is crucial, as C programs need to be translated into machine-understandable code before execution.
  • Functions can be utilized to encapsulate code for reusability, offering an organized approach to programming.
  • Alternatively, char variables can be employed to print each character of "Hello, World!" individually, demonstrating another way to achieve the same output.