C Program Structure

Program structure of a simple C program contains header files, main() function, and then program code. You will find easy in understanding the program structure of any simple C program. Let's start with C Hello World program which would print Hello World! on the output screen.

C Program Structure Example

Here is an example program, showing the general structure of a simple C program:

/* C Program Structure - This is the simplest
 * C program, prints Hello World! on the screen
 */

#include<stdio.h>
#include<conio.h>
void main()
{
   // This is single-line comment
   clrscr();
   /* This is a multi-line comment
    * This will print Hello World!
    */
   printf("Hello, World! \n");
   getch();
}

Here is the output of this C program:

c program structure

Above Example Explained

Here is the explanation of each line of the above program (Read carefully all the line):

  • Multi-line Comment - The first three line is multi-line comment, which will ignored by the compiler.
  • Preprocessor Command - The next line of the C program #include<stdio.h>; is a preprocessor command, which tells a C compiler to include stdio.h file before going to actual compilation to handle the standard input and output functions like printf(), scanf() etc like functions. printf() is used to put message on the output screen given by the programmer, and scanf() is used to scan to get the material which was entered by the user.
  • Preprocessor Command - The second line of the c program #include<conio.h> is also a preprocessor command, which tells a C compiler to include<conio.h> file before going to actual compilation to handle the getch() and clrscr() etc like functions.
  • Function - The next line void main() is a function from where the program execution begins. void tells the program to not return any values and main is that from where the program execution begins/starts.
  • Single-line Comment - The next line is single-line comment which starts from //.
  • Function - The next line clrscr() is a function which is used to clear the output screen since sometime in compiler like turboC++, there is already present their message or something other on the output screen which was set by the compiler's developer, and sometime we run our program second time or second program then the previous output is remain on screen and this will create some confusion so it is best practice to first clear the output screen to avoid confusion.
  • Multi-line Comment - The next line is also a multi-line comments, starts from /* and ends with */ which is ignored by the C compiler and it has been put to add the additional comments in program. So such lines are called as comments in the program.
  • Function - The next line printf(...) is another function available in C that causes the message "Hello, World!" to be displayed on screen since its work is to put message on the output screen according to the programmer.
  • Function - The next line getch(); getch() is used to hold the output screen in turboC++ like compiler if you will not use this function at the last of your program then after pressing run button your output screen comes and closed within fraction of seconds and you can't be able to watch your output. When you use getch() function, then your output screen will hold after all programming task will get finished and until you press any character, after pressing any character, your output will close and get back to your program .

Compile & Execute C Program

To compile and run the above C program, just download the C compiler named TurboC++. Here is the link to download this compiler
Download TurboC++
Now install this compiler, then open the compiler, and type the above code on it. After doing this, just press F9 to compile your program, and press Ctrl+F9 to run your program. You can also use this compiler to compile and run your C++ program.

More Example

Here are some more C program, that you can go for:

C Online Test


« Previous Tutorial Next Tutorial »