logo

Hello World Program in C Language (4 Ways)

The Hello World program in C is the first program beginners write when starting with the basics of programming. It introduces the essential structure of a C program, including how to use the printf() function to display text on the screen. 

In this tutorial, we’ll walk you through writing and running your first Hello World in C programming. You’ll learn how a basic C program works, see the actual output, and understand each part of the code, setting a solid foundation for more advanced topics.

Coders ki Tipaniya:

Career ki pehli coding job tab milegi, jab tumhara pehla ‘Hello World!’ screen par aayega.

Option 1: Hello World Program in C

#include <stdio.h>  

int main() {
    printf("Hello, World!");  
    return 0;  
}

Output:

Hello, World!

Explanation:

Let’s break down the C program to print Hello World in simple terms:

  • #include <stdio.h>: This is a preprocessor directive that includes the Standard Input Output library in C. This library is essential for using functions like printf() to display output. Without this line, the C compiler wouldn't know how to handle the printf() function.

  • int main(): The main function is the starting point of any C program. It tells the compiler where the program begins. The int indicates that the function will return an integer value (usually 0 or 1) to the operating system after the program executes.

  • printf("Hello, World!");: This line is the core of the program, where we use the printf function to display the text Hello, World! on the screen. The text inside the quotes is what will be printed.

  • return 0;: This line tells the program to end successfully. In C, the main() function returns an integer value. Return 0 indicates that the program has executed without errors.

Option 2: Print Hello World in C Using puts()

#include <stdio.h>  

int main() {
    puts("Hello, World!"); 
    return 0;  
}

Output:

Hello, World!

Code Explanation:

Let’s break down this C code for Hello World using the puts() function:

  • #include <stdio.h>: This line includes the Standard Input-Output library, which provides access to functions like puts() and printf().

  • int main(): The main function is the entry point of the program where execution begins. In this case, it starts and ends with the simple task of printing Hello, World!.

  • puts("Hello, World!");: The puts() function is used to print a string followed by a new line. Unlike printf(), you don’t need to add \n for a new line, as puts() automatically moves the cursor to the next line after printing.

  • return 0;: This tells the program to exit successfully. 

Option 3: Hello World Code in C Using Escape Characters

#include <stdio.h>  // Including standard input-output header

int main() {
    printf("Hello,\nWorld!"); 
    return 0;  
}

Output:

Hello,
World!

Explanation:

printf("Hello,\nWorld!");: Here, the \n is an escape character used to insert a new line. This means that after printing Hello,, the cursor moves to the next line, and then World! is printed.

This version of the Hello World program in C language shows how escape characters like \n can be used to format output by moving text to a new line.

Option 4: Hello World in C by Printing Character by Character

#include <stdio.h> 

int main() {
    printf("%c%c%c%c%c, %c%c%c%c%c!", 'H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd');  
    return 0;  
}

Output:

Hello, World!

Explanation:

In this Hello World program in C, %c is a format specifier used for printing individual characters. Each character of the phrase Hello, World! is passed separately to printf(), and it prints them one after the other to form the complete sentence.