Hello World Program in C

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

Problem Description

Write a C program to print “Hello World”.

Problem Solution

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

There are various ways to print “Hello World” in the C language. Let’s explore the different approaches for printing “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.

advertisement
advertisement
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”.

Hello World

advertisement
Method 2: Printing Hello World in C using Function

In this approach, we print Hello World using functions.

Program/Source Code
/*
 * 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

To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.

advertisement

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.