Structure of C Program
Structure of C Program
Even program in C language are written using different compilers, a good programmer follows a pre-planned structure to write a program. One can have their own way of writing programs. In this article I have summarized a simple structure for writing a program in C.
Structure can be started with writing comments on what type of program you are going to write and for what use. Comments are then followed by the standard sequence of writing C programs. I have prepared a block diagram to explain the structure of C program that goes from top to down.
Let us start with writing comment and we will go through some compulsory portions and other basics.
- Writing comments:
Comments in C programming as well as in other programming language are used to give some description on what we are writing, how does it works, change in variables and so on. The way of writing comment may differ according to the language adopted. In C program, we can write comment in two ways.
For writing comments in a single line, one can use //’ followed by comments. For example:
//binary search function starts form here
For writing a block of comments with many lines, /* comment */’ syntax is used. For example:
/* This program is written by TheComputerStudents.com and its Copyright is hold only by TCS. This programs is prepared as an example of writing comment in C programming */
- Header File Inclusion:
In this section header files are written. This section is also in called preprocessor derivatives. The line that begins with # is known as preprocessor derivatives. Each C program must start with proper header files (e.g. <stdio.h>) starting with #’ sign, include’ and a header file name enclosed within triangle brackets. Header files have .h’ as extension. Example of declaring header files in C program are:
#include<conio.h>
#include<stdio.h>
#include<graphics.h> - Function Declaration:
Besides main() function, programmer can build many other function for each block of instructions. Declaration of such function is done just after declaring header files. While declaring function the return type of the function is also given with the type and number of arguments/parameters. Example of declaring function is:
int addition( int x, int y);
This declaration explains that a function with name addition’ is declared with two integers as arguments and integer as return type. - Starting of main function:
A program may contain any number of functions or sub-programs, but the first module (sub-program or function) which is executed at first is the main()’ module. It is the compulsion function in C program, form main() function many other function and sub-functions are linked. Starting of the main function is done as:
void main()
{ - Variable declaration:
Variable are the memory slots that holds values for performing some operations. To declare variables we must have awareness of variable name convention, data type and syntax for declaration, etc. example of variable declaration:
int a,b,c;
//or can initialize value at the time of declaration as:
int a=20, b=40, c; - Message for accepting values:
Message are to be displayed after compilation of the program so as to employ user to perform some inputs and fetch some function from the selection. To display any values or messages into the screen or to file we have several functions called output functions. The functions are given below:
printf(), sprint(), cprintf(), puts(), putw(), fwrite(), fprintf()
, etc.
The syntax of printf() is given below:
printf(“message/scan format”,[list of variables]);
The scan format varies upon the type of variable. The following statement will show user a message that prompts the user to enter two values through keyboard.
printf(“ Please enter two values : ”);
- Input from the Keyboard:
Values, numbers, character and text can be input in programming. These inputs are to be entered form keyboard for performing processing in C program. To input such data into the program, C supports a numbers of functions called input functions which are given below:
, etc.
scanf(), gets(), getch(), getchar(), getc(), getw()
The syntax of scanf() is given below:
scanf(“scan format”,&variables);
The following example will accept two integer values from keyboard and store to variables a and b.
scanf(“%d%d”,&a,&b);
- Process according to the problem given:
When some process are to be performed in C program, the operation for that process is written in a line. i.e. for addition, division, multiplication, subtraction and so on. If we need to multiply two numbers then we need to have concept of operators and expression. Here we need arithmetic operator *’ for multiplication.
c = a * b;
- Show output on monitor:
After performing arithmetic operation or other operations certain output is obtained and is to be displayed on the monitor. The statement for displaying the output on the monitor is:
printf(“The product of %d and %d is = %d”,a,b,c);
- Closing the main function:
When writing of program is at end it need to be closed. Remember starting main function with { , don’t you? Now you have to close the main() function by closing brackets }’.