2. Programming in C / Session 1 / 2 of 19
Objectives
Explain the beginning of C
Explain why and when C is used
Discuss the C program structure
Discuss algorithms
Demonstrate use of flowcharts
Apply the rules for writing C program
Demonstrate compilation and execution of a C program
3. Programming in C / Session 1 / 3 of 19
Problem Solving Approach
A step-by-step approach is used to solve any
problem
The step-by-step approach that is applied to
solve a problem may vary according to the
situation
The commonly used approaches are
Programs
Flow Charts
Algorithms
4. Programming in C / Session 1 / 4 of 19
Programs 2-1
Instructions grouped together in a sequential
manner is called a program.
Program helps perform a task or solve a
problem.
A program ensures that the task takes place
without any obstructions.
When the set of instructions are known, the
execution of the program takes place smoothly.
5. Programming in C / Session 1 / 5 of 19
Programs 2-2
Here, the boss gives simple instructions in a sequential
manner to perform a task without interruptions.
6. Programming in C / Session 1 / 6 of 19
Flow Charts 3-1
Pictorial representation of the approach to
solving a problem, with use of standard set of
symbols, is a Flow Chart.
As a picture speaks better than words, drawing a
diagram will help the user understand the
solution easily.
The Flow chart can also help the user to
understand the correct flow of actions and steps
to follow.
7. Programming in C / Session 1 / 7 of 19
Flow Charts 3-2
Flow charts use a standard set of symbols.
Rectangles for processing
Diamond shapes for decision making
The symbols are accepted and understood
uniformly by all the programmers in the world.
Connectors are used to link flow charts that are
in multiple pages.
Any flow chart always has a START and STOP.
8. Programming in C / Session 1 / 8 of 19
Flow Charts 3-3
START
Result =
num1 * num2
Display
Result
STOP
Accept num1
and num 2
Terminator Symbol: Used
here to begin the Flow chart
Input / Output Symbol: Used
here to accept numbers
Process Symbol: Used here to
multiply two numbers and
obtain the result
Input / Output Symbol: Used
here to display the result
Terminator Symbol: Used
here to end the Flow chart
9. Programming in C / Session 1 / 9 of 19
Algorithms
Formulation of an approach to solve a problem
in simple and understandable language is called
an algorithm.
Statements in algorithms clearly explain the
action that is to be performed by that step.
The steps have to be short and precise.
Collection of these steps, helps solve the
problem.
Two different individuals can write two different
algorithms to achieve the same result.
Algorithm to calculate the area of a rectangle
1. Accept two values from the user as the length and
breadth of the rectangle.
2. Multiply the length and breadth to obtain the area.
3. Display the area.
10. Programming in C / Session 1 / 10 of 19
History of C 2-1
Languages like B, Combined Programming
Language (CPL) and Basic CPL were used
popularly.
The B language used earlier was added with
basic functionality of BCPL to create C language.
Dennis Ritchie developed C in 1972 while
working with Bell Laboratories.
C was developed on PDP – 11 computers.
11. Programming in C / Session 1 / 11 of 19
History of C 2-2
UNIX operating system was re-written using the
C language.
When UNIX was re-written, only a part of the
original assembly language was retained.
Most universities used UNIX and this helped
make C a popular language among students.
To avoid compatibility problems arising from the
variations of C language developed by different
companies, ANSI set the guidelines for
standardising C. This is called ANSI C.
12. Programming in C / Session 1 / 12 of 19
Applications of C
The most popular example of C application is the re -
creation of UNIX operating system.
C is used to develop compilers, interpreters, graphic and
general utilities.
C has been used in the development of various
databases, word processors and spread sheets.
Power and flexibility has prompted the use of C
language in the development of custom – made
applications like railway ticket vending machines and
applications in the banking and insurance sector.
13. Programming in C / Session 1 / 13 of 19
Structure of C program 2-1
A typical C program is as follows
#include<stdio.h>
main()
{
printf("Hello Worldn");
}
Pre-processor
command
Starting point of the program
Opening brace
Prints Hello World
Closing brace
14. Programming in C / Session 1 / 14 of 19
Structure of C program 2–2
Output of the program is
15. Programming in C / Session 1 / 15 of 19
Compiling 2-1
There are four files associated when a C program is
compiled
Source code – with ‘.c’ as the extension. It is the file
where the program instructions are written.
Header File – contains the declaration of functions and
the pre-processor commands. It has ‘.h’ as its extension.
Object Files – these are the files created by the compiler
and have a extension either as ‘.o’ or ‘.obj’.
Binary Executables – output from the linking process.
The extension of these files is ‘.exe’.
The linker is a program that collects all the related object
files and creates an executable file.
16. Programming in C / Session 1 / 16 of 19
Compiling 2-2
Source Code (User
written program)
Compile the Source Code
Object File created.
But not executed
Executable File for
execution
Link the object files to
create an executable file
17. Programming in C / Session 1 / 17 of 19
Project in Visual C++
Creating a new file and saving it in the proper
directory.
Writing the C program in the IDE.
Building the project.
Executing the project.
Closing the project.
Demonstration
The steps involved in creating a project in Visual C++
18. Programming in C / Session 1 / 18 of 19
Summary 2-1
Algorithms help achieve a task with the help of
well-defined statements.
Algorithms can be pictorial or graphically
presented by flow charts.
For easy understanding of the depicted flow
charts, a standards set of symbols is used
across the industry.
Sequentially grouped set of instructions for
performing a task is called a Program.
19. Programming in C / Session 1 / 19 of 19
Summary 2-2
C language is extremely powerful and flexible as
it exhibits the features of both second and third
generation languages.
Compiler converts a program into a program
understood by the machine.
Linker is a program that links related object files
to create an executable program.