Hello World Program in C

 

Hello World Program in C



Hello World Program in C: Printing “Hello World” is the first step we follow in learning any programming language. Printing “Hello World” introduces us to various terminologies associated with a programming language.

Problem Description

Write a C Program for printing “Hello World”.

Problem Solution

1. Declare the header files required for printing “Hello World”.
2. Declare the main function for starting program execution.
3. Inside the main function print “Hello World”.


There are several ways to print hello world in the C language. Let’s take a detailed look at all the approaches to print hello world in C.

Method 1: Printing Hello World in the main() Function

In this approach, we print Hello World simply inside the main() function.

Program/Source Code

Here is the source code of the C program for printing “Hello World” in the main() function. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
 * C program to print “Hello World”
 */
 
#include <stdio.h>
 
//main function
int main() 
{
    //print Hello World
    printf("Hello World");
 
    return 0;
}


Program Explanation

1. #include<stdio.h>:

  • #include is a preprocessor command that loads the header file stdio.h.
  • stdio.h is a header file that contains scanf() and printf() functions for input and output respectively.

2. int main(): used to declare a function main of integer return type. The execution of the program starts from the main() function.
3. printf(“Hello World”): printf() function is an inbuilt library function that is used to display output on the screen. The message Hello World will be displayed on the screen.
4. return 0: This is an exit statement. This line simply ends the program.

Program Output

In this case, we are just printing “Hello World”.


Method 2: Printing Hello World in C using Function

In this approach, we print Hello World using functions.

Program/Source Code

Here is the source code of the C program for printing “Hello World” using functions. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
 * C program to print “Hello World” using function.
 */
 
#include <stdio.h>
 
//function print() to print Hello World
void print()
{
    printf("Hello World");
}
 
//main function
void main() 
{
    print(); // calling the function print()
}
Program Explanation

1. All the terminologies used here is same as the method 1.
2. The only difference is, instead of printing Hello World in the main function we created a function named print where we printed the Hello World.
3. The program execution always starts from the main() function so the function print() is called from the main function to execute and print Hello World.
4. The print() function is of void return type which means it does not return any value to the main function.

Program Output
Hello World

Brand creation, trend analysis & style consulting

Our content is highly curated—in partnership with industry leaders from Google, Amazon, and Microsoft—to advance expertise in data science, ML, cloud computing, cybersecurity, and more. From our individual courses to our highly valued Nanodegree credential, each program is deeply focused on a single concept, eliminating guesswork in selecting the right course to overcome talent gaps.

Contact