basic structure of c

 v  Basic Structure of C Programs: - Basic structure of c program consists of several sections.

1.      Documentation Section.

2.      Link Section.

3.      Definition Section.

4.      Global Declaration Section.

5.      Function Prototype Declaration Section.

6.      Main Function

7.      Sub Function section.

1)    Documentation Section: -

Ø  Documentation section includes comments about the C program.

Ø  Comments improve readability and understandability of the programmer.

Ø  We can give comments about program, creation, modified date, author name, etc. in this section.

Ø  Comment lines are ignored by compiler while C program was compiled.

Ø  Comments can be of two types: 1) single line comments. 2) Multi line comments.

Ø  Single line comments would be declared after “ // “ double slash symbol, which can support only for single line statement in C program.

Ø  Multi line comments are used when comments are of more than one line of statements in C program. Between “ /* “  and   “ */ “ .

 

 

2)    Link Section: -

Ø  The Link section refers to the part of a program that deals with linking the compiled code with libraries and other object files to produce the final executable.

Ø  Header files are required to execute a C program, which are also known as preprocessor directives or library functions.

Ø  All preprocessor directives start with # symbol. Here # is known as preprocessor directive.

Ø  One line contains only one preprocessor directive.

Ø  No semicolon (;) is placed at the end of the preprocessor directives.

 

3)    Definition Section: -

Ø  In this section, we can declare and define symbolic constants or variables with preprocessor and used a keyword called define.

Ø  We can declare variables and set the values to these variables.

Ø  Write the variable name in upper case letters.

Ø  Symbolic constants are also known as Macros.

Ø  These variable values can be used throughout the functions in C program.

Ø  Example of definition section is 

# define PI 3.14  

# define TRUE 1

# define FALSE  0

 

4)    Global Declaration Section: -

Ø  The variables which are declared or defined before the main egg function are called Global variables.

Ø  These variables are used throughout the program of C.

Ø  Sometimes, we can declare a global variable with extern keyword for globalize the variable to all functions in program and also for other c programs.

Ø  Example:  

int a = 20;                     or           extern  int a = 20;

 

5)    Function Prototype Declaration Section: -

Ø  Function prototype gives many information about the function like return type, parameter name, parameter list used inside the function.

Ø  It is necessary, in order to provide the information to the compiler about function.

Ø  In C, declaration of function is called as prototype declaration.

Ø  Function declaration is also known as function prototype.

Ø  Prototype declaration always ends with semicolon (;).

Ø  Parameter list is optional.

Ø  Default return type is integer.

Ø  Examples of prototype declaration is

 int  sum(int , int);  

float area; 

void display(void);

 

6)    Main Function: -

Ø  Every C program execution is started from main function and this function contains two major sections called Declaration section and Executable section.

Ø  Declaration part declares all the variables that are used in Executable part.

Ø  Executable part consists at least one executable statement.

Ø  All local variables are declared at the starting and all executable functions or instructions or statements placed after Declaration section.

Ø  These two parts must be placed or written in between the opening and closing curly braces.

Ø  Each statement in declaration and executable parts must end with semicolon (;).

Ø  main() function is automatically called by operating system when the program executes.

Ø  We can’t run a C program without main () function.

 

7)    Sub Function section: -

Ø  The sub function or sub program section contains all user defined functions, that are used to perform specific tasks. These user defined functions are called in main () function.

Ø  If you are not using functions in C program then this section is optional.

Ø  It is also known as user defined function section.

Ø  These sub functions may be written before or after the main() function and called within main() function.

 

Basic C program to display a user defined message called “ Hello World”.

 

Output: -  Hello World

Example: -

 

 Output: - Enter the radius of the circle: 5

Area of the circle = 78.5000

 

v  Writing sample c programs: -

Example: - addition of two numbers using static data

 

 Output: -

 

addition of two no is: 30

 

Example: - addition of two numbers using dynamic data

 

Popular posts from this blog

operators in c programming

2.4 Arrays in c programming

2.1 Control statements in C programming (sequential & conditional)