cse 181 lecture notes

43
Computer Basics & Programming 1

Upload: piyas-chowdhury

Post on 22-Dec-2015

25 views

Category:

Documents


0 download

DESCRIPTION

CSE 181 Lecture Notes

TRANSCRIPT

Computer Basics

&

Programming

1

Reference BooksReference Books

• Introduction to Computers - Peter Norton.• Programming in ANSI C – E Balagurusamy.• Numerical Metods – Dr. V. N. Vedamurthy & Dr.

N. Ch. S. N. Iyengar.• Computer Fundamentals- Dr. M. Lutfar Rahman

& Dr. M. Alamgir Hossain.• Programming With C – Byron Gottfried.

2

Introduction to ComputersComputer Basics, Components of Computers, Importance of Computers, types of

computers.

Introduction to Computer ProgrammingHistory of C, Importance of C, Sample program: printing message, addition. Basic

structure of C programs. .

1St Cycle

3

Three basic functions of Computer

•Input

•Processing

•Output

Components of Computer SystemComponents of Computer System

•Hardware

•Software

•Humanware

•Operational Procedures

Computer BasicsComputer Basics

4

SOFTWARESOFTWARE

• is a sequence of instructions which directs a computer to

perform certain functions.

• generally catagorized as:

1. System Software: consists of programs that help the use of

a computer.

2. Application Software: includes programs to perform user

applictions.

5

Important criteriaImportant criteria Volume of data Accuracy Repetitiveness Complexity Speed Common Data

LimitationsLimitations can’t think.can’t think. can’t do anything without human instructions.can’t do anything without human instructions. can’t make any adjustment as human being.can’t make any adjustment as human being.

Importance & LimitationsImportance & Limitations

6

ClassificationClassification

Based on CapacityBased on Capacityo Microcomputerso Minicomputerso Mainframe Computerso Supercomputers

7

Computer GenerationsComputer Generations

First Generation (1942-1959)First Generation (1942-1959)• Utilized vacuum tubes in circuitry and for storage of data

and instructions• Caused tremendous heat problems.• Caused a great number of breakdowns and inefficient

operations.• Programs were written in machine language

combinations of 0 and 1.• Examples: IBM 650, IBM 704, IBM 705, IBM 709, Mark

II, Mark III etc.

8

Contd…..Contd…..

Second Generation(1960-1965)Second Generation(1960-1965)• Replacement of vacuum tubes by transistors.• Because of high speed operation and small size, perform

a single operation in microseconds and capable of storing tens of thousands of characters.

• Reliable, compact in size and virtually free of heat problems.

• Program written in both machine and symbolic languages (assembly language).

• Examples: IBM 1400, CDC 1604, RCA 501, NCR 300, GE 200, IBM 1600 etc.

9

Third Generation (1965-1971)Third Generation (1965-1971)• Characterized by increased input/output, storage and

processing capabilities.• I/O devices could communicate with computers over

distances via ordinary telephone lines, could display pictures on television-like screen, make musical sounds and even accept voice input.

• Increased storage capabilities, millions of characters could be stored and randomly accessed in fractions of a second, process instructions in nanoseconds.

• Example: IBM 360, IBM 370, PDP-8, PDP-11, GE 600 etc.

10

Contd…..Contd…..

Fourth Generation (1972-Present)Fourth Generation (1972-Present)• Semiconductor storage devices introduced.• Possessed a virtual storage capability in billions and

trillions of characters.• Execute a program many times the size of the machine’s

actual memory.• Microcomputers using microprocessors as the CPU

proliferated.• Most impressive advancement has occurred in software.

11

Contd…..Contd…..

Fifth Generation Fifth Generation • Is yet to come.• Will be capable of reasoning, learning, making

inferences and in ways considered exclusive of humans.• Will be equipped with massive primary-storage

capabilities and extremely fast processing speeds.• Hardware will continue to shrink in size but internal

memory will increase dramatically.• Artificial intelligence will be used extensively to enhance

system behavior.

12

Contd…..Contd…..

Overview of COverview of C

History of C History of C Most popular computer languages today because it is a

structured, high-level, machine independent language. Allows software developers to develop programs without

worrying about the hardware platforms where they will be implemented.

C was evolved from ALGOL, BCPL and B by Dennis Ritchie at the Bell Laboratories in 1972.

The language became more popular after publication of the book “The C Programming Language” by Brian Kerningham and Dennis Ritchie in 1978.

13

Contd…..Contd…..

• To assure that the C language remains standard, in 1983, American National Standards Institute (ANSI) appointed a technical committee to define a standard for C.

• In December 1989, a version of C is approved known as ANSI C.

• In 1990, it was approved by the International Standards Organization (ISO).

14

Importance of CImportance of C

Well suited for writing both system software and business packages.

Programs written in C are efficient and fast. C is highly portable. C language is well suited for structured programming. Another important feature of C is its ability to extend

itself.

15

Sample ProgramSample Program

Printing a messagePrinting a message#include<stdio.h>

main( )

{

/*……printing begins……..*/

printf(“I see, I remember”);

/*……..printing ends……..*/

}

16

Basic Structure of C Programs Documentation Section Link Section Definition Section Global Declaration Section main ( ) Function Section

{

}• Subprogram section

(User-defined functions)

17

Declaration part

Executable part

Function 1

- - -

Function n

Some programmer jargonSome programmer jargon• Some words that will be used a lot:

– Source code: The stuff you type into the computer. The program you are writing.

– Compile (build): Taking source code and making a program that the computer can understand.

– Executable: The compiled program that the computer can run.

– Language: (Special sense) The core part of C central to writing C code.

– Library: Added functions for C programming which are bolted on to do certain tasks.

– Header file: Files ending in .h which are included at the start of source code.

18

Constants, Variables & Data TypesConstants, Variables & Data Types

IntroductionIntroduction Data Information Program Syntax Rules

Character SetCharacter Seto Letterso Digitso Special characterso White spaces

19

C TokensC Tokens In a passage of text, individual words and punctuation

marks called tokens. Similarly, in a C program the smallest individual units are

known as C tokens. Six types of tokens in C :

1. Keywords

2. Identifiers

3. Constants

4. Strings

5. Special Symbols

6. Operators

20

Keywords & IdentifiersKeywords & Identifiers

KeywordsKeywords All keywords have fixed meanings and these meanings

cannot be changed. Keywords serve as basic building blocks for program

statements.

IdentifiersIdentifiers Identifiers refer to the names of variables, functions and

arrays. These are user defined names and consist of a

sequence of letters and digits.

21

Keywords of CKeywords of C• Flow control (6) – if, else, return, switch, case, default

• Loops (5) – for, do, while, break, continue• Common types (5) – int, float, double, char, void

• structures (3) – struct, typedef, union• Counting and sizing things (2) – enum, sizeof • Rare but still useful types (7) – extern, signed, unsigned, long, short, static, const

• Evil keywords which we avoid (1) – goto• Wierdies (3) – auto, register, volatile

22

Rules For IdentifiersRules For Identifiers

First character must be an alphabet (or underscore).

Must consist of only letters, digits or underscore.

Only first 31 characters are significant.

Cannot use a keyword.

Must not contain white space.

23

ConstantsConstants

o Numeric constantsNumeric constants

1. Integer Constants

2. Real Constants

o Character ConstantsCharacter Constants

1. Single Character Constants

2. String Constants

24

Contd……Contd……

• Integer Constants:Integer Constants: An integer constant refers to a sequence of digits. Three types of integers, namely, decimal integer, octal integer and hexadecimal integer.

• Real Constants:Real Constants: The quantities are represented by numbers containing fractional parts like 17.548, called real or floating point constants.

• Single Character Constants:Single Character Constants: contains a single character enclosed within a pair of single quote marks.

• String Constants:String Constants: sequence of characters enclosed in double quotes.

25

26

C Data TypesC Data Types

• There are only a few basic data types in C:– char – int – float– double

• short, long, signed and unsigned are additional qualifiers.

Contd…..Contd…..

int, float, charint, float, char– int (signed/unsigned)(2,4Bytes): int (signed/unsigned)(2,4Bytes): used to store

integers.– char (signed/unsigned)(1Byte): char (signed/unsigned)(1Byte): used to store

characters– float, double(4,8Bytes):float, double(4,8Bytes): used to store a decimal

number.

27

Declaration of VariablesDeclaration of Variables

• Primary Type DeclarationPrimary Type Declaration

data-type v1,v2,……vn;

where v1,v2,…….vn are the name of variables.

• User-Defined Type DeclarationUser-Defined Type Declaration

typedef type identifier;

where type refers to an existing data type and identifier refers to the new name given to data type.

28

NUMBER SYSTEMS

Many Number systems are in use in digital technology.

The most common are-

Decimal

Binary

Octal and

Hexadecimal.

29

Reading Data from KeyboardReading Data from Keyboard

The way of giving values to variables through keyboard is to use the scanf function. The general format of using scanf :

scanf(“control string”, &variable1,&variable2,…);

Control string contains the format of data being received. The ampersand symbol & before each variable name is an operator that specifies the variable name’s address.

30

Defining Symbolic ConstantsDefining Symbolic Constants

• A constant is defined as follows:

#define symbolic-name value of constant

Examples:

#define PI 3.14159

#define MAX 200• Symbolic names are sometimes called constant

identifiers.• #define statement is a preprocessor compiler directive

and is much more powerful than what has been mentioned here.

31

32

Invisible charactersInvisible characters

• Some special characters are not visible directly in the output stream. These all begin with an escape character (ie \);– \n newline– \t horizontal tab– \a alert bell– \v vertical tab

Operators & ExpressionOperators & Expression

• Operators:Operators: An operator is a symbol that tells the computer to perform certain mathematical or logical manipulations.

• Expressions:Expressions: Operators are used in programs to manipulate data and variables. They usually form a part of the mathematical or logical expressions.

33

Categories of C OperatorsCategories of C Operators

• Arithmetic operators• Relational operators• Logical operators• Assignment operators• Increment and decrement operators• Conditional operators• Bitwise operators• Special operators

34

35

Arithmetic OperatorsArithmetic Operators

• Five simple Five simple binary arithmetic operatorsbinary arithmetic operators1. + “addition or unary plus” c = a + b2. - “subtraction or unary minus” c = a - b3. * “multiplication” c = a * b4. / “division” c = a/b5. % “modulo division” c = a % b

Here a and b are variables and are known as operands.

• Modulo division operator % cannot be used on floating point data.

36

Relational OperatorsRelational Operators

• Six basic operators for comparison of values in C. These are typically called relational operators:

1. > “greater than”2. < “less than”3. >= “greater than or equal to”4. <= “less than or equal to”5. == “equal to”6. != “not equal to”

37

Logical OperatorsLogical Operators

• Logical Operators are used to create compound expressions

• There are two logical operators in C1. || “logical OR”

A compound expression formed with || evaluates to 1 (true) if any one of its components is true

2. && “logical AND” A compound expression formed with &&

evaluates to true if all of its components are true

Assignment OperatorsAssignment Operators

• Assignment operators are used to assign the result of an expression to a variable.

• C has a set of ‘shorthand’ assignment operators of the form

v op= exp;v op= exp;

is equivalent to

v = v op exp;v = v op exp;

where vv is a variable, expexp is an expression and opop is C binary arithmetic operator.

38

Increment & Decrement OperatorsIncrement & Decrement Operators

39

• Prefix Increment : ++a– example:

» int a=5; » b=++a; // value of b=6; a=6;

• Postfix Increment: a++– example

» int a=5;» b=a++; //value of b=5; a=6;

Conditional OperatorsConditional Operators

• A ternary operator pair “? :” is available in C to construct conditional expression of the form

exp1 ? exp2 : exp3exp1 ? exp2 : exp3

where exp1exp1, exp2 exp2 and exp3exp3 are expressions.

• exp1exp1 is evaluated first. If it is nonzero (true), then the exp3exp3 is evaluated and becomes the value of the expression. If exp1exp1 is false, exp3exp3 is evaluated and its value becomes the value of the expression.

40

Bitwise OperatorsBitwise Operators

• C has a distinction of supporting special operators known as bitwise operators for manipulation of data at bit level.

• Bitwise operators may not be applied to float or double.

41

Precedence TablePrecedence Table

42

Highest on top

++ -- (Postfix)

++ -- (Prefix)

* / %

+ -

<< >>

< >

&

|

&&

||

43