Chapter 1b 8-12-2022
Chapter 1b 8-12-2022
1. Compilation process in c
What is a compilation?
The compilation is a process of converting the source code into object code. It is done with the help of the
compiler. The compiler checks the source code for syntactical or structural errors, and if the source code
is error-free, then it generates the object code. The c compilation process converts the source code taken
as input into the object code or machine code.
The compilation process can be divided into four steps, i.e., Pre-processing, Compiling, Assembling, and
Linking
o Pre-processor
The preprocessor takes the source code as input, and removes all the comments from the
source code, and expands the code.
The preprocessor takes the preprocessor directive and interprets it.
The source code is written in the editor and it has an extension “.c”
For example, if <stdio.h>, the directive is available in the program, then the preprocessor interprets
the directive and replaces this directive with the content of the 'stdio.h' file.
o Compiler
The code which is expanded by the preprocessor is passed to the compiler. The compiler converts
this code into assembly code.
o Assembler
The assembly code is converted into object code by using an assembler. The name of the object file
generated by the assembler is the same as the source file.
Eg. If the name of the source file is 'hello.c', then the name of the object file would be 'hello.obj'.
o Linker
The job of the linker is to link the object code of our program with the object code of the
library files and other files.
The output of the linker is the executable file.
The name of the executable file is the same as the source file but differs only in their
extensions
For example, For example, if we are using printf() function in a program, then the linker adds its
associated code in an output file
Let's understand through an example.
hello.c
1. #include <stdio.h>
2. int main()
3. {
4. printf("Hello world");
5. return 0;
6. }
Now, we will create a flow diagram of the above program:
For example,
Algorithm 1: Add two numbers entered by the user
Step 1: Start
Step 2: Declare variables num1, num2 and sum.
Step 3: Read values num1 and num2.
Step 4: Add num1 and num2 and assign the result to sum.
Step 5: Compute sum←num1+num2
Step 6: Display sum
Step 7: Stop
Algorithm 2: Finding the average of three numbers −
Step 1: Start
Step 2: Declare variables a, b, and c.
Step 3: Read 3 numbers a,b,c
Step 4: Compute sum = a+b+c
Step 5: Compute average = sum/3
Step 6: Display average value
Step 7: Stop
3. Flowchart
The Flowchart is the most widely used graphical representation of an algorithm and procedural
design workflows.
It uses various symbols to show the operations and decisions to be followed in a program.
It flows in sequential order.
Types of Flowcharts
The various types of flowcharts are given below.
Horizontal Flowchart
Panoramic Flowchart
Vertical Flowchart
Architectural Flowchart
Terminal Symbol: In the flowchart, it is represented with the help of a circle for denoting the
start and stop symbol
Input/output Symbol: The input symbol is used to represent the input data, and the output
symbol is used to display the output operation.
Decision Symbol: Diamond symbol is used for represents decision-making statements.
Connector Symbol: The connector symbol is used if flows discontinued at some point and
continued again at another place.
Flow lines: It represents the exact sequence in which instructions are executed. Arrows are
used to represent the flow lines in a flowchart.
Hexagon symbol (Flat): It is used to create a preparation box containing the loop setting
statement.
On-Page Reference Symbol: This symbol contains a letter inside that indicates the flow
continues on a matching symbol containing the same letters somewhere else on the same
page.
Off-Page Reference: This symbol contains a letter inside indicating that the flow continues
on a matching symbol containing the same letter somewhere else on a different page.
Delay or Bottleneck: This symbol is used for identifying a delay in a flowchart. The
alternative name used for the delay is the bottleneck.
Advantages of Flowchart in C:
o Communication: A flowchart is a better way of communicating the logic of a program.
o Efficient Coding: Flowcharts act as a guide for a programmer in writing the actual code
in a high-level language.
o Proper Debugging: Flowcharts help in the debugging process.
o Effective Analysis: Effective analysis of logical programs can be easily done with the
help of a related flowchart.
o Proper Documentation: Flowchart provides better and proper documentation. It consists
of various activities such as collecting, organizing, storing, and maintaining all related
program records.
o Testing: A flowchart helps in the testing process.
o Efficient program maintenance: The maintenance of the program becomes easy with the
help of a flowchart.
Disadvantages of Flowchart in C:
o Time-consuming: Designing a flowchart is a very time-consuming process.
o Complex: It isn't easy to draw a flowchart for large and complex programs.
o There is no standard in the flowchart; there is no standard to determine the quantity of
detail.
o Difficult to modify: It is very difficult to modify the existing flowchart.
Examples of flowchart:
The various examples of the flowchart are given below:
Example 1: Design a flowchart for adding two numbers entered by the user.
Example 2: Design a flowchart for finding the largest among three numbers entered by the user.
Example 3: Design a flowchart for calculating the profit and loss according to the value entered by the user.
Example 8: Design a flowchart for checking whether the number is positive or negative according to the
number entered by the user.