C Hello World Program

C Hello World Program

  • Write a Program in c to print Hello World string on screen

Printing “Hello World” program is one of the simplest programs in C programming languages. It become the traditional first program that many people write while learning a new programming language. Hello world program helps in understanding basic syntax of C programming language and C program structure to novice programmers.

C Program to print Hello World

C Program to print Hello World 1

/*
* C Program to Print "Hello World"
*/
#include <stdio.h>
 
int main(){
    printf("Hello World");
    return 0;
}

Program Output

Hello World

Various parts of Hello World program

  • First line is program is a comment placed between /* and */. Comments are used in a C program to write an explanation or for documentation purpose. When a program becomes very complex, we need to write comments to mark different parts in the program. These comments will be ignored by the compiler at compilation time.
  • The next line #include is a preprocessor directive to load the stdio.h(Standard Input/Output header file) library. This library provides some macros and functions for input or output which we can use in our program(like printf and scanf). We need stdio for printing “Hello World” using printf function.
  • The next line is int main(){.The int is what is called the return value. Every C program must have a main() function, and every C program can only have one main() function where program execution begins. The two curly brackets {} are used to group all statements together.
  • The next line is printf(“Hello World”) which is used for printing “Hello World” string on the screen.
  • The next line is return 0. int main() declares that main must return an integer. Returning 0 means we are saying to Operating System that program successfully completed, whereas returning 1 says error while running program.

C Program to print Hello World multiple times using loop

For loops are used to repeat a sequence of statements multiple times till a condition is satisfied( here condition is “counter < 10”). We initialize counter variable with 0 and increment it after every iteration till it’s value is less than 10.

C Program to print Hello World multiple times using loop 1

/*
* C Program to print "Hello World" 5 times using for loop
*/
#include <stdio.h>
 
int main(){
    int counter;
    for(counter = 0; counter < 5; counter++){
        printf("Hello World\n");
    }
    return 0;
}

Program Output

Hello World
Hello World
Hello World
Hello World
Hello World

C Program to print Hello World using a function

C Program to print Hello World using a function 1

/*
* C Program to Print "Hello World" using function
*/
#include <stdio.h>
  
void printHelloWorld();
int main(){
    printHelloWorld();
    return 0;
}
/*
 *  Function to print "Hello World"
 */
void printHelloWorld(){
    printf("Hello World\n");
}
</stdio.h>

Program Output

Hello World

Don’t worry if you didn’t understand above code as you may not be familiar with functions yet.

Points to Remember

  • A comment starts with /*, and ends with */.
  • Some header files must be included at the beginning of your C program.
  • Every C program should have only one main() function where program execution starts.
  • Every statement must end with a semicolon.
  • Main returns an integer to Operating system informing about it’s termination state. Whether program executed successfully or an error has occurred.