Hello World C Program

  • by

Hello World C Program is the simple and basic program in c language. The first program in c language is Hello World c program. This is the first program in every programming language. This program example is also used to explain the basic structure of the c program.
In this tutorial, you will learn Hello World c program with proper comments and explanation.

   Topics covered

  1. Simple Hello World c program
  2. How Hello World c program works?
  3. Hello World c program using Function
  4. Hello World c program using Character Variables
  5. Hello World c program using Character Array 
  6. Program to print Hello World in c many times using Loop
  7. Program to display Hello World Infinitely in c

You should have the knowledge of following C Programming topic to understand Hello World c program better –

  • C Input/Output (IO) 
c hello world program
Hello World C Program

Hello World C Program

This sample c program will display Hello World on the output screen. The program is explained properly with comments. 


/* Hello world c program */
#include <stdio.h> /* For printf() & scanf() */ 
#include <conio.h> /* For clrscr() & getch() */   
main() /* Starting point of the program execution */   
{
    clrsrc(); /* Clear output screen */ 
    printf("Hello World"); /* Display the string inside double quotes */    
    getch(); /* Hold output screen */ 
} /* End of main */   

Output –

Hello World

How Hello World C Program Works?

The instructions in C program are typed in lowercase but the variables and user-defined things can be written in uppercase.

1. Comment –

The first line of this program is the comment. Every comment starts with slash and an asterisk (/*) and ends with asterisk and slash (*/).

Single line comment
/* c Hello World program */

Multi-line comment
/* c hello world program
   Author: cfundamemtals.com
   Date: 5-4-2019 */

 A comment helps the programmer in explaining the logic of the program. It improves the readability of the program. These comments can be placed anywhere in the program.
The compiler does not read these comments. Nesting of comments is not allowed. Comments are the explanation of a statement in the program to improve the readability of the program.
Comments ate not necessary but it is a good practice to begin a program with a comment. It indicates the purpose of the program, its author and the date on which the program was written.
Any number of comments can be written at any place in the program. A comment can be written before the statement, after the statement or within the statement. The comment can be split over more than one line such a comment is called a multi-line comment.

2. #include –

Note: # is the pre-processor directive or compiler directive. #include is a pre-processor directive which is used to include header files ,i.e., #include <file-name.h>. The file which includes the definition of functions are called Header file. And those functions are called pre-defined functions.

The preprocessor #, as the name implies, is a program that processes the source code before it passes through the compiler.
The preprocessor is a collection of special statements, which are executed at the beginning of the compilation process. There are called directives.

The second and third line of the program #include <stdio.h> and #include <conio.h> are used to include two header files (“stdio.h” and “conio.h”). This statement directs the compiler to include header files in the program before compiling the program. We can also write <stdio.h> as “stdio.h”. Both are same.

Header file “stdio.h” contains the definition of functions printf() and scanf().
scanf() is used to take input from user and printf() is used to display output on the screen.

Similarly, header file “conio.h” contains the definition of functions clrscr() and getch().
clrscr() is used to clear the output screen and getch() is used to hold the output screen.

If you use printf(), scanf(), clrscr() and getch() without including their header files (“stdio.h” & “conio.h”) then the program will not be compiled.

3. main() function –

Every c program consists of one or more modules called functions. One of these functions is main(). The  execution of the program begins with main() function. Other functions in the program are called by main() function.
If you write more than one main() function in a c program then the program will not work properly.
The whole Hello World c program will be written in the main() function enclosed in curly brackets { }. All the statements inside the main() are slightly ahead than main(). This is called indentation. This shows that all the statements are inside the main() function.

The main() function is defined in many forms –

  • main()
  • void main()
  • main(void)
  • void main(void)
  • int main()
  • int main(void)

We mainly use void main(), main() or int main().

Empty parentheses means that that function will not take any argument or parameter. We can also represent by writing void explicitly in empty parantheses.
In void main(), the keyword void means that the function will not return any value. main() function will end with getch().

If you use int main(), then you will have to end the main() function with return(0) after getch().

int main()
{
       //program code
       getch();
       return(0);
}

Note: Every statement in the main() function ends with a semicolon (;)

4. clrscr() –

clrscr() is a pre-defined function. Its definition is written in the header file “conio.h”. clrsrc() function is used to clear the output screen so that the user can see the output properly.
For clrscr() function to work, you have to include “conio.h” header file.

5. printf(“Hello World!”) –

Note: printf() displays the string written in double quotes as output. Here, it displays Hello World as output.

printf() is a pre-defined function. Its definition is written in the header file “stdio.h”. printf() function is used to display output on the screen.
For printf() function to work, you have to include “stdio.h” header file.

6. getch() –

getch() is a pre-defined function. Its definition is written in the header file “conio.h”. getch() function is used to hold the output screen so that user is able to see the output of c program.
For getch() function to work, you have to include “conio.h” header file.

Hello World C Program using Function

Here we will print Hello World in c programming using user-defined function hello_world().

  • void hello_world(void) – function declaration above main()
  • hello_world() – function calling in main()
  • void hello_world() – function definition after main()

/* Hello World C Program using Function */
#include <stdio.h> //printf() & scanf() Header File
#include <conio.h> //clrscr() & getch() Header File     
/* function declaration */
void hello_world(void);
int main()
{
    clrsrc(); // to clear output screen 
    /* calling function */
    hello_world();     
    getch(); //to hold output screen 
    return(0);   
}
void hello_world()
{
    /* printf() displays the string inside double quotes */    
    printf("Hello World"); 
}    

Output –

Hello World

Hello World C Program using Character Variables

Here 7 character variables are used to print Hello World in c. There are many ways to run same program with different logic.


/* Hello World c program using Character Variables */
#include <stdio.h> //printf() & scanf() Header File  
#include <conio.h> //clrscr() & getch() Header File         
int main()
{
    char a = 'H', b = 'e', c = 'l', d = 'o';
    char e = 'W', f = 'r', g = 'h';
    clrsrc(); // to clear output screen 
    /* printf() displays the string inside double quotes */    
    printf("%c%c%c%c%c %c%c%c%c%c", a, b, c, c, d, e, d, f, c, g); 
    getch(); //to hold output screen 
    return(0);  
}

Output –

Hello World

Hello World C Program using a Character Array or String

If you are not able to understand this program then don’t worry. When you will become familiar with string in c programming then you will easily understand this Hello World c program code using string or character array.


/* Hello World c program using Character Array or String */
#include <stdio.h>  //printf() & scanf() Header File   
#include <conio.h> //clrscr() & getch() Header File      
int main()
{
    char s[] = {'H','e','l','l','o',' ','W','o','r','l','d',''};
    clrsrc(); // to clear output screen 
    /* printf() dislpays the string inside double quotes */    
    printf("%s", s); 
    getch(); //to hold output screen 
    return(0);  
}

Output –

Hello World

Program to Print Hello World in c many times using Loop

This Hello World c program displays the string “Hello World” as many times as the user wants. We will take input from user and using for loop we will print “Hello World” in c program.


/* Hello World c program using For Loop */
#include <stdio.h> //printf() & scanf() Header File   
#include <conio.h> //clrscr() & getch() Header File        
int main()
{
    int i, n;
    clrsrc(); // to clear output screen 
    /* to take number from the user */    
    printf("How many times you have to print Hello world in c: "); 
    scanf("%d", &n); 
    for(i=1; i<=n; i++)
    {
        /* printf() displays the string inside double quotes */    
        printf("Hello World"); 
    }
    getch(); //to hold output screen 
    return(0);  
}

Output –


How many times you have to print Hello World in c: 5
Hello World
Hello World
Hello World
Hello World
Hello World

Program to Display Hello World Infinitely in C

In this program as the condition inside while loop while(1) will always be true so the loop will run forever ,i.e., infinite times. And Hello World string will print infinite times.


/* Print Hello world in c program Infinitely */
#include <stdio.h> //printf() & scanf() Header File 
#include <conio.h> //clrscr() & getch() Header File        
int main()
{
    clrsrc(); // to clear output screen
    while (1) 
    {
        /* printf() displays the string inside double quotes */    
        printf("Hello World"); 
    }
    getch(); //to hold output screen 
    return(0);  
}

Output –

Hello World

Other c programs examples: