C - Hello World Program

C Programming

Share

In this tutorial we will learn about the basic structure of a C program.

Open your favourite IDE and lets write a C program to print Hello World.

Save the file as helloworld.c or any other filename that you like.

#include <stdio.h>

int main(void) {
  printf("Hello World");
  return 0;
}

Now if we compile and run this code we will get Hello World as output.

Lets talk about each line of the program.

#include Directive

The first line of the program #include <stdio.h> is a preprocessor directive which instructs the compiler to include stdio.h header file from the C library for input/output.

C programs are generally divided into modules or functions. Some of these functions are written by us programmers and some of the functions are stored in the C library.

Functions stored in C library are grouped in categories and saved in files known as header files.

Header files are saved with a .h file extension.

Note! preprocessor directives are added at the beginning of a program.

The main function

Next we have the main() function which is a special function in C programming as it tells about the starting point of the program.

Every C program must have only one main function.

If we create more than one main() function in a C program then we will get an error as the compiler will not know which one represent the starting point of the program.

What is a function?

A function in C is a block of code that can take in some value, perform some tasks and can return a value.

We represent the start and end of a function block using the opening and closing curly { } brackets.

int main(void) {

}

Function code is written inside the opening and closing curly brackets.

Return type

Functions can return value and the int keyword before the main function tells us that the main function can return integer value.

If a function is not going to return any value then we use the void keyword.

We will learn about functions in the Functions tutorial.

Function Argument

Within the opening and closing parenthesis ( ) of a function we pass some values.

In the above code we have main(void) which means the main function takes no value.

void is a special keyword.

The printf function

The printf() function in C is used to print output. It is a standard C function.

So, in the above code we are printing a string message that is enclosed within double quotes "Hello World".

The semicolon ;

Every sentence in English ends with a full stop . similarly, every statement in C ends with a semicolon ; mark.

So, in the above program we end the statement with a semicolon.

printf("Hello World");

return 0;

The last line inside the main function is a return statement.

Since the return type of the main function in the above program is set to int so, we are returning an integer value 0 at the end of the function.

We will learn about return values and functions in the Functions tutorial.

Share

Recently Updated