Home C Language Basic Structure of C Program

Basic Structure of C Program

Structure of c program

In a basic C program structure the following things are involved.

  • Preprocessor declaration (Header files, etc.)
  • User-defined function prototype declaration (if any)
  • Global variable declaration (if any)
  • Main function body
  • User-defined function body (only declared function bodies are build here)

A simple structure of c program

Header files

main( )

{

statement1;

statement2;

statement3;

}

ex:

  1. /* C language sample program*/
  2. #include<stdio.h>
  3. #include<conio.h>
  4. main()
  5. {
  6. clrscr();
  7. printf(“Welcome to C language programming”);
  8. getch();
  9. }

Output:

Welcome to C language programming

Line 1: It is comment line. Comment lines start with /* and terminates with */. Any number of lines included in the block of /*…..*/ are marked as comments and these lines are ignored (not translated into machine language) by the compiler while compiling the program. Comments can be present anywhere in the program and they are meant for documentation purpose alone.

Line 2 & 3: #include<stdio.h>,#include<conio.h>  is a preprocessor declaration. Lines starting with # are called as preprocessors because they are processed before the program is processed. stdio.h stands for Standard I/O header file, which contains the definitions of input and output statements such as printf, scanf, etc. conio.h stands for Constant I/O header file, which contains the definitions of constant input and output functions like clrscr(), getch(), etc.

Line 4: main() is special function used by C system. When we start the program execution the process will begin from main onwards. Each program have only one main() function. Basically, a C program is composed of a number of different library and user-defined functions. There is no limitation to the number of functions a program can contain. However, a program should have one main() to execute it. The main() function calls the other functions in the program and the main() will called by operating system. Body of a function(main or other user-defined) should written between the curly braces({}).

Line 6: clrscr(), this function is used for clear the previous output screen. Generally this function is used in first line of the program statement.

Line 7: printf() is a predefined standard output function to print any message or result on the output screen. If we want to print a statement it should be written between quotation marks (“”). Not only static statements we can also print dynamic values also we will discuss about it in later chapters.

The General syntax of printf() is :

printf(“message”);

ex:

printf(“C language programming”);

Line 8:

getch(), this function used to stop the output screen until user press any key from keyboard. This is able to read a character from user (those character will not store anywhere in the program). This function is used to control the flow of program execution.