Title: Structured Programming in C
1Welcome to CPSC 206
- Structured Programming in C
2Lecture Information
- http//people.cs.tamu.edu/ychen/Teaching/CPSC206
3Midterm 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
4Review 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
5Topic 1 Introduction to Computer Science
- Part I an overview of computer science.
- Part II computer hardware and software.
- Part III computer languages.
6Introduction 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
7Introduction 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.
8Introduction 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
9Introduction 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
10Computer 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
11Computer 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
12Computer 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.
13Computer 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
14Computer Organization
- Memory
- What is memory?
- It is part of a computers electronic circuitry
holding the binary data which is processed by the
computers program.
15Computer 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
16Computer 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
17Computer Organization
- Central Processing Unit (CPU) (contd.)
Program Counter (PC)
Instruction Counter (IC)
18Computer 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.
19Computer 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.
20Computer 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.
21Computer 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.
22Computer Organization
- Input
- Keyboard, mouse etc
- Output
- Monitor, printer etc
23Computer Organization
- How CPU, Memory, Storage, Input and Output work
together? - IPOS Cycle
24Computer Organization
P - Process
O - Output
S - Storage
Binary Representation
P
CPU
Memory
Secondary Storage
25Introduction 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
26Solving 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
27Solving 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
28Solving problems on computers.
- Programming
- What is programming?
- Programming is the process of translating a
problems solution into instructions that a
computer can process.
29Solving 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.
30Solving 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.
31Introduction 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.
32Introduction 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.
33Introduction 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
35Chapter 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
36Chapter 1
- Review Outline (contd)
- initialization
- preprocessing directive
- include
- define
- printf and scanf
- printf
- \n
- scanf
- while statement
37Chapter 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.
38Chapter 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.
39Chapter 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
40Chapter 1
- 4. General Form of a simple program
The general form of a program preprocessing
directives int main () declarations
statements
41Chapter 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.
42Chapter 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
43Chapter 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.
44Chapter 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.
45Chapter 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
46Chapter 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
47Chapter 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
48Chapter 1
- 7. printf and scanf
- Arguments control_string and other_arguments
- control_string contains formats, called
conversion specifications, which are matched with
other arguments.
49Chapter 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)
50Chapter 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
51Chapter 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
52Chapter 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
53Chapter 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
54Chapter 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.
55Chapter 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
56Chapter 1
- 8. while
- while (expression)
- statement
- statement is executed as long as the value of
expression is true - true any non-zero value
57Chapter 1
/ 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
59Chapter 2
- Review Outline
- Comments
- Keywords
- Identifiers
- Constants
- floating numbers
- integer constants
60Chapter 2
- Review Outline (contd)
- String Constants
- Operators and Punctuators
- Precedence and Associativity of Operators
- Division
- Modulus
- Increment and Decrement Operators
- Assignment Operators
- C System
61Chapter 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.
62Chapter 2
- 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
63Chapter 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
64Chapter 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
65Chapter 2
- Identifier (contd)
Sample Question Which of the following is
incorrect? a) int comment b) int _int c) int
int_ d) int 4int
d
66Chapter 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
67Chapter 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
68Chapter 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
69Chapter 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
70Chapter 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
71Chapter 2
- 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
72Chapter 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.
73Chapter 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)
74Chapter 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.
75Chapter 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
76Chapter 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
77Chapter 2
- Operator and Punctuator
- Increment and Decrement Operator
- Rules
- Applied to variables but not
- to constants or
- ordinary expressions
78Chapter 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
79Chapter 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.
80Chapter 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
81Chapter 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.
82Chapter 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.
83Chapter 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