Lec 5 Pps
Lec 5 Pps
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.
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. */
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';
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.
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()
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:
Output
/* 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.
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.