0% found this document useful (0 votes)
15 views6 pages

Lec 5 Pps

The document outlines the structure of a C program, emphasizing the importance of its sections for reducing errors and improving clarity. It details the various components including the documentation section, preprocessor section, definition section, global declaration, main function, and user-defined functions. Additionally, it provides an example program to illustrate these concepts.

Uploaded by

Netrapal Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views6 pages

Lec 5 Pps

The document outlines the structure of a C program, emphasizing the importance of its sections for reducing errors and improving clarity. It details the various components including the documentation section, preprocessor section, definition section, global declaration, main function, and user-defined functions. Additionally, it provides an example program to illustrate these concepts.

Uploaded by

Netrapal Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Structure of a C Program

Importance of structure of a C program


Sometimes, when we begin with a new programming language, we are not aware
about the basic structure of a program. The sections of a program usually get
shuffled and the chance of omission of error rises. The structure of a language gives
us a basic idea of the order of the sections in a program. We get to know when and
where to use a particular statement, variable, function, curly braces,
parentheses, etc. It also increases our interest in that programming language.

Thus, the structure helps us analyze the format to write a program for the least
errors. It gives better clarity and the concept of a program.

The sections of a C program are listed below:

1. Documentation section
2. Preprocessor section
3. Definition section
4. Global declaration
5. Main function
6. User defined functions
Documentation section
It includes the statement specified at the beginning of a program, such as a
program's name, date, description, and title. It is represented as:

1. //name of a program

Or

1. /*
2. Overview of the code
3. .
4. */

Both methods work as the document section in a program. It provides an overview of


the program. Anything written inside will be considered a part of the documentation
section and will not interfere with the specified code.

Preprocessor section
The preprocessor section contains all the header files used in a program. It informs
the system to link the header files to the system libraries. It is given by:

1. #include<stdio.h>
2. #include<conio.h>

The #include statement includes the specific file as a part of a function at the time of
the compilation. Thus, the contents of the included file are compiled along with the
function being compiled. The #include<stdio.h> consists of the contents of the
standard input output files, which contains the definition of stdin, stdout, and stderr.
Whenever the definitions stdin, stdout, and stderr are used in a function, the
statement #include<stdio.h> need to be used.

There are various header files available for different purposes. For example, #
include <math.h>. It is used for mathematic functions in a program.

Define section
The define section comprises of different constants declared using the define
keyword. It is given by:

1. #define a = 2
Global declaration
The global section comprises of all the global declarations in the program. It is given
by:
1. float num = 2.54;
2. int a = 5;
3. char ch ='z';

The size of the above global variables is listed as follows:

char = 1 byte

float = 4 bytes

int = 4 bytes

We can also declare user defined functions in the global variable section.

Main function
main() is the first function to be executed by the computer. It is necessary for a code
to include the main(). It is like any other function available in the C library.
Parenthesis () are used for passing parameters (if any) to a function.

The main function is declared as:

1. main()

We can also use int or main with the main (). The void main() specifies that the
program will not return any value. The int main() specifies that the program can
return integer type data.

1. int main()

Or

1. void main()

Main function is further categorized into local declarations,


statements, and expressions.

Local declarations
The variable that is declared inside a given function or block refers to as local
declarations.

1. main()
2. {
3. int i = 2;
4. i++;
5. }

Statements

The statements refers to if, else, while, do, for, etc. used in a program within the
main function.

Expressions

An expression is a type of formula where operands are linked with each other by the
use of operators. It is given by:

1. a - b;
2. a +b;
User defined functions
The user defined functions specified the functions specified as per the requirements
of the user. For example, color(), sum(), division(), etc.

The program (basic or advance) follows the same sections as listed above.

Return function is generally the last section of a code. But, it is not necessary to
include. It is used when we want to return a value. The return function returns a
value when the return type other than the void is specified with the function.

Return type ends the execution of the function. It further returns control to the
specified calling function. It is given by:

1. return;

Or

1. return expression ;

For example,

return 0;

Examples
Example 1: To find the sum of two numbers given by the user

It is given by:

1. /* Sum of two numbers */


2. #include<stdio.h>
3. int main()
4. {
5. int a, b, sum;
6. printf("Enter two numbers to be added ");
7. scanf("%d %d", &a, &b);
8. // calculating sum
9. sum = a + b;
10. printf("%d + %d = %d", a, b, sum);
11. return 0; // return the integer value in the sum
12. }

Output

The detailed explanation of each part of a code is as follows:

/* Sum of the two It is the comment section. Any statement described in it is not considered as a
numbers */ code. It is a part of the description section in a code.
The comment line is optional. It can be in a separate line or part of an
executable line.

#include<stdio.h> It is the standard input-output header file. It is a command of the preprocessor


section.

int main() main() is the first function to be executed in every program. We have used int
with the main() in order to return an integer value.

{… The curly braces mark the beginning and end of a function. It is mandatory in
} all the functions.

printf() The printf() prints text on the screen. It is a function for displaying constant or
variables data. Here, 'Enter two numbers to be added' is the parameter
passed to it.

scanf() It reads data from the standard input stream and writes the result into the
specified arguments.

sum = a + b The addition of the specified two numbers will be passed to the sum
parameter in the output.

return 0 A program can also run without a return 0 function. It simply states that a
program is free from error and can be successfully exited.

You might also like