C程序設(shè)計第一章概述課件_第1頁
C程序設(shè)計第一章概述課件_第2頁
C程序設(shè)計第一章概述課件_第3頁
C程序設(shè)計第一章概述課件_第4頁
C程序設(shè)計第一章概述課件_第5頁
已閱讀5頁,還剩54頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

C語言概述-

Introduction信息對抗1電子工程學(xué)院Computer

System2A

computer

systemis

a

collection

of

hardwareand

software

components

that

work

together

torun

computerprograms.Specific

implementations

of

systems

change

overtime,

but

the

underlying

concepts

donot.All

systems

have

similar

hardware

and

softwarecomponents

that

perform

similar

functions.What

is

a

Computer?3

ComputerA

device

capable

of

performing

computations

and

making

logicaldecisions

Computer

programsSets

of

instructions

that

control

a

computer’s

processing

ofdata

HardwareVarious

devices

comprising

a

computerExamples:

keyboard,

screen,

mouse,

disks,

memory,CD-ROM,

andprocessing

units

SoftwarePrograms

that

run

acomputerHardware

Organization

of

aSystem4Computer

Organization5

Six

logical

units

in

every

computer:Input

unit

:

computer’ssensesObtains

information

from

input

devices

(keyboard,

mouse)Output

unit

:

computer’s

effectorsOutputs

information

(to

screen,

to

printer,

to

control

other

devices)RAM(

random

access

memory)chips

:

Memory

unitRapid

access,

low

capacity,

stores

input

informationCentral

processing

unit

(CPU)

:

brain

of

the

computerSupervises

and

coordinates

the

other

sections

of

the

computerBus

:

nervous

system;it

carries

information

between

the

CPU

and

everything

else

in

thecomputer.Secondary

storage

unitCheap,

long-term,

high-capacity

storage,

stores

inactive

programsProcessor6CPU

controls

and

coordinates

the

whole

machine.

It

contains

a

set

ofregisters:The

instruction

register(IR)

holds

the

current

machine

instruction.The

instruction

count(IC)

holds

the

address

of

the

next

machineinstruction.The

memory

data

register(MDR)

holds

the

data

currently

in

use.The

memory

address

register(MAR)

holds

the

address

from

which

thedata

came.One

of

its

components

is

the

clock,which

ticks

at

a

fixed

rate

andcontrols

the

fundamental

speed

at

which

all

of

the

computer’soperation

work.Arithmetic

and logic

unit(ALU)7Perform

computations:An

ALU

includes

instructions

for

addition,negation,andmultiplication

of

integers;

comparison;logicaloperations;and

other

actions.Floating-point

coprocessor:

handling

arithmetic

operationson

real(floating-point)numbers.Important

for

scientific

applications

to

achieve

adequateaccuracy

at

an

acceptable

speed.Control

ROM

,instruction

cycle8A

small

read-only

memory

inside

the

control

unitcontainsinstructions(called

microcode)

that

control

all

parts

of

theCPU

and

define

the

actions

of

the

instruction

cycle

,

theALU,and

the

instruction

cache.Memory內(nèi)存Storage

locations:

bits

binary

digits.Bytes:each

addressable

location

is

eight

bits

long.Several

different

types

of

memory

to

achieve

differentbalances

among

capacity,cost,speed,and

convenience.cacheMain

memoryDirect-access

memorySequential-access

auxiliary

memoryCapacitySpeedcost9數(shù)據(jù)表示所有的數(shù)據(jù)類型輸入到計算機內(nèi)部以后都被轉(zhuǎn)換成一種統(tǒng)一的表示格式,這種統(tǒng)一的格式就是比特模式(Bit

Pattern)一般,長度為8的比特模式我們稱之為一個字節(jié)(Byte)二進制系統(tǒng)一個字節(jié)當中的比特可以按照從右往左的順序?qū)λM行0到7的編號,最左邊的比特位我們稱之為最高有效位(MostSignificantBit,MSB),而最右邊的比特位稱為最低有效位(Least

Significant

Bit,LSB)10數(shù)

計算機中數(shù)的表示及進制轉(zhuǎn)換

數(shù)碼、基數(shù)與權(quán)數(shù)碼:表示數(shù)的符號基數(shù):數(shù)碼的個數(shù)(R)權(quán):每一位所具有的值(Ri)

數(shù)制例:八十二十進六進制進制::制:數(shù)碼:數(shù)0碼,:1,0,2,1,3,2,4,3,5,4,6,5,7,6,8,7,9,8,A,9

B,C,D,E,F基:82160權(quán):::……,1,6282,21,60821,,182600.1826-,,1,821-62,-…2,…小數(shù)點11各種進制之間的轉(zhuǎn)換二進制、八進制、十六進制轉(zhuǎn)換成十進制方法:按權(quán)相加12各種進制之間的轉(zhuǎn)換(整數(shù))十進制轉(zhuǎn)換成二進制、八進制、十六進制方法:連續(xù)除以基,從低到高記錄余數(shù),直至商為0例 把十進制數(shù)459轉(zhuǎn)換成十六進制數(shù)4591616

2816

1

0(459)10=(1CB)161CB余11余12余1商13余數(shù)例(1101001)2=(001,101,001)2=(151)8例(246)8=(010,100,110)2=(10100110)2000~

0001~

1010~

2011~

3100~

4101~

5110~

6111~

7二進制與八進制之間的轉(zhuǎn)換二進制轉(zhuǎn)換成八進制:從右向左,每3位一組(不足3位左補0),轉(zhuǎn)換成八進制八進制轉(zhuǎn)換成二進制:用3位二進制數(shù)代替每一位八進制數(shù)14二進制與十六進制之間的轉(zhuǎn)換二進制轉(zhuǎn)換成十六進制:進制十六進制轉(zhuǎn)換成二進制:用4位二進制數(shù)代替每一位十六進制數(shù)例(11010101111101)2=(0011,0101,0111,1101)2=(357D)16例(4B9E)16=(0100,1011,1001,1110)2=(100101110011110)20000~

00001~

1從右向左,每4位一組(不足4位左補0),轉(zhuǎn)換成00十10六~

20011~

30100~

40101~

50110~

60111~

71000~

81001~

91010~

A1011~

B1100~

C1101~

D1110~

E1111~

F15數(shù)的不同進制表示16十進制:4956=4

103+9

102+5

101+二進制:1011=1

23+0

22+1

21+1

2o6

10o十六進制:81AE=8

163+1

162

+10

16數(shù)制十進制數(shù)二進制數(shù)八進制數(shù)十六進制數(shù)數(shù)碼0~90~10~70~9,A~F,a~f基102816權(quán)10o,101,102,…2o,21,22,…8o,81,82,…16o,161,162,…表示八進制:4275=4

83+2

82

+7

81+5

8o

1+14

16o特點逢十進一逢二進一逢八進一逢十六進一數(shù)的表示17在計算機中,對于一般的數(shù)是采用定點數(shù)與浮點數(shù)兩種方法來表示定點數(shù):指小數(shù)點位置固定不變的數(shù)。在計算機中,通常用定點數(shù)來表示整數(shù)與純小數(shù),分別稱為定點整數(shù)與定點小數(shù)定點整數(shù):一個數(shù)的最高二進制位是數(shù)符位,用以表示數(shù)的符號;而小數(shù)

點的位置默認為在最低(即最右邊)的二進制位的后面,但小數(shù)點不單獨占一個二進制位,如圖所示:0

1001010數(shù)符位

數(shù)值位

小數(shù)位因此,在一個定點整數(shù)中,數(shù)符位右邊的所有二進制位數(shù)表示的是一個整數(shù)值。定點小數(shù):一個數(shù)的最高二進制位是數(shù)符位,用來表示數(shù)的符號;而小數(shù)點的位置默認為在數(shù)符位后面,不單獨占一個二進制位,如圖所示:0

1001010數(shù)符位|小數(shù)位

數(shù)值位因此,在一個定點小數(shù)中,數(shù)符位右邊的所有二進制位數(shù)表示的是一個純小數(shù)。數(shù)值的表示方法——原碼、反碼和補碼18

原碼:最高位為符號位,其余各位為數(shù)值本身的絕對值

反碼:正數(shù):反碼與原碼相同負數(shù):符號位為1,其余位對原碼取反

補碼:正數(shù):原碼、反碼、補碼相同負數(shù):最高位為1,其余位為原碼取反,再對整個數(shù)加1舉例原碼反碼補碼+7000001110000011100000111-7100001111111100011111001+0000000000000000000000000-0100000001111111100000000數(shù)的范圍01111111~11111111(-127~+127)01111111~10000000(-127~+127)01111111~10000000(-128~+127)負數(shù)補碼轉(zhuǎn)換成十進制數(shù):最高位不動,其余位取反加1例 補碼:11111001

取反:10000110加1:

10000111=-719字符編碼20ASCII是由美國國家標準員會制定的一種包括數(shù)字、字母、通用符號、控制符號在內(nèi)的字符編碼集,全稱為美國國家信息交換標準碼(American

Standard

Code

forInformationInterchange),被國際標準化組織(ISO)指定為國際標準漢字編碼目前有國標碼、機內(nèi)碼、外碼、字形碼和混合編碼等算法

Algorithm21算法是對特定問題求解步驟的一種描述。算法的必須滿足的三個基本要求:有窮性:一個算法必須在執(zhí)行有窮步之后結(jié)束。確定性:算法的每一步必須是確切定義的。對于相同輸入必須得到相同結(jié)果。有效性:算法的每一步都是能夠?qū)崿F(xiàn)的,即可操作的。算法表示方法(自然語言描述)22例:有50個學(xué)生,要求將他們之中成績在80分以上者打印出來。用g代表學(xué)生成績,gi

代表第i個學(xué)生成績,算法可表示如下:S1:1?iS2:如果gi

≥80

,則打印

gi

,否則不打印S3:

i+1

?iS4:如果i≤50,返回S2,繼續(xù)執(zhí)行;否則,算法結(jié)束。算法表示方法(偽代碼描述)Alogrithm(算法):Finding

SmallestPurpose

(目的):

This

algorithm

finds

the

smallest

numberamong

a

list

of

numbersPre(前提):ListofnumbersPost(后處理):NoneReturn(返回值):The

smallest1Set

smallest

to

the

first

number2Loop(not

end

of

list)if(next

number

<

smallest)set

smallest

to

next

numberend

if3end

loop4returnsmallestEnd

Finding

Smallest23算法表示方法(流程圖表示法)一般用流程圖表示,常用符號有:起止框輸入/輸出框判斷框處理框流程線24程序流程圖示例開始1=>igi>=80輸出gii+1=>ii>50結(jié)束Y

N

N25Programming

Language26定義:用于書寫計算機程序的語言。a

given

set

of

rules

orconventions語言的基礎(chǔ)是一組記號(notation)與一組規(guī)則。程序設(shè)計語言包括:語法:記號的組合規(guī)則syntax語義:記號的特定意義semantic語用:程序與使用者的關(guān)系基本成分:數(shù)據(jù)成分運算成分控制成分傳輸成分Programming

Language27區(qū)別于自然語言將自然語言形式化為有格式的語言按照與自然語言的相似性分為:高級程序設(shè)計語言High-level

programming

languages低級程序設(shè)計語言(接近機器語言)low-level

languages按照應(yīng)用范圍分為:通用語言專用語言Evolution of

Programming

Lang.機器語言匯編語言高級語言面向過程面向?qū)ο罂陀^世界可以分類,對象是類的實例面向機器的語言用助記符號描述的指令系統(tǒng)對象是數(shù)據(jù)和方法的封裝程序設(shè)計關(guān)鍵是定義類,并由類派生對象如 ADD 對象間通過發(fā)送和接受消息發(fā)生聯(lián)系A(chǔ), B馮.諾依曼結(jié)構(gòu):計算機運算器存儲器I/O設(shè)備:鍵盤、顯示器等中央處理器CPU主機:控制器程序設(shè)計是數(shù)據(jù)被加工的過程28源程序

sourceprogram29我們目前學(xué)習(xí)的就是使用高級程序設(shè)計語言編寫程序代碼編制程序(編程)由程序設(shè)計語言構(gòu)成Some

common

programming

languages

are

BASIC,C,C++,

dBASE,

FORTRAN,

and

Perl.就像用英文能夠?qū)懶≌f,用中文也可以寫出一樣內(nèi)容的小說C是高級程序設(shè)計語言最常用的一種Whence

C30C

evolved

from

a

language

calledB,

written

by

KenThompson

at

Bell

Labs

in

1970.Ken

used

B

to

write

one

of

the

first

implementations

ofUNIX.B

in

turn

was

a

descendant

of

the

language

BCPL(developed

at

Cambridge

(UK)

in

1967),

with

most

of

itsinstructions

removed.So

many

instructions

were

removed

in

going

from

BCPLto

B,

that

Dennis

Ritchie

of

Bell

Labs

put

some

back

in

(in1972),

and

called

the

languageC.The

famous

book

The

C

Programming

Languagewaswritten

by

Kernighan

and

Ritchie

in

1978,

and

was

thedefinitive

reference

book

on

C

for

almost

a

decade.Why

C?31Whither

C?32Characteristic

of

C

語言簡潔、緊湊、靈活

運算符和數(shù)據(jù)類型豐富

程序設(shè)計結(jié)構(gòu)化、模塊化

結(jié)構(gòu)化控制語句

語法限制不大嚴格,程序設(shè)計自由度大

C語言允許直接訪問物理地址

生成目標代碼質(zhì)量高

一般只比匯編程序生成的目標代碼效率低10~20%。

可移植性好

與匯編語言比中級語言33Dialects

of

CCommon

CUntil

recently

there

was

one

dominant

form

of

the

C

language.This

was

the

native

UNIX

form,

which

for

historical

reasons

isknown

as

either

Bell

Labs

C,

after

the

most

popular

compiler,

or

K.&R.

C,

after

the

authors

of

the

most

popular

textbook

on

thelanguage.

It

is

now

often

called

"Classic

C"

.ANSI

CThe

American

National

Standards

Institute

defined

a

standardforC,

eliminating

much

uncertainty

about

the

exact

syntax

of

thelanguage.

This

newcomer,

called

ANSI

C,

proclaims

itselfthestandard

version

of

the

language.

As

such

it

will

inevitablyovertake,

and

eventually

replace

common

C.ANSI

C

does

incorporate

a

few

improvements

over

the

old

common

C.

The

main

difference

is

in

the

grammar

of

the

language.The

form

of

function

declarations

has

been

changed

makingthemrather

more

like

Pascal

procedures.34The

helloprogramWe

will

begin

our

study

of

systems

by

tracing

the

lifetimeof

the

hello

program,

from

the

time

it

is

created

by

aprogrammer,

until

it

runs

on

a

system,

prints

its

simplemessage,

andterminates.As

we

follow

the

lifetime

of

the

program,

we

will

brieflyintroduce

the

key

concepts,

terminology,

and

componentsthat

come

into

play.35Information is

Bits

inContext36Our

hello

program

begins

life

as

a

source

program

(orsource

file)

that

the

programmer

creates

with

an

editorand

saves

in

a

text

file

calledhello.c.The

source

program

is

a

sequence

of

bits,

each

with

avalue

of

0

or

1,

organized

in8-bit

chunks

called

bytes.Each

byte

represents

some

text

character

in

theprogram.Most

modern

systems

represent

text

characters

using

theASCII

standard

that

represents

each

character

with

aunique

byte-sized

integer

value.Information is

Bits

inContextThe

hello.c

program

is

stored

in

a

file

as

a

sequenceof

bytes.

Each

byte

has

an

integer

value

thatcorresponds

to

some

character.37Programs

Translated by

OtherPrograms38The

hello

program

begins

life

as

a

high-level

C

programbecause

it

can

be

read

and

understand

by

human

beingsin

that

form.However,

in

order

to

run

hello.c

on

the

system,

theindividual

C

statements

must

be

translated

by

otherprograms

into

a

sequence

of

low-level

machine-languageinstructions.These

instructions

are

then

packaged

in

a

form

called

anexecutable

object

program,

and

stored

as

a

binarydiskfile.

Object

programs

are

also

referred

to

as

executableobject

files.Compilation

System39Preprocessing

phase40The

preprocessor

(cpp)modifies

the

original

C

programaccording

to

directives

that

begin

with

the

#

character.For

example,the#include

<stdio.h>command

in

line

1

of

hello.c

tells

thepreprocessor

to

read

the

contents

of

the

system

headerfile

stdio.h

and

insert

it

directly

into

the

program

text.The

result

is

another

C

program,

typically

with

the

.i

suffix.Compilation

phase41The

compiler

(cc1)

translates

the

text

file

hello.i

into

thetext

file

hello.s,

which

contains

an

assembly-languageprogram.Each

statement

in

an

assembly-language

program

exactlydescribes

one

low-level

machine-language

instruction

in

astandard

text

form.Assembly

language

is

useful

because

it

provides

acommon

output

language

for

different

compilers

fordifferent

high-level

languages.For

example,

C

compilers

and

Fortran

compilers

bothgenerate

output

files

in

the

same

assembly

language.Assembly

phase42Next,

the

assembler

(as)

translates

hello.s

into

machine-language

instructions,

packages

them

in

a

form

known

asa

relocatable

object

program,

and

stores

the

result

in

theobject

file

hello.o.The

hello.o

file

is

a

binary

file

whose

bytes

encodemachine

language

instructions

rather

than

characters.If

we

were

to

view

hello.o

with

a

text

editor,

it

wouldappear

to

be

gibberish.Linking

phase43Notice

that

our

hello

program

calls

the

printf

function,which

is

part

of

thestandard

C

library

provided

by

every

Ccompiler.The

printf

function

resides

in

a

separate

precompiledobject

file

called

printf.o,

which

must

somehow

be

mergedwith

our

hello.o

program.The

linker

(ld)

handles

this

merging.

The

result

is

the

hellofile,

which

is

an

executable

object

file(or

simplyexecutable)

that

is

ready

to

be

loaded

into

memory

andexecuted

by

the

system.Running

the

hello

Program44begin

to

understand

what

happens

when

we

run

ourexample

program?Initially,

the

shell

program

is

executing

its

instructions,waiting

for

us

to

type

acommand.As

we

type

the

characters

hello

at

the

keyboard,

theshell

program

reads

each

one

into

a

register,and

thenstores

it

in

memory,

as

shown

in

Figure。PCALUReading

thehellocommand

fromthe

keyboardI/OBridgeMainMemoryUSBcontrollerGraphicadapterDiskcontrollerdiskMemoryInterfaceMouseKeyboard

Display“hello”CPUMemory

BusI/O

BusSystem

BusExpansion

slots

forother

devices

suchas

network

adaptersRegister

FileUser

types

“Hello”45LoadWhen

we

hit

the

enter

key

on

the

keyboard,

theshellknows

that

we

have

finished

typing

the

command.The

shell

then

loads

the

executable

hello

file

by

executinga

sequence

of

instructions

that

copies

the

code

and

datain

the

hello

object

file

from

disk

to

main

memory.

The

datainclude

the

string

of

characters

”hello,

world\n”

that

willeventually

be

printed

out.Using

a

technique

known

asdirect

memory

access

(DMA),

the

data

travels

directly

from

disk

to

main

memory,without

passing

through

the

processor.

This

step

is

showninFigure46PCALULoading

the

executablefrom

diskinto

main

memoryI/OBridgeMainMemoryUSBcontrollerGraphicadapterDiskcontrollerdiskMemoryInterfaceMouseKeyboardDisplay“hello

world\n”hello

codeCPUMemory

BusI/O

BusSystem

BusExpansion

slots

forother

devices

suchas

network

adaptersRegister

FileHello

executable

stored

on

dis4k7ExecuteOnce

the

code

and

data

in

the

hello

object

file

are

loadedinto

memory,

the

processor

begins

executing

the

machine-language

instructions

in

the

hello

program’s

main

routine.These

instruction

copy

the

bytes

in

the

”hello,

world\n”string

from

memory

to

the

register

file,

and

from

there

tothe

display

device,

where

they

are

displayed

onthescreen.48PCALUWriting

the

output string

frommemory

to the

displayI/OBridgeMainMemoryUSBcontrollerGraphicadapterDiskcontrollerdiskMemoryInterfaceMouseKeyboardDisplay“hello

world\n”hello

codeCPUMemory

BusI/O

BusSystem

BusExpansion

slots

forother

devices

suchas

network

adaptersRegister

Filehello

worldHello

executable

stored

on

d4i9skMachine

Language50計算機能夠認識的語言計算機的基礎(chǔ)是數(shù)字電路機器語言就是數(shù)字電路里的電信號都是二進制文件一條機器語言成為一條指令指令是不可分割的最小功能單元可執(zhí)行程序與源程序比較51可執(zhí)行程序可以運行的程序不知道程序設(shè)計員的思想不能修改原有的流程完成功能固定源程序可以生成可執(zhí)行程序知道程序設(shè)計員的思想能修改原有的流程完成功能可以修改進入C編程52需要的條件:C語言集成環(huán)境:包含了編輯編譯鏈接調(diào)試運行等工具UsingC: Seven

Steps53C程序的上機步驟編輯鏈接編譯執(zhí)行程序代碼的錄入,生成源程序*.c語法分析查錯,翻譯生成目標程序*.obj與其它目標程序或庫鏈接裝配,生成可執(zhí)行程序*.exefile.exe54A

Simple

Program/*example1.1

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論