Vous êtes sur la page 1sur 41

Chapter 1

Introduction to Computers
Objectives
To review basic computer systems concepts
To be able to understand the different computing environments
and their components
To review the history of computer languages
To be able to list and describe the classifications of computer
languages
To understand the steps in the development of a computer
program
To review the system development life cycle

1-1 Computer Systems


Today computer systems are found everywhere.
Computers have become almost as common as
televisions. But what is a computer? A computer is a
system made of two major components: hardware and
software.
Topics discussed in this section:
Computer Hardware
Computer Software

FIGURE 1-1 A Computer System

FIGURE 1-2 Basic Hardware Components

Computer -- Hardware
Key board
Mouse

Input
Storage
Area

Program Storage Area

Output
Storage
Area

Working Storage Area

Input Devices

Monitor
Printer
Output Devices

Primary or Main Memory (RAM)


Register 1
Register 2

Register N

Arithmetic
and
Logic Unit

Secondary
Devices
MicroStorage
Processor
McGraw-Hill

The McGraw-Hill Companies, Inc., 2000

FIGURE 1-3 Types of Software

1-2

Computing Environments

In the early days of computers, there was only one


environment, the mainframe computer hidden in a central
computing department. With the advent of minicomputers and
personal computers, the environment changed with computers
on virtually every desktop. In this section we describe several
different environments.

Topics discussed in this section:


Personal Computing Environment
Time-Sharing Environment
Client/Server Environment
Distributed Computing

FIGURE 1-5 Personal Computing Environment

FIGURE 1-6 Time-sharing Environment

FIGURE 1-7 The Client/Server Environment

FIGURE 1-8 Distributed Computing

1-3

Computer Languages

To write a program for a computer, we must use a computer


language. Over the years computer languages have evolved
from machine language to natural languages.

Topics discussed in this section:


Machine Languages
Symbolic Languages
High-Level Languages

FIGURE 1-9 Computer Language Evolution

Machine Language Assembly Language HighLevel Language


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

1
2
3
4
5
6
7
8
9
1
0

00000000 00000100 0000000000000000


01011110 00001100 11000010 0000000000000010
11101111 00010110 0000000000000101
11101111 10111110 0000000000001011
11111000 10101101 11011111 0000000000010010
01100010 11011111 0000000000010101
11101111 00000010 11111011 0000000000010111
11110100 10101101 11011111 0000000000011110
00000011 10100010 11011111 0000000000100001
11101111 00000010 11011111 0000000000100100
01111110 11110100 10101101
11111000 10101110 11000101 0000000000101011
00000110 10100010 11111011 0000000000110001
11101111 00000010 11111011 0000000000110100
01010000 11010100 0000000000111011
00000100 0000000000111101

#include<stdio.h>
int main(void)
{
int n1, n2,product;
printf(Enter two numbers : );
scanf(%d %d,&n1,&n2);
product = n1 * n2;
printf(%d,product);
return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

entry
main,^m<r2>
sub12
#12,sp
jsb
C$MAIN_ARGS
moveb $CHAR_STRING_CON
pusha1
pusha1
calls
pusha1
pusha1
calls
mull3
pusha
calls
clrl
ret

-8(fp)
(r2)
#2,SCANF
-12(fp)
3(r2)
#2,SCANF
-8(fp),-12(fp),6(fp)
#2,PRINTF
r0

The only language the computer can


understand is machine language
(binary language).
A high level language is an English
like language where one instruction
typically translates into a series of

1-4

Creating and Running Programs

In this section, we explain the procedure for turning a program


written in C into machine language. The process is presented in
a straightforward, linear fashion, but you should recognize that
these steps are repeated many times during development to
correct errors and make improvements to the code.

Topics discussed in this section:


Writing and Editing Programs
Compiling Programs
Linking Programs
Executing Programs

FIGURE 1-10 Building a C Program

1-5

System Development

Weve now seen the steps that are necessary to build a program.
In this section, we discuss how we go about developing a
program. This critical process determines the overall quality
and success of our program. If we carefully design each
program using good structured development techniques, our
programs will be efficient, error-free, and easy to maintain.
Topics discussed in this section:
System Development Life Cycle
Program Development

FIGURE 1-11 Waterfall Model

Program Development Steps


1)Statement of Problem
a) Working with existing system and using proper
questionnaire, the problem should be explained
clearly.
b) What inputs are available, outputs are required
and what is needed for creating workable solution
should be understood clearly.
2)Analysis
a) The method of solutions to solve the problem can
be identified.
b) We also judge that which method gives best
results among different methods of solution.
3)Designing
a) Algorithms and flow charts will be prepared.
b) Keep focus on data, architecture, user interfaces
and program components.
4)Implementation
The algorithms and flow charts developed in the
previous steps are converted into actual programs in
the high level languages like C.

4.a)Compilation
Translate the program into machine code. This
process is called as Compilation. Syntactic errors are
found quickly at the time of compiling the program.
These errors occur due to the usage of wrong syntaxes
for the statements.
Eg: x=a*y+b
There is a syntax error in this statement, since, each
and every statement in C language ends with a
semicolon (;).
4.b)Execution
The next step is Program execution. In this phase, we
may encounter two types of errors.
Runtime Errors: these errors occur during the
execution of the program and terminates the program
abnormally.
Logical Errors: these errors occur due to incorrect
usage of the instructions in the program. These errors
are neither detected during compilation or execution
nor cause any stoppage to the program execution but
produces incorrect ouz

Executing a C program
#include<stdio.h>
Text
int main()
{
.
Editor
prog1.c
C-compiler compiles

Yes

Syntax
Errors?

010110 100
.
01011 101

No
Object machine code
Linker
00101010
.
01010101

prog1.obj

adds

Executable
machine code

machine code of
library file

C-Runtime

prog1.exe

Executes
Feeds

Input

Runtime
or Logic
Errors ?

Output

Yes

Translators are system


software used to
convert high-level
language program
into machinelanguage code.
Compiler : Coverts the
entire source
program at a time
into object code
file, and saves it in
secondary storage
permanently. The
same object
machine code file
will be executed

Note
Pseudocode
English-like statements that follow a loosely defined syntax
and are used to convey the design of an algorithm.

Structure of C program
Documentation Section
Linkage Section
Definition Section
Global Declaration Section
Main Function Section
Local Declaration Part
Executable Code Part
Sub Program Section
Function1()
Function2()

FunctionN()

Character Set of C-Language


Alphabets : A-Z and a-z
Digits : 0-9
Special Symbols : ~ ! @ # $ % ^ & ( ) _ - + = | \ { } [ ] : ;
<>,.?/
White Spaces : space , Horizontal tab, Vertical tab, New Line
Form Feed.
C-Language Keywords(C99)
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
_Bool _Imaginary
restrict
_Complex
inline

C-Tokens
Tokens : The smallest individual units of a C- program are
called Tokens. Key words, Identifiers, Constants,
Operators, Delimiters.
Key words : have a predefined meaning and these meanings
cannot be changed. All keywords must be written in small
letters (except additional c99 keywords).
Identifiers : names of variables, functions, structures,
unions, macros, labels, arrays etc.,
Rules for define identifiers :
a) First character must be alphabetic character or
under score
b) Second character onwards alphabetic character of
digit or under score.
c) First 63 characters of an identifier are significant.
d) Cannot duplicate a key word.
e) May not have a space or any other special symbol
except under score.
f) C language is Case-sensitive.

C-Tokens
Constants : fixed values that do not change during execution of a program.
Boolean constants : 0 ( false) and 1 (true)
Character constants :
only one character enclosed between two single quotes
( except escape characters ).
wide character type - wchar_t - for Unicode characters.
Integer constants : +123, -3454 , 0235 (octal value),
0x43d98 ( hexa - decimal value)
54764U, 124356578L, 124567856UL
Float constants : 0.2 , 876.345, .345623 , 23.4E+8, 47.45e+6
String Constants : Hello world , Have a nice day!
Complex Constants : real part + imaginary part * I ex : 12.3 + 3.45 * I
Operators : a symbol, which indicates an operation to be performed.
Operators are used to manipulate data in program.
Delimiters : Language Pattern of c-language uses special kind of symbols
: (colon, used for labels) ; (semicolon terminates statement ) ( ) parameter list
[ ] ( array declaration and subscript ),
{ } ( block statement )
# ( hash for preprocessor directive )
, (comma variable separator )

Data Types ( pre defined )


Type
char
unsigned char
signed char
int
32,767
unsigned int
signed int
short int
unsigned short int
signed short int
long int
2,147,483,647
long long int

Typical Size in Bits


Minimal Range
8
127 to 127
8
0 to 255
8
127 to 127
16 or 32
32,767 to
16 or 32
16 or 32
16
16
16
32
64

0 to 65,535
Same as int
32,767 to 32,767
0 to 65,535
Same as short int
2,147,483,647 to

(263) to 263 1 (Added by

Conversion Specifiers
Code
Format
%a
Hexa decimal output in the form of
0xh.hhhhp+d(C99 only)
%s
String of characters (until null zero is
reached )
%c
Character
%d
Decimal integer
%f
Floating-point numbers
%e
Exponential notation floating-point
numbers
%g
Use the shorter of %f or %e
%u
Unsigned integer
%o
Octal integer
%x
Hexadecimal integer
%i
Signed decimal integer

Back Slash ( Escape Sequence) Characters

Code

Meaning

\b Backspace
\f Form feed
\n New line
\r Carriage return
\t Horizontal tab
\" Double quote
\'
Single quote
\ \ Backslash
\v Vertical tab
\a Alert
\? Question mark
\N Octal constant (N is an octal constant)
\xN Hexadecimal constant (N is a hexadecimal
constant)

Increment and Decrement Operators


prefix increment (++a) postfix increment (a++) prefix
decrement(- -a) postfix decrement (a- -)
Executes all Prefix Operations
Before evaluation of expression
Evaluation of expression
After evaluation of expression
Executes all Postfix Operations
and before leaving the statement
/* prefix operators
*/
#include<stdio.h>
int main() {
int a = 7, b =
12, c;
c = b * (++a) +
5 * (++a);
printf( a =
%d, a);

/* prefix and postfix


operators */
#include<stdio.h>
int main() {
int a = 7, b = 12, c;
c = b * (a++) + 5 *
(++a);
printf( a = %d,
a);
printf(\n b =

/* postfix
operators */
#include<stdio.h>
int main() {
int a = 7, b =
12, c;
c = b * (a++) +
5 * (a++);
printf( a =
%d, a);

Bitwise Logical Operators


& -- Bitwise AND

A& B

A| B

A^ B

~A

| -- Bitwise OR

^ -- Bitwise XOR

Bitwise AND
~ -- Bitwise NOT
A (42) : 00000000 00101010
B (15) : 00000000 00001111
----------------------------------& (10) :Bitwise
00000000
00001010
XOR
----------------------------------A (42) : 00000000 00101010
B (15) : 00000000 00001111
-----------------------------------

Bitwise OR
A (42) : 00000000 00101010
B (15) : 00000000 00001111
----------------------------------| (47) :Bitwise
00000000
00101111
NOT
----------------------------------A (42) : 00000000 00101010
----------------------------------~ (-43) : 11111111 11010101

BITWISE SHIFT OPERATORS

Bitwise Right Shift


Bitwise Right Shift (
( >> )
>> )
(positive values)
(negetive values)
A (43) : 00000000 A (43) : 00000000
A (-44) : 11111111
00101011
00101011
Note
: Right shift operator
fills the left vacant11010100
fields with zeros f
-----------------------------------------------------------------------with ones for negative
numbers.
------------------TYPE ----------CONVERSION
Opposite
Explicit Type Conversio
A >> 2 : 00000000
ATo
<< 2 : 00000000
A >> 2 : 11111111
(casting)
1.bool
00001010
10101100
11110101
Gravity
2.char
(forcible)
-----------------------------------------------------------------------(forcible)
3.short
int
----------------------------- .
.
.
4.int
.
.
.
5.long int
6.long long int
7.float
(automa
(natu
8.double
tic)
ral)
Implicit
Type
Conversio
Conversi
9.long double
Gravity
(automatic)
Bitwise Left Shift
( << )

Precedence and Associativity of Operators


Precdence Group
(Highest to Lowest )
(param) subscript etc.,
Unary operators
Multiplicative
Additive
Bitwise shift
Relational
Equality
Bitwise AND
Bitwise exclusive OR
Bitwise OR
Logical AND
Logical OR
Conditional
Assignment
Comma

Operators

Associativity

( ) [ ] >.
- + ! ~ ++ (type) * & sizeof
*/%
+
<< >>
< <= > >=
= = !=
&
^
|
&&
||
?:
= += = *= /= %= &= ^=
|= <<= >>=
,

L R
RL
L R
L R
L R
L R
L R
L R
L R
L R
L R
L R
RL
RL
L R

Statement has two parts :

Declaration

The part of the program that tells the


compiler the names of memory cells in a
program

Executable statements

Program lines that are converted to machine


language instructions and executed by the
computer

Principles of Programming - NI 2005

33

The printf function

Used to send data to the standard output


(usually the monitor) to be printed
according to specific format.
General format:

printf(string literal);

A sequence of any number of characters


surrounded by double quotation marks.

printf(format string, variables);

Format string is a combination of text,


conversion specifier and escape sequence.

Principles of Programming - NI 2005

34

The printf function cont

Example:

printf(Thank you);
printf (Total sum is: %d\n, sum);

%d is a placeholder (conversion specifier)

marks the display position for a type integer


variable

\n is an escape sequence

moves the cursor to the new line

Principles of Programming - NI 2005

35

Each statement in C needs to be


terminated with semicolon (;)
Example:
#include <stdio.h>
void main(void)
{
printf(I love programming\n);
printf(You will love it too once );
printf(you know the trick\n);
}

Escape Sequence
Escape Sequence
\a
\b
\f
\n
\r
\t
\v
\\
\
\o
\x
\O
Principles of Programming - NI 2005

Effect
Beep sound
Backspace
Formfeed (for printing)
New line
Carriage return
Tab
Vertical tab
Backslash
sign
Octal decimal
Hexadecimal
NULL
37

Placeholder / Conversion Specifier


No
1
2
3
4
5
6
7
8
9
10
11
12
13

Conversion
Specifier
%d
%i
%o
%u
%x
%X
%f
%e
%E
%g
%G
%c
%s

Principles of Programming - NI 2005

Output Type

Output Example

Signed decimal integer


Signed decimal integer
Unsigned octal integer
Unsigned decimal integer
Unsigned hexadecimal (small letter)
Unsigned hexadecimal (capital letter)
Integer including decimal point
Signed floating point (using e notation)
Signed floating point (using E notation)
The shorter between %f and %e
The shorter between %f and %E
Character
String

76
76
134
76
9c
9C
76.0000
7.6000e+01
7.6000E+01
76
76
7
76'

38

The scanf function

Read data from the standard input device


(usually keyboard) and store it in a
variable.

General format:

scanf(Format string, &variable);

Notice ampersand (&) operator :

C address of operator
it passes the address of the variable instead
of the variable itself
tells the scanf() where to find the variable to
store the new value

The scanf function cont

Example :

int age;
printf(Enter your age: );
scanf(%d, &age);

Common Conversion Identifier used in printf


and scanf functions.
printf
scanf
int
%d
%d
float
%f
%f
double
%f
%lf
char
%c
%c
string
%s
%s

The scanf function cont

If you want the user to enter more than


one value, you serialise the inputs.

Example:
float height, weight;
printf(Please enter your height and
weight:);
scanf(%f%f, &height, &weight);
41

Vous aimerez peut-être aussi