Hello World & Program Structure
This article explains the basic program structure of a C program, and you can follow this structure for any C programs you do.
Structure of a C program
Below is the structure for any C program. By writing C programs with this proper structure, you can avoid compilation errors.
- HEADER
- MAIN()
- VARIABLE DECLARATION
- BODY
- RETURN
Header Section
The header file is a file with the extension .h which includes function declaration and macro definition for C in-built library functions. stdio.h, stdlib.h, iostream.h, are some of the header files. Syntax: include
Main Method Declaration
The next part of a C program is to declare the main() function. The main () function is where the execution of the program gets started. It is the entry point of any C program.
Syntax : Int main()
Variable Declaration
The variable declaration refers to the variables that are to be used in the function. In the C program, no variable can be used without being declared before any operation in the function.
Syntax : int a;
Body Section
This section refers to the operations that are performed like printing, sorting, manipulations, searching, etc.
Return Statement
The last part is the return statement. The return statement returns the values from a function and the return value depends on the return type of the function. For example: for the void return type, there will be no return statement. For any other case, there will be a return statement and the return value will be based on the return type.
Let us look into an example with the basic structure of C programming and how to compile and run a program in coderseditor-online compiler.
[maxbutton id=”1″ url=”https://coderseditor.com/?id=88″ ]
#include<stdio.h> int main() { printf("Hello World!"); return 0; }
The above program includes all the sections for a perfect C program structure.
Now you can compile and run the program by clicking on the Run program button on the top or by pressing Ctrl + F9.
OUTPUT
Hello World!
The program runs with no error and hence the output is displayed. Hope, at the end you have got a clear idea about the C program structure.