Hello World Program in C
Write a C Program for printing “Hello World”.
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.
In this approach, we print Hello World simply inside the main() function.
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; }
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.
In this case, we are just printing “Hello World”.
In this approach, we print Hello World using functions.
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() }
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.
Hello World