0% found this document useful (0 votes)
102 views17 pages

Chapter 1

The document is an introductory chapter about computer programming and problem solving using the C language. It discusses what a computer is, the major hardware and software components, different types of computer languages including machine language, assembly language, and high-level languages like C. It also explains the software development process, including problem analysis, algorithm development, flowcharts, and compiling and executing C programs.

Uploaded by

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

Chapter 1

The document is an introductory chapter about computer programming and problem solving using the C language. It discusses what a computer is, the major hardware and software components, different types of computer languages including machine language, assembly language, and high-level languages like C. It also explains the software development process, including problem analysis, algorithm development, flowcharts, and compiling and executing C programs.

Uploaded by

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

CE-102: CPPS-I ( Chapter # 1 ) Batch 2003

Computer Programming & Problem Solving ( CPPS-I )

Introduction to Turbo C Programming Environment


Chapter # 1

 Introduction to Computer

 What is a Computer?

 Major Categories of a Computer

 Types of Software

 Computer Language

 3 Types of Computer Languages

 Software Development Problem Solving Tools

 Introduction to C Language

 Why use C Language?

 Turbo C Development System

 Files used in C program development

 Writing a Program in C Language

 Saving the Program in C Language

 The Basic Structure of C Program

Complied By: Muzammil Ahmad Khan

Sir Syed University of Engineering & Technology Page 1 of 17


CE-102: CPPS-I ( Chapter # 1 ) Batch 2003

Introduction to Computer

What is a Computer?

A Computer is a smart or intelligent machine, which can work only on certain


instructions given by a human being.

Major Categories of a Computer.

The elements of a Computer system fall into two major categories:


1. Hardware
2. Software

Hardware

All the equipment’s and accessories which can be seen and touched within the
computer system or related to the computer system.
Hardware is the equipment used to perform the necessary computations and
includes the CPU, monitor, keyboard and printer etc.

Software

Software consists of the programs that enable us to solve problems with a


computer by providing it with lists of instructions to perform.

Hardware and Software are both interdependent to each other.

Figure of components of a Computer.

Sir Syed University of Engineering & Technology Page 2 of 17


CE-102: CPPS-I ( Chapter # 1 ) Batch 2003

Types of Software

Basically there are two types of Software

1. Prewritten Application Software


2. User-Written Application Software

Prewritten application programs are the programs, which are used by a user to
acquire the desired results.
Examples are Windows, MS Word and Paintbrush etc

User-written application programs are the programs, which is written by a user


him self in the form of certain instructions. These User-written application
programs are however not understandable to the microprocessor of the
computer. To make it understandable certain types of compiler and interpreter
are used. Examples are Basic, Pascal, Cobol and C etc.

What are Instructions?

A set of characters or commands given by a human being to a machine or


computer to operate in such a way that the desired output is obtained.
Example is the addition of any two numbers in a calculator.

Computer Language

A computer language is a set of rules and conventions used to convey the


information to a computer.

3 Types of Computer Languages

 Machine Language
 Low Level Language
 High Level Language

Machine Language

The native tongue of a computer is the Machine Language. Each Machine


Language instruction is a binary string of 0’s and 1’s that specifies an operation
and identifies the memory cells involved in that operation.

Low Level Language

In the Low Level Language, Machine Language is still used by the computer as it
processes data, but Low Level Language software first translate the specified
operation symbol onto its Machine Language equivalent.
Example is Assembly Language.

Sir Syed University of Engineering & Technology Page 3 of 17


CE-102: CPPS-I ( Chapter # 1 ) Batch 2003

High Level Language

High Level Language is a programming language where an instruction resembles


everyday language. Instructions are given to a computer by using a convenient
letters, symbols or English text rather than by using 1’s and 0’s code that the
computer understands.
Example is Basic and Pascal etc.

The main difference between the High Level Language and the other two are that
High Level Language is much easier and understandable by a human being and
the second difference is that in High Level Language one instruction can perform
several machine level instructions.

Where C Language Stands

C Language in between the Low Level Language and High Level Language.
That’s why it is also called a Middle Level Language, since it was designed to
have both: a relatively good programming efficiency and relatively good machine
efficiency.

Relation between High Level Language and Machine Language

Because a computer understands only programs written in machine language,


each instruction in the High Level Language programs must first be translated
into the machine language before it can be executed. The original High Level
Language written program is called the Source Program; and the machine
language translation is called the Object Program.

Processing a High Level Language program

1. Use an editor or a word processor program to enter each line of the source
program into memory and to save it on disk as a source file.
2. Use a compiler program to translate the source program into machine
language. If there are syntax error i.e error in grammar, the compiler displays
these errors on the monitor. Use the editor program to correct the errors by
editing and resaving the source program.
3. When the source program is free of syntax errors, the compiler saves its
machine language translation as an object program.
4. The linker program combines your object program with additional object files
that may be needed for your program to execute and saves the final machine
language program an executable file on the disk.
5. The loader program places the executable file in the memory, ready for
execution.

Sir Syed University of Engineering & Technology Page 4 of 17


CE-102: CPPS-I ( Chapter # 1 ) Batch 2003

Processing a High Level Language program

You enter the program


and save it as a
Source File

Source Revised
File Source
File

The Compiler attempts to You correct


translate the program Syntax Errors

Success
Failure List of
Object Errors
File

The Linker links the new


object file other object files

Executable
File

The Loader places the


executable file into the
memory

Executable
Program in
Memory

Sir Syed University of Engineering & Technology Page 5 of 17


CE-102: CPPS-I ( Chapter # 1 ) Batch 2003

Software Development Problem Solving Tools

Computer is a general-purpose problem-solving machine that needs a program


to solve the problem. Following are the steps, which must be taken to prepare a
program.

1. Problem ( Requirements, Documents )

It is the actual object or task on which software is to be developed. It must be


well defined to achieve the entire desired goal.
Example computerization of all departments of SSUET.

2. Analysis ( Specifications, Documents )

Before analysing a problem a programmer/analyst must know the following


things.
 Type of information provided
 Problem to be solved
 Type of machines to be used
 Language of the software

In this step the whole strategy is developed.


Example how to start project, collect data, which software and hardware to be
used.

3. Algorithm ( Architectural or Over-all design )

It is the 3rd step of the problem-solving tool. In this step all the programming
steps are written in human languages step by step to solve the problem.
Algorithm is the sequence of instruction, which are given to computer.
Example what to perform technical task, how the instruction can be divided in
a modules, skeleton development of a problem.

4. Flow chart ( Data flow or Detailed design )

A flow chart is a graphical, pictorial or diagrammatically representation of the


logic or algorithm. It uses different types of boxes or symbols to show different
types of instructions. (Example overall structural details how to pick data).

Sir Syed University of Engineering & Technology Page 6 of 17


CE-102: CPPS-I ( Chapter # 1 ) Batch 2003

There are two types of flow chart.

a). System flow chart

It is used to show the flow of entire data.

b). Program flow chart

It is used to show the logic of instructions, which are given to


computers.

Flow Chart Symbols

The flow chart uses boxes of different shape to represent different types of
instructions. These symbols have been standardize by ANSI ( American National
Standard Institute ). Some of the major symbols are shown as follows.

a). Terminators.

This symbol is used to indicate the beginning and ending of the program.
It is the first and last step of the flow chart.

b). Input/Output

This symbol is used to show the process of any input or output function
such as read or write instructions.

c). Process

This symbol is used to represents arithmetic operations like addition,


subtraction, multiplication, division and data movement instructions.
Mathematical formulas are also represented in this box.

d). Decision

This symbol is used to represents the occurrence of a question or takes


the decision. A decision box normally entered from the top and have at
least 2 exit or may be more than that,

Sir Syed University of Engineering & Technology Page 7 of 17


CE-102: CPPS-I ( Chapter # 1 ) Batch 2003

e). Documentation

This symbol is used to indicate output on printer or any other


documentation on process.

f). On-Page Connectors

If a program is too long that might cause some confusion, we connect the
program by connector to make it simpler. An On-Page connector connects
the flow chart on the same page. A connector symbol is represented by a
circle and a letter or digit is placed within the circle to indicate the link.

g). Off-Page Connectors

When the program is too long to fit in a single page then we use the
symbol of Off-Page connector to continue the program on the other page.

h). Flow Lines with Arrow-Head

These arrows show the flow of the program or flow chart. These arrows
show the exact sequence of the program instructions.

5. Coding ( Unit Testing )

Program written in flow charts are transferred in any computer languages step
by step is called Coding.

6. Integration

To integrate different modules at a place is called Integration.

Sir Syed University of Engineering & Technology Page 8 of 17


CE-102: CPPS-I ( Chapter # 1 ) Batch 2003

7. Debugging ( Overall Testing)

Debugging means to find out or remove the errors from the program. There
are four types of errors that may be encountered when running a program.

a). Syntax Error

The statements that do not follow the rules of programming languages


cause syntax error. Syntax errors are caused by typing mistake, spelling
mistake or some other type of similar mistakes.

b). Run-Time Error

Run-Time error appears by the programmer’s carelessness and they are


not serves enough to prevent the translator from translating the program.
Run-Time error are caused by using variables that have not been defined
previously or nor providing enough data items to satisfy the data
requirements of the program.

c). Logical Error

Logical errors are often most difficult type of errors to detect. These errors
can be avoided be careful planning and by following a properly
constructed flow chart. If a program runs but does not produce the
desirable results then there may be an error in the logical organization of
the program.

d). Operator Error

Operator Error is normally occurred while entering input data by the


operators. These errors are not considered as the programming errors.

8. Documentation and Installation

It is very important that each program or project be properly be documented


to assists in revision of reference. A help guide or manual helps the user to
understand the software or package. This step also includes the complete
installation of the software.

Sir Syed University of Engineering & Technology Page 9 of 17


CE-102: CPPS-I ( Chapter # 1 ) Batch 2003

Introduction to C Language

C, a high-level language programming language was developed in early 1970s


by Dennis Ritchie at Bell Laboratories. Over the years, the power and flexibility of
C, together with the availability of high quality C compilers for computers of all
sizes, have made it a popular language in industry for a wide variety of
applications.

Why use C Language?

There are many reasons and advantages for using C Language.

1. C is the most popular PC programming language.


2. C is unique in programming language in that it provides the
convenience of a high level language such as Basic or Pascal, but at
the same time allows much closer control of a computer’s hardware
and peripherals, as the assembly language does.
3. C compilers can generates amazingly fast code.
4. C Language is a well-structured and modular language. Its syntax
makes it easy to write programs that are modular and therefore they
are easy to understand and maintain.
5. C Language is portable: i.e it is easier to convert a C program to run
on a different machine than it is to convert programs written in most
other languages.
6. C Language IDE is more user-friendly then most other languages.

Turbo C Development System

There are two development system in Turbo C

1. IDE
IDE means Integrated Development Environment. It is a screen display
with windows and pull down menus. The program listing, its output,
error messages and other information are displayed in separate
windows. You can use menu selections to invoke all the operations
necessary to develop your program including editing, compiling,
debugging, linking and program execution.

2. Command-Line Development System


This is a traditional command line system, in which editing, compiling,
debugging, linking and program execution are invoked from the DOS
command line prompt as separate activities, performed by separate
programs.

Sir Syed University of Engineering & Technology Page 10 of 17


CE-102: CPPS-I ( Chapter # 1 ) Batch 2003

Memory Models

 There are five memory models: Small, Compact, Medium, Large and
Huge.
 In the Small model your program can have upto 64K of code and 64K of
data storage.
 In the Medium model the program code can be exceed this 64K limit, but
the data is still restricted to 64K.
 Other models offer different arrangements of memory space.
 Each memory model requires a separate library file, and each library files
takes up space on your hard disk.

Files used in C program development

Executable Files

Executable files are stored in the subdirectory BIN. The most important
executable file for the Turbo C Language is the TC.EXE. Executing this program
places the IDE on your screen.
The BIN directory also contains programs for the command line development
process. For example

TCC Command line compiler


TLINK Command line linker
TCINST Customize Turbo IDE
CPP Preprocessor Utility
TLIB Library file manager
MAKE File management program

Library and Runtime Files

Various files are combined with your programs during linking. These files contain
routines for a wide variety of purposes. They are stored in LIB subdirectory.

Library Files

Library files are group of precompiled routines for performing specific tasks. For
example, if a programmer uses a function such as printf() to displayed text on
the screen, the code to create the display is contained in a library file. A library
file has a unique characteristic: only those parts of it that are necessary will be
linked to a program, not the whole file.

Sir Syed University of Engineering & Technology Page 11 of 17


CE-102: CPPS-I ( Chapter # 1 ) Batch 2003

Header Files

The subdirectory called INCLUDE contains header files. These files are text files,
like the one you generate with a word processor or the Turbo C editor. Header
files can be combined with your program before it is complied, in the same way
that a typist can insert a standard heading in a business letter. Each header file
has a “. h” file extension.

Programmer-Generated Files

You can place the programs that you write in any subdirectory you choose; for
instance, a subdirectory under TC.

Writing a Program in C Language.

The edit window should be active, with the cursor flashing at the upper right
corner. Type a program as follows:

void main (void)

{
printf ( “ This is my first program in C Language. “ );
}

Type as you would on a typewriter. Characters will appear where the cursor is
positioned. Press [ Enter ] key to move the cursor to the beginning of the net line.
If you make a mistake, use the [ Backspace ] key to delete the character to the
left of the cursor, or the [ Del ] key to delete the character under the cursor.

Saving the Program in C Language.

After you have typed in the source file for the program, you should save it on
your disk or hard disk. To do this, select the Save option from the File menu. You
can also accomplish the same effect simply by pressing the [ F2 ] function key.

In C language a New file is created by a temporary name NoName00.C. You can


save your source file as first.c or first.cpp.

Saving the source file is not necessary before compiling and running your
program, but it is a good idea and programming practice.

Sir Syed University of Engineering & Technology Page 12 of 17


CE-102: CPPS-I ( Chapter # 1 ) Batch 2003

Compiling and Making an .exe file

After you have written the source file for your program, you need to turn it into an
executable file. The compiler, which is a part of the IDE, translate the source
code into another file known as ( .obj ) Object file, consisting of the machine
language. The linker then links the entire necessary object files together to
produce a final executable program with the extension ( .exe ).

In Turbo C IDE, compiling and linking can be performed together in one step.
There are two ways to do this: either you can select make EXE file from the
compile menu, or you can press the [ F9 ] key.

If there is no errors in the compiling and linking process, you are ready to run the
program by simply selecting Run option from the Run menu bar or by pressing
the [ Ctrl ] [ F9 ] key combination.

To see the program output, select user screen option from the window menu or
press [ Alt ] [ F5 ].

To exit from the Turbo C IDE, select the Quit option from the file menu.

Class Assignment No 1

1. Difference between Backspace and Del key.

2. Difference between obj and exe file.

3. Difference between Machine Language and High Level Language.

4. What do you mean by IDE.

Compiler Vs Interpreter

Compiler translates the whole source program into machine language at once
while the Interpreter translates the source program into machine language line by
line.

Sir Syed University of Engineering & Technology Page 13 of 17


CE-102: CPPS-I ( Chapter # 1 ) Batch 2003

The Basic Structure of C Program.

void main (void)

{
printf ( “ This is my first program in C Language. “ );
}

 Function Definition
 Delimiter
 Statement Terminator
 The printf ( ) function

 Function Definition

All C Language programs are divided into units called “Functions”. A function
in C will always a ( ) at its end. Main( ) is always be the first function to be
executed and is the one to which control is passed when the program is
executed.

The word “void” preceding the “main“ specifies that the function main( ) will
not return a value. The second “void”, in the parenthesis, specifies that the
function takes no arguments.

 Delimiter

Following the function definition are the braces, which signals the beginning
and ending of the body of the function.
The opening brace “ { “ indicates that a block of code that forms a distinct unit
is about to begin. The closing brace “ } “ terminates the block code.

 Statement Terminator

The line in our program that begins with the word “ printf “ is an example of a
statement. A statement in C Language is terminated with a semicolon “ ; “.

The C Language pays no attention to any of the “ White Space Character “:


the carriage return (newline), the Spacebar and the Tab key. You can put as
many or as few white spaces characters in your program as you like; since
they are invisible to the compiler.

Sir Syed University of Engineering & Technology Page 14 of 17


CE-102: CPPS-I ( Chapter # 1 ) Batch 2003

 The printf ( ) function

The program line printf ( “ This is my first program in C Language. “ ); causes


the phrase in quotes to be printed on the screen. The word printf is actually a
function, just as “ main “ is a function name. Since “ printf “ is a function,
therefore it is followed by parenthesis.

The linker looks in the CS.LIB file, finds the section of this file containing
printf( ) and causes this section to be linked with the source program.

C Language distinguishes between uppercase and lowercase letters. Thus


the function PRINTF( ) and Printf( ) are not the same as the function printf( ).

Exploring the printf ( ) Function.

The printf( ) function uses a unique format for printing constants and variables.
For example

void main (void)

{
printf ( “ I am %d years old. “, 20 );
}

Output:

I am 20 years old.

The function printf( ) can be given more than one argument. For example in the
above program we have taken 2 arguments in the printf( ) function. The two
arguments are separated by a comma. The printf( ) function takes the vales on
the right of the comma and plugs it into the string on the left.
Why we are using it?
Why does it plug in it?
Where it finds a format specifier such as %d?

Sir Syed University of Engineering & Technology Page 15 of 17


CE-102: CPPS-I ( Chapter # 1 ) Batch 2003

Format Specifier

The format specifier tells the printf( ) where to put a value in a string and what
format to use in printing the values. In the previous program, the %d tells the
printf( ) to print the value 20 as a decimal integer.

Then what will be the method of writing a character or floating point number?

Why not simply put the decimal integer into the original string as compared to the
format specifier?

List of Format Specifier for printf( ).

%c single character
%s string
%d signed decimal integer
%f floating point ( decimal notation )
%e floating point ( exponential notation )
%g floating point ( %f or %e, which ever is shorter )
%u unsigned decimal integer
%x unsigned hexadecimal integer
%o unsigned octal integer

l prefix used with %d, %u, %x, %o to specify long integer.


For example %ld.

Example:

void main (void)

{
printf ( “ My name is %s and I am %d years old. “, ”Ahmad”, 20 );
}

Output:

My name is Ahmad and I am 20 years old.

Sir Syed University of Engineering & Technology Page 16 of 17


CE-102: CPPS-I ( Chapter # 1 ) Batch 2003

Example:

void main (void)

{
printf ( “ The letter %c is “, ‘j’ );

printf ( “ pronounced as %s “, “jay” );


}

Output:

The letter j is pronounced as jay.

In the above program, there are two different new things to note.

First we have used %c for the printing of a character which is surrounded by


single quotes where as %s is used for printing a string which is surrounded by
double quotes. This is how C Language recognizes the difference between a
character and a string.

Second thing to note is that even though the output statement is printed by two
separate program lines, it does not consists of two line of text on the output
screen. That is because printf( ) does not automatically prints a newline character
at the end of a line.

So what to do for inserting a new line in a printf( ) statement?

Class Assignment No 2

1. What is the purpose of the format specifier?

2. How can we insert a new line in printf( ) statement?

3. Why not simply put the decimal integer into the original string as compared to
the format specifier?

4. Write a program, which shows the output of the character, exponential and
unsigned decimal integer.

Complied By: Muzammil Ahmad Khan

Sir Syed University of Engineering & Technology Page 17 of 17

You might also like