The Fun Begins: Basic Structure of a C Programme-With PDF

Dear readers, here you will learn to write Basic Structure of a C Programme to display “Hello, World!” on the screen. Since, it’s a very simple program, it is often used to illustrate the syntax of a programming language.

# Why “Hello, World!” program?

“Hello, World!” is a simple program that displays “Hello, World!” on the screen. Since, it’s a very simple program, it is used to illustrate the basic syntax of any programming language.

Yeah ! Obviously you can write any sentence instead of  “Hello, World!” .
 

This program is often used to introduce programming language to a beginner. So, let’s get started.

#include <stdio.h>
int main()
{
    printf("Hello, World!\n");
    return 0;
}

# How “Hello, World!” program works?

You have to remember the following 4 steps to write a “Hello, World!” program in C programming language.

  1. Include stdio.h header file in your program

  2. The main() function

  3. The printf() function

  4. The return statement

# Include stdio.h header file in your program

C programming is small and cannot do much by itself. You need to use libraries that are necessary to run the program. The stdio.h is a header file and C compiler knows the location of that file. To use the file, you need to include it in your program using #include preprocessor.

# Why do you need stdio.h file in this program?

In this program, we have used printf() function which displays the text inside the quotation mark. Since printf() is defined in stdio.h, you need to include stdio.h.

# The main() function

In C programming, the code execution begins from the start of main() function (doesn’t matter if main() isn’t located at the beginning).

The code inside the curly braces { } is the body of main() function. The main() function is mandatory in every C program.

int main() 
{
}

This program doesn’t do anything but, it’s a valid C program.

# The printf() function

The printf() is a library function that sends formatted output to the screen (displays the string inside the quotation mark). Notice the semicolon at the end of the statement.

In our program, it displays Hello, World! on the screen.

Remember, you need to include stdio.h file in your program for this to work.

# The return statement

The return statement return 0; inside the main() function ends the program. This statement isn’t mandatory. However, it’s considered good programming practice to use it.

# Key notes to take away

  • All C program starts from the main() function and it’s mandatory.
  • You can use the required header file that’s necessary in the program. For example: To use sqrt() function to calculate square root and pow() function to find power of a number, you need to include math.h header file in your program.
  • C is case-sensitive; the use of uppercase letter and lowercase letter have different meanings.
  • The C program ends when the program encounters the return statement inside the main() function. However, return statement inside the main function is not mandatory.
  • The statement in a C program ends with a semicolon.

Download this article as PDF

We hope you enjoyed to read this article. If you have any questions, please comment below!

Previous                                Main Menu                                Next Page

Leave a Reply

Your email address will not be published.