Structure of C language program - Tech students
 
 

Structure of C language program

          structure of C language program


          To write programs in C one should you follow the basic structure of the C Program.
Structure of C

          Structure of a C program includes various sections/parts. All these sections may or may not be used in all the programs.

But to write complete C program that can be easily understood by programmers one should use some or all the sections based on the program complexity and functionality. Simple programs may use a few sections and complex program may use all the sections of the structure.

To understand the concept let us begin with a sample program with only sections.


learn simple programe click here

1.Documentation Section : Gives idea about program
/*Documentation section of Welcome program
Written to all Diploma Students of Computer Engineering Branch
Author: Shaik jeelan  Govt. Polytechnic for Minorities. Kurnool*/

2.Link Section: To include Header files
#include  /* link Section */

3. Main Function Section: To start the execution of a C program.
int main() /*Main Function */
Now combine the above three section/parts to make a complete C program.

/*Documentation section of Welcome program Written to all Diploma Students of Computer Engineering Branch
Author: shaik jeelan  Polytechnic for Minorities, Kurnool*/
#include  /* link Section */
int main() /Main Function */
output :
Hello DCME students ! Hearty Welcome to C Programming World

Note : Only Link and Main function sections are compulsory, Documentation Section is Optional.

          Now let us consider a complex program that includes a main function and also user defined function to find the sum of two numbers to understand all the sections parts of a C program.

Note : Do not worry about the program functionality at this time of point. But to know how to include all the sections of a C program this example is considered. You will understand this program at latter stage clearly.


          The various sections/parts of a C program are
1. Document Section: Gives the Brief explanation of the Program, author, date written etc.
2. Link Section :Includes header files with # include statements.
3.Definition Section: Declare variables and initializes them using #define.
4. Global Declaration Section: The variables used throughout the program are declared.
5. Function Prototype Declaration section: User defined functions are declared here.
6. Main Function: Main function is written here to start the program execution.
7. User Defined Function Section: The bodies of all the user defined functions are defined here.
Let us see all these sections with example in detail.

1. Documentation section: (Optional) It includes the comments about the program, creation or modified date, author name etc. in this section. The characters or words or something that area unit given between "/*" and "*/" could be a comment. The C compiler will never consider this for compilation process.
Syntax :/* comment line1
comment line2
comment line3*/
Example /*C :: program structure
Documentation section
Author : shaik jeelan Polytechnic for Minorities Kurnool Date 06/06/2014*/

2. Link Section : (Optional) Header files that are required to execute a C program are included in this section with the s syntax.
Syntax : #include <header filename>
Example: #include <stdio.h>
Note : header files and their extensions shall be discussed later

3.Definition Section : during this section, variables are defined and values are set to these variables.
Syntax: #DEFINE VARIABLE VALUE
Example : DEFINE pi 3.14159

4.Global declaration section : world variables area unit outlined during this section. When a variable is to be used throughout the program, they are called as global variables.
Syntax: type variable-name-value:
Example: int total=0;

5. Function prototype declaration section(Optional): Function prototype gives much information about a function like return type. parameter names used inside the function.
Syntax: return-type function-name(type1, type2..)
Example: int sum (int, int)   /*function to add two integers and gives integer result */

6. Main function : Every C program execution starts from main function and this function contain two major sections called declaration section and executable section.

Syntax: int main()
{
Declaration section
Executable section
Return 0;
}

7. User defined function definition section: User can define his own functions in this section which perform particular task as per the user requirement.
Syntax :
return-type function-name(type1, type2...)
Example: int total (int a, int b)  /*User defined function*/
/*C basic program structure Documentation section Author: K.V. Nagabhushanam, HCMES. Govt. Polytechnic for Minorities. Kurnool
Date:05/05/2014
*/
#include /* Link section */
#DEFINE pi 3.14159 /* Definition section*/
int total = 0 /*Global declaration and definition section*/
int sum (int , int); /*Function declaration section*/
int main() /*Main function*/
{
int x,y; /*Declaration section*/
printf("This could be a C basic program to feature 2 numbers \n");  /* Execution section here*/
x=1;
y=8;
total = sum (x,y);
printf ("Sum of two numbers : %d\n", total);
return 0; /*Execution section ends here*/
}
int sum (int a, int b) */user defined function */
returns;
}
Output :
This is a C basic program to add two numbers
Sum of two numbers: 9

I hope you my dear friends your understood the structure of C language any problem question comment on below. And share your friends to know the structure of C program. And more friends C programmes I started check out it. Thanks for visit us.

Structure of C language program Structure of C language program Reviewed by Tech students on December 26, 2018 Rating: 5

No comments:

 
Powered by Blogger.