Structured Programming in C - PowerPoint PPT Presentation

1 / 84
About This Presentation
Title:

Structured Programming in C

Description:

What does CPU stand for? Central Processing Unit (CPU) What is ... Random storage: floppy disk, zip disk, CD-ROM, etc. Static storage. ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 85
Provided by: srot4
Learn more at: http://parasol.tamu.edu
Category:

less

Transcript and Presenter's Notes

Title: Structured Programming in C


1
Welcome to CPSC 206
  • Structured Programming in C

2
Lecture Information
  • http//people.cs.tamu.edu/ychen/Teaching/CPSC206

3
Midterm 1
  • Time Sept 30, Thur., 800-915AM
  • Location HRBB 124
  • Closed book
  • No textbook, No notes, No calculator
  • You are required to bring
  • Answer sheet SCANTRON (NCS MP90051 or
    0-101607-TAMU) (You can find it in MSC
    Bookstore)
  • No. 2 pencils, eraser
  • Student ID

4
Review 1
  • Topic 1 Introduction to Computer Science
  • Topic 2 Overview of C Language
  • Chapter 1
  • Section 1.1 1.8
  • Chapter 2
  • Section 2.1 2.12

5
Topic 1 Introduction to Computer Science
  • Part I an overview of computer science.
  • Part II computer hardware and software.
  • Part III computer languages.

6
Introduction to Computer SciencePart I an
overview of computer science.
  • What is computer Science?
  • Basic idea about research areas in computer
    science.
  • Architecture
  • Operating System
  • Computability theory
  • Algorithm design
  • Programming Language
  • Compiler
  • Complexity theory

7
Introduction to Computer SciencePart I an
overview of computer science.
  • Basic idea about research areas in computer
    science. (contd.)
  • Human-computer interaction
  • Artificial Intelligence
  • Software engineering
  • Networking, Parallel and Distributed System.

8
Introduction to Computer SciencePart I an
overview of computer science.
Sample Question Software engineering focuses on
how to develop applications for networks. True of
False
False
9
Introduction to Computer SciencePart II
computer hardware and software
  • Computer organization.
  • Data representation
  • Main Memory
  • Central Processing Unit (CPU)
  • Secondary Storage
  • Input / Output
  • Solving problems on computers.
  • System Design
  • Algorithm Development and Representation
  • Programming

10
Computer Organization
  • Data Representation
  • How data is stored and processed?
  • All data is stored and processed in binary form,
    that is, as a series of 0s and 1s.
  • The definition of Bit, Byte, Word

Sample Question The number of bits in one Bytes
is a) 4 b) 8 c) 16 d) 32 e) none of the above
b
11
Computer Organization
  • Data Representation
  • Binary number
  • Convert a decimal number to a binary number
  • What is the decimal value of a binary number

Sample Question The decimal value of a binary
number 100001 is a) 33 b) 1 c) 100001 d) 17 e)
none of the above
a
12
Computer Organization
  • Data Representation (contd.)
  • How a signed number is represented in binary
    form?
  • In a binary representation, the leftmost bit is a
    sign bit followed by the magnitude bits.  
  • Sign-bit 0 implies a positive number, 1 implies
    a negative number.
  • How character is represented? (ASCII coding
    system)
  • Each character is represented in a 7-bit format.

13
Computer Organization
Sample Question Consider the data representation
of signed integers using 4 bits, in which one bit
is used as the sign bit. The maximum decimal
integer that can be represented is ___ a) 1111 b)
111 c) 7 d) 15 e) none of the above
c
14
Computer Organization
  • Memory
  • What is memory?
  • It is part of a computers electronic circuitry
    holding the binary data which is processed by the
    computers program.

15
Computer Organization
  • CPU
  • What does CPU stand for?
  • Central Processing Unit (CPU)
  • What is
  • Control Unit, ALU, Program Counter, Instruction
    Counter?
  • What is program? How a program is executed?
  • Machine Cycle
  • Hardware characteristics
  • Clock speed
  • MIPS

16
Computer Organization
  • Central Processing Unit (CPU)

CPU reads program instructions from main memory.
CPU executes one instruction at a time until
completion.
CPU
Arithmetic/Logic Unit (ALU)
Control unit
17
Computer Organization
  • Central Processing Unit (CPU) (contd.)

Program Counter (PC)
Instruction Counter (IC)
18
Computer Organization
  • Central Processing Unit (CPU) (contd.)
  • What is program?
  • Program is a set of instructions
  • How a program is executed?
  • Program is loaded into memory.
  • In each Machine Cycle
  • Fetch, Decode, Execute
  • Machine Cycle is repeated until completion.

19
Computer Organization
  • Central Processing Unit (CPU) (contd.)
  • In each Machine Cycle
  • Fetch
  • The control unit loads the instruction pointed to
    by PC to IC.
  • PC is updated to the address of the next
    instruction.
  • Decode control unit decodes the fetched
    instruction and determines the required action to
    take.
  • Execute control unit activates the appropriate
    circuitry.

20
Computer Organization
  • Central Processing Unit (CPU) (contd.)
  • Hardware characteristics
  • The speed of a computer can be quoted as
  • Clock speed, e.g. 100MHz means 100 million cycles
    per second.
  • MIPS, a million instructions per second.

21
Computer Organization
  • Secondary Storage
  • Mass storage devices
  • Sequential storage tape.
  • Random storage floppy disk, zip disk, CD-ROM,
    etc.
  • Static storage.
  • it doesn't require power to retain stored data.
  • Slower speed.

22
Computer Organization
  • Input
  • Keyboard, mouse etc
  • Output
  • Monitor, printer etc

23
Computer Organization
  • How CPU, Memory, Storage, Input and Output work
    together?
  • IPOS Cycle

24
Computer Organization
  • I - Input

P - Process
O - Output
S - Storage
Binary Representation
P
CPU
Memory
Secondary Storage
25
Introduction to Computer SciencePart II
computer hardware and software
  • Computer organization.
  • Data representation
  • Main Memory
  • Central Processing Unit (CPU)
  • Secondary Storage
  • Input / Output
  • Solving problems on computers.
  • System Design
  • Algorithm Development and Representation
  • Programming

26
Solving problems on computers.
  • System Design
  • Top-down
  • break the problem into smaller and smaller
    sub-problems until they can be solved trivially.
  • Bottom-up
  • start by designing the low-level details, and
    then decide how these will be put together to
    create the entire system

27
Solving problems on computers.
  • Algorithm Development and Representation
  • What is algorithm?
  • a specification of the series of steps which must
    be followed in order to solve a problem or
    accomplish a task.
  • Algorithm Representation
  • Pseudo Code
  • Decision Trees
  • Flow Charts

28
Solving problems on computers.
  • Programming
  • What is programming?
  • Programming is the process of translating a
    problems solution into instructions that a
    computer can process.

29
Solving problems on computers.
  • Programming Paradigms
  • Procedural Programming is based upon the concept
    of the modularity.
  • A main procedural program is composed of one or
    more modules. Each module is composed of one or
    more subprograms.
  • Declarative programming
  • describes to the computer a set of conditions and
  • lets the computer figure out how to satisfy them.

30
Solving problems on computers.
  • Programming Paradigms
  • Object oriented programming
  • A computer program is composed of a collection of
    individual units, called objects.
  • Operations are provided for each class of
    objects.
  • Operations change the state of an object.
  • To make the overall computation happen, the
    objects interact through their own operations and
    their own data.

31
Introduction to Computer SciencePart III
Computer Language
  • Why Computer needs language?
  • Computer requires an unambiguous language
  • Computer Language are designed to be unambiguous.
  • Precedence and associativity determine precisely
    how expressions are evaluated.

32
Introduction to Computer SciencePart III
Computer Language
  • C language
  • C is a general purpose programming language.
  • C is a middle level language.
  • It combines the elements of high-level language
    with the functionality of low-level language.
  • C is a structured language.
  • It allows programmer to divide program into
    modules.

33
Introduction to Computer SciencePart III
Computer Language
There is only one programming language that a
computer can actually understand and execute its
own native binary machine code.
  • Languages are classified as
  • low level if they are close to machine code and
  • high level if each language statement corresponds
    to many machine code instructions.

34
  • End of Review of Topic 1
  • Introduction of Computer Science

35
Chapter 1
  • Review Outline
  • A brief history of C
  • Features of C
  • Get Ready to Program
  • General form of a simple program
  • main function
  • variable declarations

36
Chapter 1
  • Review Outline (contd)
  • initialization
  • preprocessing directive
  • include
  • define
  • printf and scanf
  • printf
  • \n
  • scanf
  • while statement

37
Chapter 1
  • A brief history of C
  • 1960s, CPL (Combined Programming Language)
  • capable of both high level machine independent
    programming and would still allow the programmer
    to control the behavior of individual bits of
    data.
  • too large for use in many applications.
  • 1967, BCPL (Basic CPL) a scaled down version of
    CPL.
  • In 1970, B a scaled down version of BCPL
  • written specifically for use in systems
    programming.
  • In 1972, C
  • Dennis Ritchie returned some of features of BCPL
    to the B language in the process of developing C.

38
Chapter 1
  • 2. Features of C
  • Small
  • The core language of C is small
  • Non-essential functionality, such as math
    functions or file handling, is provided by a
    standardized set of library routines.
  • Portable
  • easily moved from machine to machine.
  • Powerful
  • it is able to access low level commands
  • Useful in writing system software
  • Procedural Programming paradigm
  • Allows programmer to divide program into modules.

39
Chapter 1
  • 3. Get Ready to Program
  • The Programming Process
  • Specify the task
  • Discover an algorithm for its solution
  • Code the algorithm in C
  • Test the code
  • The cycle of the programming process

edit
pico / vi
gcc / cc
40
Chapter 1
  • 4. General Form of a simple program

The general form of a program preprocessing
directives int main () declarations
statements
41
Chapter 1
  • 4. General Form of a simple program (Contd).
  • main function
  • Every program has a main function.
  • The execution starts at main function.
  • Declaration
  • All variables in a program must be declared
    before they can be used.
  • The data type of a variable specifies what kind
    of data can be stored in the variable.

42
Chapter 1
  • 4. General Form of a simple program (Contd).

Sample Question int main() int c14,
c22 printf(Sum d\n, sumc) What is the
output? a) 6 b) 4 c) sumc d) error
d
43
Chapter 1
  • 5. Initialization
  • A variable can be initialized when it is
    declared.
  • Constants or constant expressions can be used to
    initialize a variable.
  • Declared variables can be used to initialize a
    variable.
  • a variable cannot be used before it has been
    declared.

44
Chapter 1
  • 6. preprocessing directive
  • include filename
  • The preprocessor replaces the line with a copy of
    the named file.
  • define A B
  • It affects only those lines in the file that come
    after it.
  • All occurrences (after this line) of the
    identifier A, except in quoted string, will be
    changed to B.

45
Chapter 1
  • 6. preprocessing directive (Contd)

Sample Question include ltstdio.hgt define Sumc
c1c2c3 int main() int c14,
c22 printf(Ave d\n, Sumc) What is the
ouput? a) 6 b) 4 c) sumc d) error
d
46
Chapter 1
  • 6. preprocessing directive (Contd)

Sample Question include ltstdio.hgt define Sumc
c1c2 int main() int c14, c22 printf(Ave
d\n, Sumc) What is the ouput? a) 6 b) 4 c)
sumc d) error
a
47
Chapter 1
  • 6. preprocessing directive (Contd)

Sample Question include ltstdio.hgt define Sumc
c1c2 int main() int c14, c22 printf(Ave
d\n, Sumc/2) a) 5 b) 3 c) 4 d) 2
a
48
Chapter 1
  • 7. printf and scanf
  • Arguments control_string and other_arguments
  • control_string contains formats, called
    conversion specifications, which are matched with
    other arguments.

49
Chapter 1
  • 7. printf and scanf (contd)
  • printf How to specify format using conversion
    specification?
  • field_widthconversion_character
  • field_width.precisionconversion_character
  • Example
  • printf(7.2f, 1.221)
  • printf(7d, 1221)

50
Chapter 1
  • 7. printf and scanf (contd)
  • printf How to specify format using conversion
    specification?
  • field_widthconversion_character
  • field_width.precisionconversion_character
  • conversion_character how the data is printed?
  • c character
  • d decimal integer
  • f floating-point number
  • s, e, g

table of conversion character, page 16
51
Chapter 1
  • 7. printf and scanf (contd)
  • printf

Sample Question What is the output? pirntf(8.2f
, 1.234) a)_ _ _ 1.23 b) 1.234 c) _ _ _1.234
d) _ _ _ _ 1.23
d
52
Chapter 1
  • 7. printf and scanf (contd)
  • \n newline

Sample Question include ltstdio.ngt int
main(void) printf(8.2f\n\n\n,
1.234) printf(8.2f\n\n\n, 1) printf( \nd
, 2) return 0 if 1.234 is printed at line
1, which line is 2 written? a)5 b) 7 c) 8 d)6
c
53
Chapter 1
  • 7. printf and scanf (contd)
  • scanf
  • Arguments control_string and other_arguments
  • control_string conversion specifications
  • conversion_character how the data is read?
  • c character
  • d decimal integer
  • f floating-point number
  • lf floating-point number (double)
  • Lf floating-point number (long double)
  • s string
  • other_arguments are addresses

table of conversion, page 18
54
Chapter 1
  • 7. printf and scanf (contd)
  • scanf
  • Example scanf(d, x)
  • address operator
  • d how to interpret the input stream
  • the input characters typed at the keyboard is
    interpreted as a decimal integer
  • x where to store the value
  • the value of the decimal integer is stored at the
    address of x.

55
Chapter 1
  • 7. printf and scanf (contd)
  • scanf

Sample Question Which format should be used to
read a long double value? a) Lf b)lf c)
f d) L
a
56
Chapter 1
  • 8. while
  • while (expression)
  • statement
  • statement is executed as long as the value of
    expression is true
  • true any non-zero value

57
Chapter 1
  • 8. while (contd)

/ Some powers of 2 are printed. / include
ltstdio.hgt int main(void) int e 0,
power_of_two 1 while (e lt 10)
printf("5d", power_of_two 2)
printf("\n") return 0
How many times the body of the loop is executed?
58
  • End of Review of
  • Chapter 1

59
Chapter 2
  • Review Outline
  • Comments
  • Keywords
  • Identifiers
  • Constants
  • floating numbers
  • integer constants

60
Chapter 2
  • Review Outline (contd)
  • String Constants
  • Operators and Punctuators
  • Precedence and Associativity of Operators
  • Division
  • Modulus
  • Increment and Decrement Operators
  • Assignment Operators
  • C System

61
Chapter 2
  • Comments
  • What is comment?
  • Arbitrary strings of symbols placed between the
    delimiters / and /.
  • Single line comment // text
  • The compiler changes each comment into a single
    blank character.
  • Rules
  • Multi-line comments cannot be placed in the
    middle of a keyword or identifier.
  • Multi-line comments may not be nested.

62
Chapter 2
  1. Comments (contd)

Sample Question Which of the following is
correct? a) / comment //a single line comment
/ b) int main(v/ comment/oid) c) int
var/comment/1 d) / comment / comment / /
a
63
Chapter 2
  • Keyword
  • What is Keywords?
  • Keywords are explicitly reserved words that have
    a strict meaning as individual tokens in C.
  • Examples of Keywords
  • Data Type int, char, long, short
  • Keywords cannot be redefined or used in other
    contexts.

The table of keywords, page 46
64
Chapter 2
  • Identifier
  • What is identifier?
  • The names of variables, functions, labels and
    other user-defined items are called identifier.
  • Special identifier
  • Keywords, names of functions in C library, main
  • Rules
  • composed of letters, digits, and underscore _ .
  • The first character must be a letter or
    underscore.
  • case-sensitive
  • would not be defined as the special identifiers

65
Chapter 2
  1. Identifier (contd)

Sample Question Which of the following is
incorrect? a) int comment b) int _int c) int
int_ d) int 4int
d
66
Chapter 2
  • Constants
  • Floating constants
  • float an F suffix
  • 1.22F
  • double unsuffixed floating constant
  • 1.22
  • long double an L suffix
  • 1.22L
  • Character constants
  • A

67
Chapter 2
  • Constants (contd)
  • integer constants decimal, octal, hexadecimal
  • Representation of integer constants
  • Decimal integer 17
  • Octal integer 017
  • Hexadecimal integer 0x17
  • Conversion between different types
  • an octal integer lt-gt a decimal integer
  • a hexadecimal integer lt-gt a decimal integer

68
Chapter 2
  • Constants (contd)
  • A review of binary number
  • What is the decimal value of a binary integer?
  • A binary integer a ik-1 ik-2 ..i0
  • The decimal value is
  • ik-1 2k-1 ik-2 2k-2 .. i0 20
  • Example
  • The decimal value of a binary integer
  • a i3i2i1i01101 is
  • 1 23 1 22 0 21 1 20 13

69
Chapter 2
  • Constants (contd)
  • Octal Integer
  • What is the decimal value of an octal integer?
  • An octal integer a ik-1 ik-2 ..i0
  • The decimal value is
  • ik-1 8k-1 ik-2 8k-2 .. i0 80
  • Example
  • The decimal value of an octal integer
  • a i3i2i1i07121 is
  • 7 83 1 82 2 81 1 80 3665

70
Chapter 2
  • Constants (contd)
  • Hexadecimal Integer
  • What is the decimal value of a hexadecimal
    integer?
  • A hexadecimal integer a ik-1 ik-2 ..i0
  • The decimal value is
  • ik-1 16k-1 ik-2 16k-2 .. i0 160
  • Example
  • The decimal value of a hexadecimal integer
  • a i3i2i1i07121 is
  • 7 163 1 162 2 161 1 160 28961

71
Chapter 2
  1. Constants (contd)

Sample Question Which of the decimal value of an
octal integer 17? a) 15 b) 17 c) 23 d) 8
a
Sample Question What is the output? print(d,
017) a) 15 b) 17 c) 23 d) 017
a
72
Chapter 2
  • String Constants
  • String constant is a sequence of characters
    enclosed in a pair of double quote marks.
  • Special characters \, \\
  • You mustn't split a string constant across lines
  • Two string constants that are separated only by
    white space are concatenated by the compiler into
    a single string.

73
Chapter 2
  • Operator and Punctuator
  • Precedence and Associativity
  • determine precisely how expressions are
    evaluated.
  • Precedence of operators indicates when they will
    be evaluated.
  • Associativity
  • left to right Operations are performed from
    left to right
  • Examples ,-,/,
  • right to left Operations are performed from
    right to left
  • Examples (prefix), --(prefix)

74
Chapter 2
  • Operator and Punctuator
  • Division and Modulus
  • Division a/b an integer expression divided by
    another integer expression yields an integer
    value.
  • Any fractional part is discarded.
  • Modulus ab the remainder after a is divided by
    b.
  • If a or b is negative, the results of division
    and modulus are system-dependent
  • In a/b and ab, the value of b cannot be zero.

75
Chapter 2
  • Operator and Punctuator
  • Increment Operator i, i
  • Each of the expressions i and i has a value.
  • i
  • the stored value of i is incremented first
  • the expression takes as its value the new stored
    value of i
  • i
  • the expression takes as its value the current
    stored value of i
  • the stored value of i is incremented

76
Chapter 2
  • Operator and Punctuator
  • Decrement Operator i-- and --i
  • Each expression has a value.
  • --i
  • the stored value of i is decremented by 1
  • the expression takes as its value the new stored
    valued of i
  • i--
  • the expression takes as its value the current
    stored valued of i
  • the stored value of i is decremented by 1

77
Chapter 2
  • Operator and Punctuator
  • Increment and Decrement Operator
  • Rules
  • Applied to variables but not
  • to constants or
  • ordinary expressions

78
Chapter 2
  • Operator and Punctuator
  • Increment and Decrement Operator

Sample Question What is the output? int
i0x11 print(d, i) a) 15 b) 17 c) 23 d)
017
b
79
Chapter 2
  • Operator and Punctuator
  • Assignment Operator
  • op , -, , / , ,
  • Semantics
  • variable op expression
  • equivalent to variable variable op (expression)
  • Example
  • var expr ? varvar expr
  • a 3 ? a a 3
  • if a was 4 before the assignment, then a 12
    after that.

80
Chapter 2
  • Operator and Punctuator
  • Assignment Operator

Sample Question What is the output? int
i0x11 print(d, i 0x11) a) 15 b) 17 c)
34 d) 017
c
81
Chapter 2
  • C System
  • The C system consists of
  • C language,
  • The preprocessor
  • The compiler
  • The library
  • Other tools useful to the programmer, such as
    editors and debugger.

82
Chapter 2
  • C System (contd)
  • C Standard library a collection of header files
    and library files.
  • Header file
  • The names and characteristics of functions are
    included into computer files called header file.
  • Library file
  • The actual implementation of functions are
    separated into a library file.

83
Chapter 2
  • C System (contd)
  • How to use a function in the standard library?
  • The programmer needs to provide the function
    prototype.
  • Including appropriate header files.
  • Do we need to locate the function in the library
    file? No.
  • The system knows where to find the code that
    corresponds to functions from the standard
    library.

84
  • End of Review of
  • Chapter 2
Write a Comment
User Comments (0)
About PowerShow.com