Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Name: Dr. Gufran Ahmad Ansari

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 37

CHAPTER 1

Introduction to Computers and C


Programming

Name: Dr. Gufran Ahmad Ansari

Email: gufran_1231@rediffmail.com , Phone No: 0543402062


OUTLINE
Lectures: Sat 08:00-11:00am,
Sun 08:00-11:00am at Room no:
 INTRODUCTION OF COMPUTER
 WHAT IS COMPUTER ?
 TYPES OF COMPUTER
 COMPUTER ORGANIZATION
 OPERATING SYSTEM
 LANGUAGES
 TYPES OF LANGUAGES
Machine Languages,
Assembly Languages
High-level Languages
 HISTORY OF C
 The C Standard Library
OBJECTIVE
In this chapter, you will learn:
 To understand basic Computer concepts.
 To become familiar with different types of programming
languages.
 To become familiar with the history of the C
programming language.
 To become aware of the C standard library.
 To understand the elements of a typical C program
development environment.
 To appreciate why it is important to learn C in a first
programming course.
 To appreciate why C provides a foundation for further
study of programming languages in general and of C++
and Java in particular.
Related Posts:

WHAT IS COMPUTER

Block Diagram of Computer Digital Computer .

A device that computes, especially a programmable electronic


machine that performs high-speed mathematical or logical
operations or that assembles, stores, correlates, or otherwise
processes information
COMPUTER ORGANIZATION
Six logical units in every computer

Input unit
Obtains information from input devices (keyboard, mouse)
Output unit
Outputs information (to screen, to printer, to control other
devices)
Memory unit
Rapid access, low capacity, stores input information
Arithmetic and logic unit (ALU)
Performs arithmetic calculations and logic decisions
Central processing unit (CPU)
Supervises and coordinates the other sections of the
computer
Secondary storage unit
Cheap, long-term, high-capacity storage
Stores inactive programs
TYPES OF COMPUTER
Analog Computer
Digital Computer
Hybrid Computer

Analog Computer

The analog computer is almost an extinct type of computer these


days. It is different from a digital computer in respect that it can
perform numerous mathematical operations simultaneously. It is
also unique in terms of operation as it utilizes continuous
variables for the purpose of mathematical computation. It utilizes
mechanical, hydraulic, or electrical energy or operation.
Analogue types of Computer uses what is known as analogue signals
that are represented by a continuous set of varying voltages and are
used in scientific research centers?, hospitals and flight centers.

With analogue types of computer no values are represented by


physical measurable quantities e.g. voltages.

Analogue computer types program arithmetic and logical


operations by measuring physical changes i.e. temperatures or
pressure.
DIGITAL COMPUTER

In the digital computers operation are on electrical input that can


attain two inputs, states of ON=1 and state of OFF = 0. With digital
type of computers data is represented by digital of 0 and 1 or off state
and on state. Digital computer type recognizes data by counting
discrete signal of (0 0r 1), they are high speed programmable; they
compute values and stores results. After looking at the Digital
computer type and how it functions will move to the third computer
type as mentioned above.
HYBRID COMPUTERS

These types of computers are, as the name suggests, a combination


of both Analog and Digital computers. The Digital computers which
work on the principle of binary digit system of “0” and “1” can give
very precise results. But the problem is that they are too slow and
incapable of large scale mathematical operation. In the hybrid types
of computers the Digital counterparts convert the analog signals to
perform Robotics and Process control.
Computers are also categorized on the basis of physical structures
and the purpose of their use. Based on Capacity, speed and reliability
they can be divided into three categories of computers:

The Mainframe Computer

These are computers used by large organizations like meteorological


surveys and statistical institutes for performing bulk mathematical
computations. They are core computers which are used for desktop
functions of over one hundred people simultaneously.

The Microcomputer

These are the most frequently used computers better known by the
name of “Personal computers”. This is the type of computer meant
for public use. Other than Desktop
The Mini Computer

Mini computers like the mainframe computers are used by business


organization. The difference being that it can support the
simultaneous working of up to 100 users and is usually maintained in
business organizations for the maintenance of accounts and finances.
Generation of Computer

First Generation (1940-1956) Vacuum Tubes

Second Generation (1956-1963) Transistors

Third Generation (1964-1971) Integrated Circuits

Fourth Generation (1971- 1982) VLSI

Fifth Generation Present


Operating System
An operating system, or OS, is a software program that
enables the computer hardware to communicate and operate
with the computer software. Without a computer operating
system, a computer would be useless

Operating system types


As computers have progressed and developed so have the types
of operating systems. Below is a basic list of the different types of
operating systems and a few examples of operating systems that
fall into each of the categories. Many computer operating
systems will fall into more than one of the below categories.

GUI - Short for Graphical User Interface, a GUI Operating


System contains graphics and icons and is commonly navigated
by using a computer mouse. Below are some examples of GUI
Operating Systems.
System 7.x
Windows 98

Multi-user - A multi-user operating system allows for multiple


users to use the same computer at the same time and/or different
times. Below are some examples of multi-user operating systems.

Linux
Unix
Windows 2000
Multiprocessing - An operating system capable of supporting and
utilizing more than one computer processor. Below are some
examples of multiprocessing operating systems.

Linux , Unix, Windows 2000

Multitasking - An operating system that is capable of allowing


multiple software processes to run at the same time. Below are some
examples of multitasking operating systems.

Unix , Windows 2000


Multithreading - Operating systems that allow different parts of a
software program to run concurrently. Operating systems that
would fall into this category are:

Linux, Unix
Types of Languages
Three types of programming languages

Machine languages
Strings of numbers giving machine specific instructions
Example:
+1300042774
+1400593419
+1200274027

Assembly languages
English-like abbreviations representing elementary
computer operations (translated via assemblers)
Example:
LOAD BASEPAY
ADD OVERPAY
STORE GROSSPAY
High-level languages

Codes similar to everyday English


Use mathematical notations (translated via compilers)
Example:

Gross Pay = base Pay + over Time Pay


HISTORY OF C
C is a programming language which born at “AT & T’s Bell
Laboratories” of USA in 1972.

It was design and written by Dennis Ritchie.

In the late seventies C began to replace the more familiar languages


of that time like PL/I, ALGOL, etc. No one pushed C.

Because C is such a powerful, dominant and simple language, its use


quickly spread beyond Bell Labs.

Possibly why C seems so popular is because it is reliable, simple


and easy to us.
To solve this problem, the American National Standard Institute
(ANSI) formed a committee in 1983 to establish a standard definition
of C. This committee approved a version of C in 1989 which is known
as ANSI C. With few exceptions, every modern C compiler has the
ability to adhere to this standard. ANSI C was then approved by the
International Standard Organization (ISO) in 1990.

The C language is so named because its predecessor was called B.

The B language was developed by Ken Thompson of Bell Labs.


WHY USE C
In today's world of computer programming, there are many high-
level languages to choose from, such as Pascal, BASIC, and Java. But
C stands apart from all these languages. This is due to its many
desirable qualities. It is a robust language whose rich set of built-in
functions and operators can be used to write any complex logic
program.

The C language compiler combines the capabilities of a low level


language with the features of a high level language. Therefore the
language is suitable for writing both system software as well as
business packages & other software. You will many compilers
available in the market written in C.
Program written in C are very efficient and fast. This is due to its
variety of data types and powerful operators.

It is many time faster than BASIC. This helps developers in saving


their valuable time.
C is a powerful and flexible language which helps system developers to
deliver various complex tasks with ease. C is used for diverse projects as
operating systems, word processors, graphics, spreadsheets, and even
compilers for other languages.

C is popular among professional programmers for programming, as a


result, a wide variety of C compilers and helpful accessories are
available.

C is highly portable language. This means that a C program written for


one computer system (an IBM PC, for example) can be run on another
system (a DEC VAX system, perhaps) with little or no modification.
Portability is enhanced by the ANSI standard for C, the set of rules for
C compilers.
C’s another striking feature is its ability to extend itself. A C program
is basically a collection of various function supported by C library (also
known as header files). We can also add our own functions to the C
library. These functions can be reused in other applications or
programs by passing pieces of information to the functions, you can
create useful, reusable code.

• Writing C program with user-defined functions makes program more


simple and easy to understand. Breaking a problem in terms of
functions makes program debugging, maintenance and testing easier.

As these features shows that C is an excellent choice for your first


programming language. But what is C++? You might have heard this
term C++ and the programming technique called Object-Oriented
Programming (OOP) Possibly you're wondering what the differences
are between C and C++ and whether you should learn C or C++.
Steps in learning English language:

Alphabets Words Sentences Paragraphs

Steps in learning C:

Alphabets Constants
Digits Variables Instructions
Special symbols Keywords

Program
Alphabets A, B, ….., Y, Z
a, b, ……, y, z
Digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Special symbols ~ ‘ ! @ # % ^ & * ( ) _ - + = | \ { }
[]:;"'<>,.?/

Constants, Variables and Keywords

The alphabets, numbers and special symbols when properly combined


form constants, variables and keywords. Let us see what are ‘constants’
and ‘variables’ in C. A constant is an entity that doesn’t change
whereas a variable is an entity that may change.
TYPES OF CONSTANT
Types of C Constants
C constants can be divided into two major categories:
Primary Constants
Secondary Constants
These constants are further categorized as shown

C Constants

Primary Constant Secondary Constant

Array
Integer Constant
Pointer
Real Constant
Union
Character Constant Enum, etc.
Rules for Constructing Integer Constants:

 An integer constant must have at least one digit.


 It must not have a decimal point.
 It can be either positive or negative.
 If no sign precedes an integer constant it is assumed to be positive.
 No commas or blanks are allowed within an integer constant.
 The allowable range for integer constants is -32768 to 32767.

Truly speaking the range of an Integer constant depends upon the


Compiler For a 16-bit compiler like Turbo C or Turbo C++ the range is
–32768 to 32767
Example:
426
+782
-8000
-7605
Rules for Constructing Real Constants:

Real constants are often called Floating Point constants. The real
constants could be written in two forms—Fractional form and
Exponential form.
Following rules must be observed while constructing real
constants expressed in fractional form:

A real constant must have at least one digit.


It must have a decimal point.
It could be either positive or negative.
Default sign is positive.
No commas or blanks are allowed within a real constant.

Ex.: +325.34
426.0
-32.36
Following rules must be observed while constructing realconstants
expressed in exponential form:

The mantissa part and the exponential part should be separated by a


letter e.
The mantissa part may have a positive or negative sign.
Default sign of mantissa part is positive.
The exponent must have at least one digit, which must be a positive or
negative integer. Default sign is positive.
Range of real constants expressed in exponential form is
-3.4e38 to 3.4e38.

Ex.: +3.2e-5
4.1e8
-0.2e+3
-3.2e-5
Rules for Constructing Character Constants

A character constant is a single alphabet, a single digit or a single special


symbol enclosed within single inverted commas. Both the inverted
commas should point to the left.

For example, ’A’ is a valid character constant whereas ‘A’ is


not.

The maximum length of a character constant can be 1


character.
Ex.: 'A'
'I'
'5'
'='
Types of C Variables
As we saw earlier, an entity that may vary during program execution
is called a variable.
For example, an integer variable can hold only an integer constant, a
real variable can hold only a real constant and a character variable
can hold only a character constant
Rules for Constructing Variable Names
A variable name is any combination of 1 to 31 alphabets, digits or
underscores. Some compilers allow variable names whose length could be
up to 247 characters. Still, it would be safer to stick to the rule of 31
characters. Do not create unnecessarily long variable names as it adds to
your typing effort.
The first character in the variable name must be an alphabet or
underscore.
No commas or blanks are allowed within a variable name.
No special symbol other than an underscore (as in gross_sal)
can be used in a variable name.
Ex.: si_int
m_hra
The first character in the variable name must be an alphabet or
underscore.
No commas or blanks are allowed within a variable name.
No special symbol other than an underscore (as in gross_sal)
can be used in a variable name.

Ex.: int si, m_hra ;


float bassal ;
char code ;
C Keywords
Keywords are the words whose meaning has already been explained
to the C compiler (or in a broad sense to the computer). The
keywords cannot be used as variable names because if we do so we
are trying to assign a new meaning to the keyword, which is not
allowed by the computer.
The keywords are also called ‘Reserved words. There are only 32
keywords available in C.

auto double int struct


break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
The First C Program

Before we begin with our first C program do remember the following


rules that are applicable to all C programs:

Each instruction in a C program is written as a separate statement.


Therefore a complete C program would comprise of a series of
statements.

The statements in a program must appear in the same order in which


we wish them to be executed; unless of course the logic of the
problem demands a deliberate ‘jump’ or transfer of control to a
statement, which is out of sequence.

Blank spaces may be inserted between two words to improve the


readability of the statement. However, no blank spaces are allowed
within a variable, constant or keyword.
All statements are entered in small case letters.

C has no specific rules for the position at which a statement is to be


written. That’s why it is often called a free-form language.

Every C statement must end with a ;.


Thus ; acts as a statement terminator.
/* First program in C */
#include <stdio.h>
main()
{
printf( “Abudlla”);

Welcome to C!
/* Calculation of simple interest */
/* Author Name: 25/09/2010 */
main( )
{
int p, n ;
float r, si ;
p = 100 ;
n=3;
r = 4.5 ;
/* formula for simple interest */
si = p * n * r / 100 ;
printf ( "%f" , si ) ;
}
Basics of a Typical C Program
Development Environment
Editor Disk
• Phases of C++ Preprocessor
Preprocessor program
Disk processes the code.
Programs: Compiler creates object
Compiler
1. Edit Disk code and stores
it on disk.
2. Preprocess Linker Disk Linker links the object
Memory code with the libraries
3. Compile Primary
Primary Memory

Loader
4. Link Loader puts program in
5. Load Disk ..
..
memory.

6. Execute
..

Primary Memory
Primary MemoryCPU takes each
CPU instruction and executes it,
  possibly storing new data
values as the program
..
..
..
executes.

You might also like