C Hello World Program

  • A simple C program to print Hello World string on screen. It is often the first C program of new programmers used to illustrate the basic syntax of a C programming language.

We are going to learn a simple “Hello World” C program which will give us a brief overview of how to write and compile a c program. Before that, we have to understand the structure and fundamental essential components of a bare minimum C program.

Components of a C Program

  • Preprocessor Commands : The preprocessor step comes before compilation of c program and it instruct the compiler to do required pre-processing before actual compilation.
  • Main Function : This is the function from where execution of any C program starts.
  • Comments : You can add comments and documentation in your source code, which will not be executed as the part of program. It helps others to better understand the logic of your code.
  • Variables : A variable in C is a named memory location where a program can store and modify the data.
  • Statements & Expressions : Statements and Expressions are used to implement the core logic of the C program(For Example: Sum = A+B;).

C Program to Print “Hello World”

Printing “Hello World” program is one of the simplest programs in C programming languages. It became the traditional first program that many people write while learning a new programming language.

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

Output

Hello World

How to Write and Compile a C Program

  1. Write
    • Open a text editor and type the above mentioned “Hello World” code.
    • Save this file as HelloWorld.c.
  2. Compile
    • Open command prompt and go to your current working directory where you saved your HelloWorld.c file.
    • Compile your code by typing gcc HelloWorld.c in command prompt. Your program will compile successfully, If your program doesn’t contain any syntax error.
    • It will generate an a.out file.
  1. Execute
    • Now run your program by typing a.out in command prompt.
  1. Output
    • You will see “Hello World” printed on your console.

Description 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 informing operating system that program successfully completed, whereas returning 1 means error while running program.

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.
  • All variables must be declared with respective data types before their first use.
  • 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.