ANSI C Programming Course
ANSI C Programming Course
INSTITUTE OF
ELECTRONICS
This study guide contains excerpts from the A First Book of ANSI C Instructor's resources and the book
A First Book of ANSI C by Gary Bronson
Contents
Chat with Your Instructor ......................................................................................................... 3
Chapter 1 - Introduction to Computer Programming ............................................................ 4
Chapter 2 - Getting Started in C Programming .................................................................... 10
ANSI C Lesson 9201C Exam ................................................................................................ 15
Chapter 3 - Processing and Interactive Input ........................................................................ 17
ANSI C Lesson 9202C Exam ................................................................................................ 21
Chapter 4 - Selection ................................................................................................................. 24
Chapter 5 - Repetition .............................................................................................................. 28
ANSI C Lesson 9203C Exam ................................................................................................ 31
Chapter 6 - Modularity Using Functions: Part I ................................................................... 35
ANSI C Lesson 9204C Exam ................................................................................................ 38
Chapter 7 - Modularity Using Functions: Part II .................................................................. 42
ANSI C Lesson 9205C Exam ................................................................................................ 45
Chapter 8 - Arrays .................................................................................................................... 49
ANSI C Lesson 9206C Exam ................................................................................................ 52
Chapter 9 - Character Strings ................................................................................................. 56
Chapter 10 - Data Files ............................................................................................................. 60
ANSI C Lesson 9207C Exam ................................................................................................ 63
Chapter 11 - Arrays, Addresses, and Pointers ....................................................................... 67
ANSI C Lesson 9208C Exam ................................................................................................ 71
Chapter 12 - Structures ............................................................................................................ 76
Chapter 13 - Dynamic Data Structures .................................................................................. 80
ANSI C Lesson 9209C Exam ................................................................................................ 84
Chapter 14 - Additional Capabilities ...................................................................................... 88
ANSI C Lesson 9210C Exam ................................................................................................ 91
Appendix - Quick Quiz Answers ............................................................................................. 95
Quick Quizzes
Additional Resources
Key Terms definitions
The topics follow the section-by-section format of the book and are intended to give you a
range of ideas for your understanding.
Students often learn from investigation of programming topics. For this reason, there are many
topic tips throughout this guide. These topic tips may require you to solve a problem, work
through a new direction in technology or even attempt to predict the future. All of the topic tips
are meant to be thought provoking and to help you apply what you are learning.
Each chapter in the Study Guide also includes a set of interesting Additional Resources, which
are Web links to topics of interest. Finally, a list of definitions of Key Terms is included for
each chapter.
A First Book of ANSI C, Fourth Edition, covers many exciting topics, and the staff believes that
you will share in the excitement.
If you have a technical problem, we recommend the following:
Many software products include on-line help. If the answer is not available in the
printed materials, try using the Help feature of your software.
Feel free to call the instruction department during business hours (8:30 AM to 6 PM
Eastern time), Monday through Friday, and Saturday during the weekend hours (8:30
AM to 5 PM Eastern time). Be prepared to describe which lesson you're working on and
the problem you're having.
Lesson Contents
Overview
Objectives
Quick Quizzes
Additional Resources
Key Terms
Chapter Notes
Overview
Chapter 1 provides an introduction to computer programming. You will learn about the
history of computers and about computer hardware. You will also learn about computer
languages, how to classify them, and how executable programs are generated. Students
will learn about algorithms and about the software development process. Through a
case study, you will put to practice some of the concepts learned in the chapter. Finally,
you learn about common programming errors and how to avoid them.
Objectives
Topic Tips
History and Hardware
Topic Tip
The historical note on page 8 introduces Turing Machine. You may note that Turings
contributions to the field of computer science were so important that the highest award
in the field of computing was named after him. For more information, see:
http://en.wikipedia.org/wiki/Turing_Award.
Quick Quiz 1
1. The smallest and most basic data item in a computer is a ____________________; it is really a
switch that can be either open (0) or closed (1).
2. What is the ALU of a computer?
3. What is the control unit of a computer?
4. A(n) ____________________ allows a computer to read or write any one file or program
independent of its position on the storage medium.
Programming Languages
Topic Tip
Topic Tip
Quick Quiz 2
1. What is an assembly language?
2. The program that translates a high-level source program as a complete unit before any
individual statement is executed is called a(n) ____________________.
3. What is a linker?
4. When English phrases are used to describe an algorithm (the processing steps), the description
is called ____________________.
See the footnote on page 29 for a very interesting explanation on why the term bug is
used to refer to program errors.
Quick Quiz 3
1. What is the software development process?
2. When writing a program, a(n) ____________________ structure provides the capability to
make a choice between different instructions, depending on the result of some condition.
3. When writing a program, a(n) ____________________ structure involves summoning into
action specific sections of code as they are needed.
4. What is a repetition structure?
Additional Resources
1. History of Computing Hardware:
http://en.wikipedia.org/wiki/History_of_computing_hardware
2. C Programming Language:
http://en.wikipedia.org/wiki/C_programming_language
3. Algorithm:
http://en.wikipedia.org/wiki/Algorithm
4. Software Development Process:
http://en.wikipedia.org/wiki/Software_development_process
Key Terms
Application software consists of programs written to perform particular tasks required
by users.
The Arithmetic and Logic Unit (ALU) of a computer performs all of the
computations, such as addition, subtraction, comparisons, and so on, that a computer
provides.
Translator programs that translate assembly language programs into machine language
programs are known as assemblers.
Programming languages that use the substitution of word-like symbols, such as ADD,
SUB, MUL, for the binary opcodes, and both decimal numbers and labels for memory
addresses are referred to as assembly languages.
The smallest and most basic data item in a computer is a bit; it is really a switch that
can be either open (0) or closed (1).
6
Operating systems that permit each user to run multiple programs are referred to as both
multiprogrammed and multitasking systems.
Multiuser systems are able to handle multiple users concurrently.
The output produced by the compiler is called an object program, which is a machine
language version of the source code.
Languages with object orientation like C++, Java, Visual Basic, and C#, are known as
object-oriented languages.
Opcode is short for operation code.
Collectively, the set of system programs used to operate and control a computer is
called the operating system.
The three tasks of an overall solution algorithm are the primary responsibility of almost
every problem, and we refer to this algorithm as the Problem-Solver Algorithm.
In a procedural language, the available instructions are used to create self-contained
units, referred to as procedures.
The purpose of a procedure is to accept data as input and transform the data in some
manner to produce a specific result as an output.
The program instructions resulting from coding an algorithm are referred to as program
code, or simply code, for short.
In an automobile, control is provided by the driver, who sits inside of and directs the
car; in a computer, the driver is called a program.
A statement of a problem, or a specific request for a program, is referred to as a
program requirement.
The set of instructions that can be used to construct a program is called a programming
language.
Programming is the process of writing instructions in a language that the computer can
respond to and that other programmers can understand.
When English phrases are used to describe an algorithm (the processing steps), the
description is called pseudocode.
When writing a program, a repetition structure, which is also referred to as looping and
iteration, provides the ability for the same operation to be repeated based on the value
of a condition.
Each field of study has its own name for the systematic method used to design solutions
to problems. In science this method is referred to as the scientific method, while in
engineering disciplines the method is referred to as the systems approach.
When writing a program, a selection structure provides the capability to make a choice
between different instructions, depending on the result of some condition.
When writing a program, a sequence structure defines the order in which instructions
are executed by the program.
Another term for a program or set of programs is software.
The technique used by professional software developers for understanding the problem
that is being solved and for creating an effective and appropriate software solution is
called the software development process.
Programs written in a computer language (high or low level) are referred to
interchangeably as both source programs and source code.
A structured language is a high-level procedural language, such as C, that enforces
structured procedures.
Procedures conforming to structure guidelines are known as structured procedures.
System software is the collection of programs that must be readily available to any
computer system to enable the computer to operate.
8
At a Glance
Lesson Contents
Overview
Objectives
Quick Quizzes
Additional Resources
Key Terms
Lesson Assignment
Chapter Notes
Overview
Chapter 2 provides an introduction to C programming. You learn about good
programming style and about the data types available in C. You also learn about
arithmetic operations, and how to declare and initialize variables. You put to practice
the concepts learned through a case study on temperature conversion. Finally, you learn
about some common programming and compiler errors, and how to avoid them.
Objectives
Introduction to C programming
Programming style
Data types
Arithmetic operations
Variables and declarations
Case Study: Temperature conversion
Common programming and compiler errors
10
Topic Tips
Introduction to C Programming
Topic Tip
In a computer language, a token is the smallest unit of the language that has a
unique meaning. Thus, the reserved words, programmer-defined identifiers, and
all special mathematical symbols, such as + and -, are considered tokens of the C
language.
Topic Tip
What if you want to include the backslash character in a string? Just precede it
with another backslash, as \\.
Programming Style
Topic Tip
Quick Quiz 1
1. What is an identifier?
2. What is a function header line?
3. The two characters \ and n, when used together, are called a(n) ___________________.
4. A(n) ____________________ is a word that is predefined by the programming
language for a special purpose and can only be used in a specified manner for its
intended purpose.
Data Types
Topic Tip
For a discussion on the meaning of the term precision, see the box on page 64.
Quick Quiz 2
1. What is a data type?
11
Topic Tip
Several tips on how to select variable names have been provided throughout this
chapter. Try to describe the ones you can remember and write the list on a sheet
of paper. Next, add any other tips missing from the list (see the Programming
Note on page 82).
Quick Quiz 3
1. ____________________ are simply names given by programmers to computer storage
locations.
2. What is an assignment statement?
3. What is a definition statement?
4. When a declaration statement provides an initial value, the variable is said to be
____________________.
Additional Resources
1. Indent Style:
http://en.wikipedia.org/wiki/Indent_style
2. C/C++ Data Types:
http://www.cppreference.com/data_types.html
3. C Tutorial - Lesson 2: Variables:
http://cplus.about.com/od/beginnerctutoria1/l/aa030302a.htm
4. Operator Precedence:
http://computer.howstuffworks.com/c37.htm
Key Terms
12
Items passed to a function are always placed within the function name parentheses and
are called arguments.
Data transmitted into a function at run time are referred to as arguments of the
function.
The operators used for arithmetic operations are called arithmetic operators.
An assignment statement tells the computer to assign a value to (that is, store a value
in) a variable.
Associativity is the order in which operators of the same precedence are evaluated.
Binary operators require two operands to produce a result.
A built-in data type is one that is provided as an integral part of the language.
A comment is a note about the code that the programmer includes so that he (or other
programmers) can keep track of what the various parts of the program do.
A control string is referred to as a control specifier.
A string that also includes a conversion control sequence, such as %d, is termed a
control string.
Conversion control sequences are also referred to as conversion specifications and
format specifiers.
A data type is defined as a set of values and a set of operations that can be applied to
these values.
Definition statements define or tell the compiler how much memory is needed for data
storage.
A double value is sometimes referred to as a double-precision number.
The main() function is sometimes referred to as a driver function, because it tells the
other functions the sequence in which they are to operate.
The backslash character, \, is also known as the escape character.
The combination of a backslash and one of several specific characters is called an
escape sequence.
All statements that cause some specific action to be performed by the computer when
the function is executed must end with a semicolon (;); such statements are known as
executable statements.
An expression is any combination of operators and operands that can be evaluated to
yield a value.
An expression containing only floating-point values (single and double precision) as
operands is called a floating-point expression (the term real expression is also used),
and the result of such an expression is a double-precision value.
A floating-point value, which is also called a real number, can be the number zero or
any positive or negative number that contains a decimal point.
A function header line, which is always the first line of a function, contains three
pieces of information: (1) what type of data, if any, is returned by the function, (2) the
name of the function, and (3) what type of data, if any, is sent into the function.
A header file is placed at the top, or head, of a C program using the #include
command.
The names of functions, as well as all of the words permitted in a program that have
special meaning to the compiler, such as radius and circumference, are
collectively referred to as identifiers.
Declaration statements can also be used to store an initial value into declared variables;
this value is referred to as an initial value.
13
14
3) banners
4) strings
3) literals
4) basic
15
11. A(n) ____ is any combination of operators and operands that can be evaluated to yield a value.
1) expression
3) operation
2) statement
4) argument
12. The grouping of 8 bits to form a larger unit is an almost universal computer standard and is referred
to as a ____.
1) byte
3) word
2) character
4) opcode
13. A(n) ____ is an acceptable value for a data type.
1) identifier
2) variable
3) escape sequence
4) literal
END OF EXAM
16
Lesson Contents
Overview
Objectives
Quick Quizzes
Additional Resources
Key Terms
Lesson Assignment
Chapter Notes
Overview
Chapter 3 provides an introduction to processing and interactive input in C. You learn
how assignment statements work and how to use mathematical library functions. You
learn to use the scanf() function for interactive input, and how to accomplish
complex formatted output. In this chapter, you also learn how to create and use
symbolic constants. The chapter case study has you practice creating a program with
interactive input. Finally, the common programming and compiler errors related to this
chapter are reviewed.
Objectives
Assignment
Mathematical library functions
Interactive input
Formatted output
17
Symbolic constants
Case study: Interactive input
Common programming and compiler errors
Topic Tips
Assignment
Topic Tip
Explore the meaning of the terms lvalue and rvalue, as described in the
Programming Note on page 107.
Besides math.h, ANSI C provides other standard library header files. For more
information, see http://en.wikipedia.org/wiki/C_standard_library.
Quick Quiz 1
1. In C, the ____________________ symbol is called the assignment operator.
2. The automatic conversion across an assignment operator is referred to as a(n)
____________________ type conversion.
3. What is a garbage value?
4. What is the prefix increment operator?
Interactive Input
Topic Tip
Note that there are two other solutions for the problem described above:
1) Replace the last scanf() call in Program 3.11 with the statement
scanf("\n%c",&skey);,
2) Place the statement fflush(stdin); after accepting a one-character input.
The fflush() function flushes the input buffer of any remaining characters.
Quick Quiz 2
18
1. A(n) ____________________ is a message that tells the person at the screen what
should be typed.
2. On most computer systems, characters read by the keyboard are stored in a temporary
holding area called a(n) ____________________ immediately after they are pressed.
3. What are robust programs?
4. What is user-input validation?
Formatted Output
Topic Tip
The #define preprocessor directive can also be used to create useful macros.
For more information, see: http://en.wikipedia.org/wiki/C_preprocessor.
Quick Quiz 3
1. The format of numbers displayed by printf() can be controlled by
____________________ included as part of each conversion control sequence.
2. What are magic numbers?
3. #define statements are also called ____________________ statements.
4. What does the term literal data mean?
Additional Resources
1. math.h:
www.cplusplus.com/ref/cmath/
2. scanf:
www.cplusplus.com/ref/cstdio/scanf.html
3. C Tutorial - Lesson 3: Constants:
http://cplus.about.com/od/beginnerctutoria1/l/aa031002a.htm
4. C Macros:
http://en.wikipedia.org/wiki/C_preprocessor
Key Terms
19
20
23
Chapter 4 - Selection
LESSON 9203C
At a Glance
Lesson Contents
Overview
Objectives
Quick Quizzes
Additional Resources
Key Terms
Chapter Notes
Overview
Chapter 4 introduces the selection structures available in C. You learn to create and
evaluate relational expressions and to use if and if-else statements. You also learn
to use the switch statement. In the case study you learn how to use selection
structures for data validation. Finally, you learn to identify and avoid common
programming and compiler errors.
Objectives
Relational expressions
The if and if-else statements
The if-else chain
The switch statement
Case study: Data validation
Common programming and compiler errors
24
Relational Expressions
Topic Tip
Read the section about De Morgans laws, as they are useful when writing
relational expressions. For more information, see the Historical Note on page
161.
Quick Quiz 1
1. What is flow of control?
Topic Tip
Topic Tip
Topic Tip
Quick Quiz 2
1. The simplest C selection statement is the ____________________ if statement.
2. A(n) ____________________ statement is one or more statements contained between
braces.
25
Do you have previous programming experience with Pascal? If so, note that the
equivalent to the switch statement is the case statement in Pascal.
Quick Quiz 3
1. What is a switch statement?
2. Internal to the switch statement, the keyword ____________________ identifies the
values that will be compared to the value of the switch expression.
3. What is the role of the default statement in a switch?
4. Once an entry point has been located by the switch statement, no further case
evaluations are done; this means that unless a(n) ____________________ statement is
encountered, all statements that follow, until the closing brace of the switch
statement, will be executed.
Additional Resources
5. C Tutorial - Lesson 5: Conditional Processing, Part 1: If/Else Statements and Relational
Operators:
http://cplus.about.com/od/beginnerctutoria1/l/aa040202a.htm
6. The Use of Braces in C/C++:
http://cplus.about.com/od/cprogrammingtip1/l/aa010102a.htm
7. C Ternary Operator:
http://cplus.about.com/od/cprogrammin1/l/bldef_ternaryop.htm
8. C Tutorial - Lesson 6: Conditional Processing, Part 2: Switch Statements and Logical
Operators:
http://cplus.about.com/od/beginnerctutoria1/l/aa040302a.htm
Key Terms
26
27
Chapter 5 - Repetition
At a Glance
Lesson Contents
Overview
Objectives
Quick Quizzes
Additional Resources
Key Terms
Lesson Assignment
Chapter Notes
Overview
Chapter 5 introduces the use of repetition statements in C. You learn about the basic
loop structures and how to use while and for statements. Computing sums and
averages using a while loop is studied in detail. Some case studies are explored that
introduce you to several loop programming techniques. You also learn about nested
loops and how to use the do-while statement. Finally, some common programming
and compiler errors are reviewed.
Objectives
The Programming Note in page 220 explains how to control a loop with a
symbolic constant.
Quick Quiz 1
1. What is a loop?
2. In a(n) ____________________ loop, which is also known as a fixed-count loop, the
condition is used to keep track of the number of repetitions that have occurred.
3. What is a program loop?
4. A(n) ____________________ loop is a condition-controlled loop where one specific
value is required to terminate the loop.
Note that sentinels are sometimes called flags or flag values. For more
information, see: http://en.wikipedia.org/wiki/Sentinel_value.
The Programming Note on page 242 discusses whether a programmer should use
a for loop or a while loop.
Topic Tip
Remember that there are different styles of writing braces in C programs. Stress
that which style is chosen is not that important, as long as the style is consistent
throughout a program (or group of programs). For more information, see the
Programming Note on page 244.
Quick Quiz 2
1. In computer programming, data values used to signal either the start or end of a data
series are called ____________________.
29
Quick Quiz 3
1. What is a nested loop?
2. The second loop of a nested loop is called the ____________________ loop.
3. A(n) ____________________ statement always creates a posttest loop.
4. What type of application is ideally suited for a posttest loop?
Additional Resources
1. C Tutorial - Lesson 7: Looping:
http://cplus.about.com/od/beginnerctutoria1/l/aa040402a.htm
2. While Loop:
http://en.wikipedia.org/wiki/While_loop
3. For Loop:
http://en.wikipedia.org/wiki/For_loop
4. Do While Loop:
http://en.wikipedia.org/wiki/Do_while_loop
5. How C Programming Works: Branching and Looping:
http://computer.howstuffworks.com/c8.htm
Key Terms
Lists in C, where commas are required to separate individual expressions in the list, are
referred to as comma-separated lists.
In a condition-controlled loop, the tested condition does not depend on a count being
achieved, but rather on a specific value being encountered.
30
31
3) !
4) %%
4.
A(n) ____ loop is a condition-controlled loop that terminates when a value within a
valid range is entered.
1) input-validation
3) condition-controlled
2) sentinel-controlled
4) counter-controlled
5.
It is a good practice to terminate the last case in a switch statement with a ____.
1) switch
3) default
break
2)
4) case
6.
The ____ statement literally loops back on itself to recheck the expression until it
evaluates to 0 (becomes false).
1) for
3) do-while
2) switch
4) while
7.
Omitting the ____ expression in a for statement results in an infinite loop.
1) initializing
3) tested
2) altering
4) break
32
8.
int age = 0;
if (age = 40)
printf("Happy Birthday!");
else
printf("Sorry");
1) Happy Birthday!
3) Runtime error.
2) Sorry
4) Nothing; the program will not compile.
9.
In Unix operating systems, the EOF mark is generated whenever the ____ keys are
pressed simultaneously.
1) Ctrl and D
3) Ctrl and F
2) Ctrl and E
4) Ctrl and Z
10.
In computer programming, data values used to signal either the start or end of a data
series are called ____.
1) input values
3) sentinels
2) limits
4) iterators
11.
In IBM-compatible computers, the EOF mark is generated whenever the ____ keys are
pressed simultaneously.
1) Ctrl and D
3) Ctrl and F
2) Ctrl and E
4) Ctrl and Z
12.
The use of ____ in a C program will result in a compiler error.
1) if (age == 40)
3) if (age = 40)
if
(40
==
age)
2)
4) if (40 = age)
13.
The logical AND operator is ____.
1) ||
3) !
2) &&
4) %%
14.
In a switch statement, the word ____ is optional and operates the same as the last
else in an if-else chain.
1) if
3) case
2) break
4) default
15.
A ____ statement is a specialized selection statement that can be used in place of an
if-else chain where exact equality to one or more integer constants is required.
1) case
3) switch
2) break
4) nested if
16.
Which of the following operators has right to left associativity?
1) !
3) &&
2) *
4) ||
33
17.
34
Lesson Contents
Overview
Objectives
Quick Quizzes
Additional Resources
Key Terms
Lesson Assignment
Chapter Notes
Overview
Chapter 6 introduces the role of C functions. You learn about functions and parameter
declarations, and also how to return a value. In the case study, you put to practice the
concepts learned to the particular case of calculating age norms. You also learn about
several useful standard library functions. Finally, you learn to identify and avoid
common programming and compiler errors.
Objectives
35
Topic Tip
Topic Tip
Quick Quiz 1
1. What is the difference between a called function and a calling function?
2. The items enclosed within the parentheses in a function call statement are called
____________________ of the function.
3. What is a pass by value?
4. The argument names in the header line of a function are known as
____________________.
Returning a Value
Topic Tip
A basic rule of testing states that each function should only be tested in a
program in which all other functions are known to be correct (see the
Programming Note on page 295).
Quick Quiz 2
1. A(n) ____________________ is the beginning of a final function that is used as a
placeholder for the final function until the function is completed.
2. Pass by value is also referred to as ____________________.
3. How can you return a value from a function?
4. How do you write the prototype of a function with an empty parameter list?
36
Quick Quiz 3
1. What are random numbers?
2. What are pseudorandom numbers?
3. The method for adjusting the random numbers produced by a random-number generator
to reside within a specified range is called ____________________.
4. The standard library consists of ____________________ header files.
Additional Resources
1. C Tutorial - Lesson 14: Functions:
http://cplus.about.com/od/beginnerctutoria1/l/aa051002a.htm
2. C Programming Tutorial: Lesson 4: Functions:
www.cprogramming.com/tutorial/c/lesson4.html
3. C Standard Library:
http://en.wikipedia.org/wiki/C_standard_library
4. C Programming Tips: Random Numbers: Using Pseudorandom Numbers:
http://cplus.about.com/od/cprogrammingtips/l/aa041403a.htm
Key Terms
Other terms used as synonyms for arguments are actual arguments and actual
parameters.
A fairly common procedure in child development is to establish normal ranges for
height and weight as they relate to a childs age; these normal ranges are frequently
referred to as age norms.
The items enclosed within the parentheses in a function call statement are called
arguments of the function.
A function that is called or summoned into action by its reference in another function is
a called function.
A function that calls another function is referred to as the calling function.
The purpose of a function body is to operate on the passed data and return, at most, one
value directly back to the calling function.
37
The portion of the function header that contains the function name and parameters is
known as a function declarator, which should not be confused with a function
declaration (prototype).
The purpose of a function header is to identify the data type of the value returned by
the function, if any, provide the function with a name, and specify the number, order,
and type of values expected by the function.
A function prototype declares the function to the compilerit tells the compiler the
name of the function, the data type of the value that the function will return (the
keyword void indicates that the function will not be returning any value), and the data
types of each argument that the function expects to receive when it is called.
The argument names in the header line of a function are known as parameters or
formal parameters and formal arguments.
When a function simply receives copies of the values of each of the arguments and must
determine where to store these values before it does anything else, this is known as a
pass by value (or a call by value).
Pseudorandom numbers are numbers which are not really random, but are sufficiently
random for the task at hand.
Random numbers are a series of numbers whose order cannot be predicted.
The method for adjusting the random numbers produced by a random-number generator
to reside within a specified range is called scaling.
A stub is the beginning of a final function that is used as a placeholder for the final
function until the function is completed.
38
39
7.
1)
2)
3)
4)
8.
____ reads the computers internal clock time, in seconds.
1) stime()
3) time()
2) time(SECONDS)
4) time(NULL)
9.
1)
2)
3)
4)
10.
A function that is called or summoned into action by its reference in another function is
a ____.
1) function prototype
3) calling function
2) called function
4) function declarator
11.
The function ____ returns a non-0 number if the argument is a letter or a digit;
otherwise it returns a 0.
1) int isalnum(int)
3) int isdigit(int)
2) int isalpha(int)
4) int isxdigit(int)
12.
To return a value, a function must use a(n) ____ statement.
1) exit
3) break
2) throw
4) return
13.
When a function simply receives copies of the values of the arguments and must
determine where to store these values before it does anything else, this is known as a ____.
1) pass by value
3) stub
2) pass by reference
4) function declarator
14.
1)
2)
3)
4)
15.
The method for adjusting the random numbers produced by a random-number generator
to reside within a specified range is called ____.
1) scaling
3) prototyping
2) stubbing
4) converting
16.
The ____ function can be used to generate a random number in C.
1) rand()
3) random()
2) srand()
4) rnd()
40
17.
All C compilers provide ____ function(s) for creating random numbers, defined in the
stdlib.h header file.
1) one
3) three
2) two
4) four
18.
A function that calls another function is referred to as the ____.
1) function prototype
3) calling function
2) called function
4) function declarator
19.
The purpose of a ____ is to identify the data type of the value returned by the function,
if any, provide the function with a name, and specify the number, order, and type of values
expected by the function.
1) function declarator
3) function body
2) prototype
4) function header
20.
1)
2)
3)
4)
21.
The items enclosed within the parentheses in a function call statement are called ____
of the function.
1) parameters
3) arguments
2) formal parameters
4) formal arguments
22.
The function ____ returns the absolute value of its double-precision argument.
1) double ceil(double)
3) double fabs(double)
2) double fmod(double)
4) double abs(double)
23.
The argument names in the header line of a function are known as ____.
1) arguments
3) actual arguments
2) parameters
4) actual parameters
24.
The minimum requirement of a ____ is that it compile and link with its calling module.
1) function body
3) stub function
2) function prototype
4) function declarator
25.
The function ____ returns the common logarithm of its argument.
double
exp(double)
1)
3) double log10(double)
2) double log(double)
4) double fmod(double)
END OF EXAM
41
Lesson Contents
Overview
Objectives
Quick Quizzes
Additional Resources
Key Terms
Lesson Assignment
Chapter Notes
Overview
In Chapter 7, you learn about variable scope and storage classes. The concept of pass by
value is refreshed and the concept of pass by reference is introduced. In the case study,
you create a function that uses pass by reference to swap the values of two variables. In
this chapter, you also learn about recursion and when to use recursion instead of
iteration. Finally, you learn to identify and avoid common programming and compiler
errors.
Objectives
Variable scope
Variable storage class
Pass by reference
Case study: Swapping values
Recursion
Common Programming and Compiler Errors
42
Variable Scope
Quick Quiz 1
1. What are local variables?
2. What is scope?
3. A variable with a(n) ____________________ scope is simply one that has had storage
locations set aside for it by a declaration statement made within a function body.
4. A variable with ____________________ scope is one whose storage has been created
for it by a declaration statement located outside any function.
You can find a short quiz about the auto storage class in:
www.cs.utah.edu/~hamlet/lib/lessons/c26/c26/node1.shtml.
Topic Tip
It is important to note that some compilers initialize local static variables the first
time the definition statement is executed rather than when the program is
compiled.
Topic Tip
You can find a short quiz about the static storage class in:
www.cs.utah.edu/~hamlet/lib/lessons/c26/c26/node2.shtml.
Topic Tip
The Programming Tip on page 341 summarizes the storage classes rules.
Pass by Reference
Topic Tip
Note that addresses have their own data type that more correctly are displayed in
hexadecimal notation using the %p conversion sequence.
Quick Quiz 2
1. Where and how long a variables storage locations are kept before they are released can
be determined by the ____________________ of the variable.
2. What are the registers in a computer?
43
Recursion
Topic Tip
In 1936, Alan Turing showed that although not every possible problem can be
solved by computer, those problems that have recursive solutions also have
computer solutions, at least in theory. For more information, see
http://en.wikipedia.org/wiki/Church-Turing_thesis and
http://en.wikipedia.org/wiki/Turing_machine.
Topic Tip
For an interesting example of another use of recursion, you may wish to research
the Sierpinski triangle (http://en.wikipedia.org/wiki/Sierpinski_triangle).
Quick Quiz 3
1. Functions that call themselves are referred to as self-referential or
____________________ functions.
2. When a function invokes itself, the process is called ____________________ recursion.
3. What is mutual recursion?
4. With respect to computer program execution, what is the stack?
Additional Resources
1. C Tutorial - Lesson 15: Scope and Program Structure:
http://cplus.about.com/od/beginnerctutoria1/l/aa060402d.htm
2. Pass by Value vs. Pass by Reference:
www.cs.princeton.edu/~lworthin/126/precepts/pass_val_ref.html
3. Recursion:
http://en.wikipedia.org/wiki/Recursion
4. Cprogramming.com Tutorial: Lesson 16: Recursion:
www.cprogramming.com/tutorial/lesson16.html
Key Terms
44
With respect to storage classes, the term auto is short for automatic.
When a function invokes itself, the process is called direct recursion.
A variable with global scope is one whose storage has been created for it by a
declaration statement located outside any function.
A variable with global scope is more commonly termed a global variable.
When using a pointer variable, the value that is obtained is always found by first going
to the pointer for an address; this is called indirect addressing.
To use a stored address, C provides us with an indirection operator, *.
A variable with a local scope is simply one that has had storage locations set aside for it
by a declaration statement made within a function body.
Variables created inside a function are available only to the function itself; they are said
to be local to the function, or local variables.
A function can invoke a second function, which in turn invokes the first function; this
type of recursion is referred to as indirect or mutual recursion.
Passing an address is referred to as a function pass by reference, because the called
function can reference, or access, the variable using the passed address.
In pass by value, a called function receives values from its calling function, stores the
passed values in its own local parameters, manipulates these parameters appropriately,
and directly returns, at most, a single value.
A variable that can store an address is known as a pointer variable.
Pointer variables are also pointers.
Registers are high-speed storage areas physically located in the computers processing
unit.
In run-time initialization, initialization occurs each time the declaration statement is
encountered.
Scope is defined as the section of the program where the variable is valid or known.
Functions that call themselves are referred to as self-referential or recursive functions.
C allocates new memory locations for all function arguments and local variables as each
function is called. This allocation is made dynamically, as a program is executed, in a
memory area referred to as the stack.
Where and how long a variables storage locations are kept before they are released can
be determined by the storage class of the variable.
45
2) global to main()
4) global to the program
9.
A declaration statement that specifically contains the word ____ is different from every
other declaration statement in that it does not cause the creation of a new variable by reserving
new storage for the variable.
1) auto
3) extern
2) static
4) register
10.
A variable that can store an address is known as a(n) ____ variable.
1) register
3) static
2) pointer
4) extern
11.
The ____ of a variable defines the location within a program where that variable can be
used.
1) storage class
3) scope
2) time dimension
4) data type
12.
Where and how long a variables storage locations are kept before they are released can
be determined by the ____ of the variable.
1) storage class
3) scope
2) time-dimension
4) data type
13.
____ can only be members of the auto, static, or register storage classes.
1) Constants
3) Local variables
2) int variables
4) Global variables
14.
The declaration statement ____ declares milesAddr to be a pointer variable that can
store the address of (that is, will point to) an integer variable.
1) int milesAddr&;
3) int *milesAddr;
2) int milesAddr*;
4) int &milesAddr;
15.
When a function invokes itself, the process is called ____ recursion.
1) direct
3) self-referential
2) mutual
4) indirect
16.
The purpose of the ____ storage class is to extend the scope of a global variable
declared in one source code file into another source code file.
1) auto
3) extern
2) static
4) register
17.
A ____ variable is one whose storage has been created for it by a declaration statement
located outside any function.
1) local
3) module
2) global
4) function
18.
____ variables have the same time duration as automatic variables.
1) Static
3) Extern
2) Register
4) Global
19.
The four available storage classes are called auto, static, extern, and ____.
1) stack
3) void
2) intern
4) register
47
20.
When the function returns control to its calling function, its ____ variables die.
1) local static
3) local extern
2) extern
4) local auto
21.
____ variables allow the programmer to jump around the normal safeguards provided
by functions.
1) Global
3) Static
2) Local
4) void
22.
Functions that call themselves are referred to as ____ functions.
1) nested
3) loop-back
2) recursive
4) rolling
23.
A function can invoke a second function, which in turn invokes the first function; this
type of recursion is referred to as ____ recursion.
1) direct
3) self-referential
2) mutual
4) tail
24.
A variable with a ____ scope is simply one that has had storage locations set aside for it
by a declaration statement made within a function body.
1) function
3) local
2) module
4) global
25.
____ is defined as the section of the program where the variable is valid or known.
1) Scope
3) Domain
2) Resolution
4) Reach
END OF EXAM
48
Chapter 8 - Arrays
LESSON 9206C
At a Glance
Lesson Contents
Overview
Objectives
Quick Quizzes
Additional Resources
Key Terms
Lesson Assignment
Chapter Notes
Overview
Chapter 8 provides an introduction to arrays. You first learn about one-dimensional
arrays: how to create them, use them, initialize them, and pass them as function
arguments. In a case study, you use a one-dimensional array and learn to compute
averages and standard deviations. You also learn about two-dimensional arrays. Several
programming examples are used to show you how to use these types of arrays. You are
also briefly introduced to larger dimensional arrays. Finally, common programming and
compiler errors are reviewed.
Objectives
One-dimensional arrays
Array initialization
Arrays as function arguments
Case study: Computing averages and standard deviations
Two-dimensional arrays
Common programming and compiler errors
49
One-Dimensional Arrays
Topic Tip
One-dimensional arrays are sometimes called vectors. For more information, see:
http://en.wikipedia.org/wiki/Array.
Topic Tip
In C, the starting index value for all arrays is always 0. This starting index value
is fixed by the compiler and cannot be altered. Although other high-level
languages, such as Visual Basic, allow the programmer to change this starting
value (even permitting negative values), C does not. In C, the first array element
is always 0, and negative index values are not permitted. For more information,
see the Programming Note on page 377.
Topic Tip
Topic Tip
To practice this topics concept, write a short program in which you try to access
a non-existent array element. What happens? Understand that answers will vary
depending on the compiler and operating system being used.
Quick Quiz 1
1. What is an atomic variable?
2. What is a data structure?
3. A(n) ____________________ array is a list of values of the same data type that is
stored using a single group name.
4. Each item in an array is called a(n) ____________________ or component of the array.
Array Initialization
Topic Tip
Be aware that if the number of initializers is less than the declared number of
elements listed in square brackets, the initializers are applied starting with array
element 0. After providing this information, ask students to think of an easy way
to initialize all elements to zero. They should come up with something like int
myArray[100] = {0};. Note that this does not work for local auto arrays.
Quick Quiz 2
50
Two-Dimensional Arrays
Quick Quiz 3
1. What is a two-dimensional array?
2. When initializing two-dimensional arrays, the ____________________ braces can be
omitted.
3. Initialization in a two-dimensional array is done in ____________________ order.
4. How can you view or interpret arrays of three, four, five, six or more dimensions?
Additional Resources
1. C Tutorial - Lesson 9: Arrays:
http://cplus.about.com/od/beginnerctutoria1/l/aa040802a.htm
2. C++ Tutorial - Lesson 10: Arrays and Vectors:
http://cplus.about.com/od/beginnerctutorial/l/aa050102a.htm
3. Array:
http://en.wikipedia.org/wiki/Array
4. How C Programming Works: Arrays:
http://computer.howstuffworks.com/c10.htm
51
Key Terms
One of the simplest data structures, called an array, is used to store and process a set of
values, all of the same data type that forms a logical group.
An atomic variable, which is also referred to as a scalar variable, is a variable whose
value cannot be further subdivided or separated into a built-in data type.
C does not check the value of the index being used (called a bounds check).
In bubble sort, successive values in the list are compared, beginning with the first two
elements.
A data structure, which is also known as an aggregate data type, is a data type with
two main characteristics. First, its values can be decomposed into individual data
elements, each of which is either atomic or another data structure. Secondly, it provides
an access scheme for locating individual data elements within the data structure.
Each item in an array is called an element or component of the array.
External sorts are used for much larger data sets that are stored in large external disk or
tape files, and cannot be accommodated within the computers memory as a complete
unit.
Each individual element is referred to as an indexed variable or a subscripted
variable because both a variable name and an index or subscript value must be used to
reference the element.
Internal sorts are used when the data list is not too large and the complete list can be
stored within the computers memory, usually in an array.
The two most common methods of performing such searches are the linear and binary
search algorithms.
In 1958, John McCarthy developed a language at the MIT specifically for manipulating
lists; this language was named LISP, the acronym for List Processing.
The NULL character (\0) is automatically appended to all strings by the C compiler.
A one-dimensional array, which is also known as both a single-dimensional array
and a single-subscript array, is a list of values of the same data type that is stored
using a single group name.
The Quicksort algorithm, which is also called a partition sort, divides a list into
two smaller sublists and sorts each sublist by portioning into smaller sublists, and so on.
The third subscript in a three-dimensional array is often called the rank.
In a selection sort, the smallest value is initially selected from the complete list of data
and exchanged with the first element in the list.
In a linear search, which is also known as a sequential search, each item in the list is
examined in the order it occurs until the desired item is found or the end of the list is
reached.
52
2) static
4) extern
10.
char codes[] = "sample"; sets aside ____ elements in the codes array.
1) 5
3) 7
2) 6
4) 8
11.
A(n) ____, is used to store and process a set of values, all of the same data type, that
forms a logical group.
1) data structure
3) array
4) atomic variable
2) scalar variable
12.
A two-dimensional array is sometimes referred to as a ____.
1) list
3) queue
4) table
2) vector
13.
Any individual element in an array can be accessed by giving the name of the array and
the elements position; this position is called the elements ____ value.
1) component
3) index
4) element
2) variable
14.
A(n) ____ variable, is a variable whose value cannot be further subdivided or separated
into a built-in data type.
1) data structure
3) array
4) class
2) scalar
15.
A ____ is a list of values of the same data type that is stored using a single group name.
1) one-dimensional array
3) three-dimensional array
4) matrix
2) two-dimensional array
16.
The term ____ uniquely identifies the element in row 1, column 3.
1) val[3][1]
3) val[3,1]
4) val[1,3]
2) val[1][3]
17.
1)
2)
3)
4)
18.
In a function prototype that has a two-dimensional argument, the ____ size is optional.
1) column
3) array
4) subscript
2) row
19.
A ____ loop is very convenient for cycling through array elements.
1) while
3) switch
4) for
2) do-while
20.
1)
2)
3)
4)
21.
1)
2)
3)
4)
For one-dimensional arrays, the offset to the element with index i is calculated as ____.
Offset = i * the size of the array
Offset = i * the size of the subscript
Offset = i * the size of a component + 1
Offset = i * the size of an individual element
22.
A ____-dimensional array can be viewed as a book of data tables.
1) one
3) three
4) four
2) two
23.
Any expression that evaluates a(n) ____ may be used as a subscript.
1) character
3) boolean
4) integer
2) double
24.
1)
2)
3)
4)
25.
Each item in an array is called a(n) ____ of the array.
1) subscript
3) index
2) variable
4) element
END OF EXAM
55
Lesson Contents
Overview
Objectives
Quick Quizzes
Additional Resources
Key Terms
Chapter Notes
Overview
In Chapter 9, you learn how to create, use and manipulate strings in C. You learn how
to use several useful library functions for string and character manipulation. You also
learn how to use strings for input data validation. An optional section covers how to
format strings using printf(), scanf(), sprintf() and sscanf(). The case
study shows how to process strings character-by-character to be able to count characters
and words in a string. Finally, several common programming and compiler errors are
reviewed.
Objectives
String fundamentals
Library functions
Input data validation
Formatting strings (optional)
Case study: Character and word counting
Common programming and compiler errors
56
String Fundamentals
Topic Tip
Some languages have a special String or string data type. You can find
more information about strings in several languages in:
http://en.wikipedia.org/wiki/String_%28computer_science%29.
Topic Tip
Refer to the Programming Note on page 449 to help explain the difference
between '\n' and "\n".
Topic Tip
You may be wondering why the char data type uses integer values. The
Programming Note on page 451 provides a good explanation on this issue.
Quick Quiz 1
1. What is a string literal?
2. What other terms are used to refer to a string literal?
3. The NULL character is ____________________.
4. The newline character is ____________________.
Library Functions
Topic Tip
You learned how to initialize strings in Chapter 8, but it is possible you may
have forgotten by now. The Programming Note on page 456 summarizes the
issues involved in initializing strings.
Quick Quiz 2
1. How does strcpy(str1, str2) work?
2. How does int toupper(char) work?
3. isalpha() is included in the ____________________ header file.
4. atoi() is included in the ____________________ header file.
57
Formatting Strings
Topic Tip
Quick Quiz 3
1. What is the difference between the angle brackets and the double quotes in a
#include statement?
2. The statement printf("|%25s|","Have a Happy Day"); displays the
message Have a Happy Day, ____________________-justified, in a field of 25
characters.
3. What is the result of using the statement printf("|%-25.12s|","Have a
Happy Day");?
4. True/False: When you use any of the four functions, printf(), scanf(),
sprintf(), or sscanf(), the control string containing the conversion control
sequences need not be explicitly contained within the function.
Additional Resources
1. String (computer science):
http://en.wikipedia.org/wiki/String_%28computer_science%29
2. C Tutorial - Lesson 10: Strings:
http://cplus.about.com/od/beginnerctutoria1/l/aa041302a.htm
3. C Programming Tips: Character Strings: Avoiding Common Mistakes Using Character
Strings in C:
http://cplus.about.com/od/cprogrammingtips/l/aa070203a.htm
4. How C Programming Works: Strings:
http://computer.howstuffworks.com/c35.htm
Key Terms
A string literal is also referred to as a string constant and string value, and more
conventionally as a string.
58
59
At a Glance
Lesson Contents
Overview
Objectives
Quick Quizzes
Additional Resources
Key Terms
Lesson Assignment
Chapter Notes
Overview
Chapter 10 provides an overview of the use of data files in C. You learn to declare,
open and close file streams, as well as read and write to text files. You also learn how to
work with random access files and how to pass and return filenames from and to
functions. In the case study, you develop a program that creates and uses a table of
constants. You also learn to write and read binary files. Finally, several common
programming and compiler errors are reviewed.
Chapter Objectives
When opening a file, the filename must not only exist, but you should also have
permission to read and/or write to the file.
Topic Tip
The Programming Note on pages 488 and 489 describe in detail the role of each
of the mode indicators available when opening a file. It is important to be aware
that you can open a file for both input and output at the same time.
Topic Tip
Note the importance of checking fopen()s return value (for more information
see the Programming Note on page 492).
Topic Tip
Remember that when using strings for filenames, you should make sure that the
string is long enough to include the end-of-string marker.
Quick Quiz 1
1. What is a file?
2. What is a file stream?
3. Each file stream name, when it is declared, is preceded by a(n)
____________________.
4. If a file opened for reading does not exist, the fopen() function returns the
____________________ address value.
The Programming Note on page 499 describes the issues involved in using full
path names when opening a file.
Topic Tip
Practice by writing a short program that writes some output to stderr. Where
is the output displayed?
61
Topic Tip
It may be a good idea if you try to write a short program in which you test
reading and writing to a file using random file access. It is important that you get
a good understanding of how these functions work.
Quick Quiz 2
1. The ____________________ function resets the current position to the start of the file.
2. What is the role of the fseek() function?
3. The function prototype for ftell() is contained in ____________________.
4. What happens if a file is opened for output and the file already exists?
Topic Tip
You may wish to explore how to use fwrite() to write all the elements of an
array to a file at once. For an example, see
www.cprogramming.com/tutorial/cfileio.html.
Quick Quiz 3
1. What are binary files?
2. What is a disadvantage of using binary files (instead of text files)?
3. The specification for explicitly creating and writing to a binary file is made by
appending a(n) ____________________ to the mode indicator when the file is opened.
4. When using fwrite() to write to a binary file, the first method argument is always
the ____________________ operator and a variable name.
Additional Resources
1. C Tutorial - Lesson 13: File I/0 and Command Line Arguments:
http://cplus.about.com/od/beginnerctutoria1/l/aa042902a.htm
2. How C Programming Works: Text Files:
http://computer.howstuffworks.com/c17.htm
3. How C Programming Works: Binary Files:
http://computer.howstuffworks.com/c39.htm
62
Key Terms
Binary files use the same code as your computer processor uses internally for Cs
primitive data types.
Text files are also known as character-based files.
Data that is stored together under a common name on a storage medium other than the
computers main memory is called a data file.
Each file has a unique filename referred to as the files external name.
A file is a collection of data that is stored together under a common name, usually on a
disk, magnetic tape, or CD-ROM.
A holiday table consists of legal holiday dates that have been previously stored in a
file.
A file stream that receives (that is, reads) data from a file into a program is referred to
as an input file stream.
A file stream that sends (that is, writes) data to a file is referred to as an output file
stream.
Text files store each individual character, such as a letter, digit, dollar sign, decimal
point and so on, using an individual character code (typically ASCII).
63
2.
1
2
3
4
5
6
7
8
9
10
11
1)
2)
3.
1)
2)
3)
4)
Line ____ in the following section of code checks for the end-of-string character.
void strcopy (char string1[], char string2[])
{
int i = 0;
while (string2[i] != '\0')
{
string1[i] = string2[i];
i++;
}
string1[i] = '\0';
}
3
5
3) 7
4) 10
____ causes the same display as the statement printf("Hello World!");.
fprintf(stdout,"Hello World!");
fprintf(stdin,"Hello World!");
fprintf(stderr,"Hello World!");
fprintf(NULL,"Hello World!");
4.
To write to a binary file you use the ____ function.
1) fput()
3) fwrite()
2) fputb()
4) write()
5.
The actual declaration of the FILE structure is contained in the ____ standard header
file.
1) stdio.h
3) file.h
2) stdlib.h
4) stream.h
64
6.
A ____ is a one-way transmission path that is used to connect a file stored on a physical
device, such as a disk or CD-ROM, to a program.
1) data file
3) binary file
2) text file
4) file stream
7.
The statement ____ displays the message Have a Happy Day, right-justified, in a
field of 25 characters.
1) printf("%s25","Have a Happy Day");
2) printf("%s-25","Have a Happy Day");
3) printf("%25s","Have a Happy Day");
4) printf("%-25s","Have a Happy Day");
8.
The array char message[81]; can be used to store a string of up to ____
characters.
1) 79
3) 81
4) 82
2) 80
9.
The string "Good Morning!" is stored in memory using a character array of size
____.
1) 13
3) 15
4) 16
2) 14
10.
Programs that use the gets() routine must include the ____ header file.
1) stdio.h
3) string.h
4) ctype.h
2) stdlib.h
11.
Typically, the ____ function is used to assemble a string from smaller pieces until a
complete line of characters is ready to be written, either to the standard output device or to a
file.
1) strcpy()
3) sscanf()
4) sprintf()
2) strcat()
12.
Programs that use the atoi() routine must include the ____ header file.
1) stdio.h
3) string.h
4) ctype.h
2) stdlib.h
13.
The value assigned to the NULL constant is ____.
1) '\n'
3) '\NULL'
4) "NULL"
2) '\0'
14.
Data that is stored together under a common name on a storage medium other than the
computers main memory is called a ____.
1) database
3) text file
4) binary file
2) data file
15.
Notice that each file stream name, when it is declared, is preceded by a(n) ____.
1) pipe
3) ampersand
4) asterisk
2) underscore
16.
____ reads values for the listed arguments from the file, according to the format.
1) fgetc()
3) fprintf()
2) fgets()
4) fscanf()
65
17.
When using #include, the characters ____, tell the compiler to start looking in the
default directory where the program file is located.
1) ""
3) //
4) \\
2) <>
18.
____ files store each individual character, such as a letter, digit, dollar sign, decimal
point, and so on, using an individual character code.
1) Data
3) Binary
4) ASCII
2) Text
19.
A file stream is closed using the ____ function.
exit()
1)
3) fclose()
4) close()
2) osclose()
20.
fputc() is the general form of ____.
1) fput()
3) putchar()
2) putc()
4) fputchar()
END OF EXAM
66
Lesson Contents
Overview
Objectives
Quick Quizzes
Additional Resources
Key Terms
Lesson Assignment
Chapter Notes
Overview
Chapter 11 thoroughly covers how pointers work in C. You learn the relationship
between arrays and pointers. You also learn how to manipulate pointers and how to pass
and use array addresses. You practice processing strings using pointers and learn how to
create strings using pointers. Finally, several common programming and compiler errors
are reviewed.
Objectives
67
Quick Quiz 1
1. Pointers, both as variables and function parameters, are used to store
____________________.
2. With respect to pointers, what is an offset?
3. When an array is created, the compiler automatically creates an internal pointer
____________________.
4. Can a pointer access be replaced using subscript notation? If so, under which
circumstances?
Manipulating Pointers
Topic Tip
Quick Quiz 2
1. What is the purpose of adding or subtracting numbers from pointers?
2. When adding or subtracting numbers to pointers, the computer automatically adjusts the
number to ensure that the result still points to a value of the original
____________________.
3. How are pointer operations scaled automatically?
4. When initializing pointers you must be careful to set a(n) ____________________ in
the pointer.
68
Topic Tip
Quick Quiz 3
1. If nums is a two-dimensional integer array, *(*(nums + 1) + 2) refers to
element ____________________.
2. What is the main difference between the following declarations?
char message1[81] = "this is a string";
char *message2 = "this is a string";
3. The header line ____________________ declares calc to be a pointer to a function
that returns an integer.
4. What does the declaration char *seasons[4]; create?
Additional Resources
1. FAQ: Arrays and Pointers:
http://c-faq.com/~scs/cgi-bin/faqcat.cgi?sec=aryptr
2. Tutorial: Pointers in C and C++:
http://augustcouncil.com/~tgibson/tutorial/ptr.html
3. The Function Pointer Tutorials:
www.newty.de/fpt/fpt.html
4. C Tutorial - Lesson 8: An Introduction To Pointers:
http://cplus.about.com/od/beginnerctutoria1/l/aa040702a.htm
5. How C Programming Works: Pointers:
http://computer.howstuffworks.com/c20.htm
69
70
Key Terms
Anagram is a rearrangement of the letters in a word or phrase that takes another word
or phrase.
One unique feature of pointers is that offsets may be included in expressions using
pointers.
A word, phrase, or sentence that reads the same forward and backward, such as top spot
is a palindrome.
When an array is created, the compiler automatically creates an internal pointer
constant for it and stores the base address of the array in this pointer.
71
72
9.
1
2
3
4
5
6
7
1)
2)
3)
4)
You can replace lines 5 and 6 in the following function with ____.
/* copy string2 to string1 */
void strcopy(char string1[], char string2[])
{
int i = 0;
while (string1[i] = string2[i])
i++;
}
while (*string1 = *string2) ;
while (*string1 = string2) ;
while (*string1++ = *string2++) ;
while (*++string1 = *++string2) ;
10.
When an array is created, the compiler automatically creates an internal ____ for it and
stores the base address of the array in it.
1) pointer constant
3) symbolic constant
4) location
2) pointer
11.
int nums[100];
int *nPtr;
The statement ____ produces the same result as nPtr = nums;.
1) nPtr = &nums[0];
3) nPtr = *nums[0];
4) nPtr = &nums;
2) nPtr = nums[0];
12.
&grade[3] is equivalent to ____; assume that grade is an array of integers, and
each integer requires 4 bytes of storage..
1) &grade[0] + 3
3) &grade[0] + (3 * 4)
4) &grade[0] + (3 / 4)
2) &grade[0] + 4
13.
The address operator in C is ____.
1) &
3) ->
4) .
2) *
14.
If nums is a two-dimensional integer array, ____ refers to element nums[0][0].
1) *nums
3) *(&nums)
4) &(*nums)
2) *(*nums)
15.
____ uses the pointer and then increments it.
1) *ptNum-3) *ptNum++
4) *++ptNum
2) *--ptNum
16.
If numPtr is declared as a pointer variable, the expression ____ can also be written as
numPtr[i].
1) *numPtr + i
3) *numPtr
2) (numPtr + i)
4) *(numPtr + i)
73
17.
Consider the following declarations of a function that receives an array of integers and
finds the element with the maximum value:
(i) findMax(int *vals, int numEls)
(ii) findMax(int vals[], int numEls)
The address in vals may be modified ____.
1) only if the function is declared as in (i)
2) only if the function is declared as in (ii)
3) if either (i) or (ii) is used
4) in neither case because an array variable cannot be modified (it is a pointer
constant)
18.
Of the following expressions, ____ is the most commonly used. This is because such
an expression allows each element in an array to be accessed as the address is marched along
from the starting address of the array to the address of the last array element.
1) *ptNum-3) *ptNum++
4) *++ptNum
2) *--ptNum
19.
If nums is a two-dimensional integer array, ____ refers to element nums[1][0].
1) *nums[1]
3) *nums + 1
4) *nums++
2) *nums[0]
20.
1)
2)
3)
4)
21.
Pointers ____ be initialized when they are declared.
1) must
3) can
4) cannot
2) must not
22.
If gPtr is a pointer that points to the first element of an integer array (and each integer
requires four bytes of storage), ____ references the variable that is three integers beyond the
variable pointed to by gPtr.
1) *gPtr + 3
3) *(gPtr + 3 * 4)
4) *(gPtr + 3 / 4)
2) *(gPtr + 3)
23.
If we store the address of grade[0] in a pointer named gPtr (using the assignment
statement gPtr = &grade[0];), then, the expression ____ references grade[0].
1) gPtr(0)
3) &gPtr
4) *gPtr
2) gPtr
24.
Adding ____ to a pointer causes the pointer to point to the next element of the original
data type being pointed to.
1) 1
2) 1 * sizeof(data type being pointed to)
3) 2
4) 2 * sizeof(data type being pointed to)
74
25.
In performing ____ on pointers, we must be careful to produce addresses that point to
something meaningful.
1) comparisons
3) subscript operations
2) arithmetic
4) duplication
END OF EXAM
75
Chapter 12 - Structures
LESSON 9209C
At a Glance
Lesson Contents
Overview
Objectives
Quick Quizzes
Additional Resources
Key Terms
Chapter Notes
Overview
Chapter 12 covers the use of structures and unions in C. You learn to create and use
structures and arrays of structures. You also learn how to pass and return structures to
and from functions. You learn how to create and use unions in C. Finally, several
common programming and compiler errors are reviewed.
Objectives
Single structures
Arrays of structures
Passing and returning structures
Unions (optional)
Common programming and compiler errors
Single Structures
76
Topic Tip
The Programming Note on page 581 introduces the terms homogeneous and
heterogeneous data structure.
Quick Quiz 1
1. What is the difference between a structures form and the structures contents?
2. Assigning actual data values to the data items of a structure is called
____________________ the structure.
3. What is the difference between a homogeneous and a heterogeneous data structure?
4. For non-ANSI C compilers, the keyword ____________________ must be placed
before the keyword struct for initialization within a local declaration statement.
Arrays of Structures
Topic Tip
Quick Quiz 2
1. A ____________________ statement provides a simple method for creating a new and
typically shorter name for an existing structure type.
2. What are parallel arrays?
3. What is the problem with parallel arrays?
4. When initializing an array of structures, the ____________________ braces are not
necessary.
Unions
Topic Tip
Usually we may have a hard time thinking of uses for unions. You can find a
detailed example of a situation that benefits from using a union at
www.cs.utk.edu/~plank/plank/classes/cs140/Spring-1999/notes/Unions/.
Quick Quiz 3
77
1. Individual structure members may be passed to a function in the same manner as any
____________________ variable.
2. An alternative to passing a copy of a structure is to pass the ____________________ of
the structure.
3. What is a union?
4. How much memory space does a union reserve?
Additional Resources
1. C Tutorial Lesson 11: Structures:
http://cplus.about.com/od/beginnerctutorial1/l/aa041602a.htm
2. How C Programming Works: Pointers to Structures:
http://computer.howstuffworks.com/c31.htm
3. C FAQ: Structures, Unions, and Enumerations:
http://c-faq.com/struct/
4. C Tutorial: Unions:
www.sysprog.net/cunions.html
Key Terms
The structures contents consist of the actual data stored in the symbolic names.
Each of the individual data items in a structure (single unit) is an entity by itself that
is referred to as a data field.
A structures form consists of the symbolic names, data types, and arrangement of
individual data fields in the record.
A record is a heterogeneous data structure, which means that each of its components
can be of different data types.
An array is a homogeneous data structure, which means that each of its components
must be of the same type.
The data items of a structure are called members of the structure.
Parallel arrays are two or more arrays, where each array has the same number of
elements and the elements in each array are directly related by their position in the
arrays.
Assigning actual data values to the data items of a structure is called populating the
structure.
Taken together, all the data fields form a single unit that is referred to as a record.
In C, a record is referred to as a structure, and we use these terms interchangeably.
78
When defining structures, if the form of the structure is not followed by any variable
names, the list of structure members must be preceded by a user-selected structure
type name.
A union is a data type that reserves the same area in memory for two or more variables,
each of which can be a different data type.
79
At a Glance
Lesson Contents
Overview
Objectives
Quick Quizzes
Additional Resources
Key Terms
Lesson Assignmenbt
Chapoter Notes
Overview
Chapter 13 provides an introduction to linked lists and dynamic memory allocation in
C. You learn how to use and create linked lists, stacks and queues. You also learn how
to create dynamically linked lists. Finally, you learn about some common programming
and compiler errors, and how to avoid them.
Objectives
80
You may want to use an animation to help visualize how a linked list works. For
example, see www.cs.stir.ac.uk/~mew/dissertation/simulation.htm.
Topic Tip
Variants of linked lists include doubly-linked lists and circularly-linked lists. For
more information, see http://en.wikipedia.org/wiki/Linked_list.
Quick Quiz 1
1. What is a linked list?
2. What is a self-referencing structure?
3. All programming languages that support pointers provide a special pointer value,
known as both NULL and ____________________, which acts as a sentinel or flag to
indicate when the last structure has been processed.
4. The expression t1.nextaddr->name can, of course, be replaced by the equivalent
expression ____________________, which explicitly uses the indirection operator.
Make sure you read and understand why it is very important to check return
values when making malloc() and realloc() function calls (see the
Programming Note on page 616).
Stacks
Topic Tip
You may use an animation to helps visualize how a stack works. For example,
see www.cs.usask.ca/resources/tutorials/csconcepts/1998_5/stacks/java/ or
www.cs.hope.edu/~alganim/jvall/applet/stack.html.
Topic Tip
Quick Quiz 2
81
1. What functions are available in C for the dynamic allocation and release of memory
space?
2. How does malloc() work?
3. A(n) ____________________ is a special type of linked list in which objects can only
be added to and removed from the top of the list.
4. The operation of placing a new structure on the top of a stack is called a PUSH, and
removing a structure from a stack is called a(n) ____________________.
Queues
Topic Tip
Stacks and queues are two special forms of a more general data object called a
deque (pronounced deck). The term deque stands for double-ended queue.
For more information, see the Historical Note on page 620.
Topic Tip
You may use an animation to help visualize how a stack works. For example, see
www.cs.odu.edu/~zeil/cs361/Demos/replays/queuelist.html.
Quick Quiz 3
1. What is a queue?
2. What are the names of the operations used to add and remove items to/from a queue?
3. In a(n) ____________________, elements can be added and removed from anywhere
within the list.
4. The operation of adding a new structure to a dynamically linked list is called a(n)
____________________.
Additional Resources
1. Linked List:
http://en.wikipedia.org/wiki/Linked_list
2. Stack:
http://en.wikipedia.org/wiki/Stack_%28data_structure%29
3. Queue:
http://en.wikipedia.org/wiki/Queue
4. Deque:
82
http://en.wikipedia.org/wiki/deque
83
Key Terms
Dynamic memory allocation makes it unnecessary to reserve a fixed amount of
memory for a scalar, array or structure variable in advance.
Placing a new item on top of the queue is formally referred to as enqueueing.
The heap consists of unallocated memory that can be allocated to a program as
requested, while the program is executing.
The field on which a list is ordered is referred to as the key field, and insertions and
deletions are always made to preserve the ordering of this field.
A linked list is a set of structures in which each structure contains at least one member
whose value is the address of the next logically ordered structure in the list.
Items are removed from a queue in the order in which they were entered.
Dynamic memory allocation is also known as run-time allocation.
Structures that are linked together by including the address of the next structure in the
structure immediately preceding it are known as self-referencing structures.
The operation of removing an item from a queue is formally referred to as serving.
84
5.
Stacks and queues are two special forms of a more general data object called a(n) ____.
1) array
3) deque
4) heap
2) table
6.
____ memory allocation makes it unnecessary to reserve a fixed amount of memory for
a scalar, array, or structure variable in advance.
1) Dynamic
3) Partial
4) Advanced
2) Static
7.
The operation of removing a structure from a stack is called a ____.
1) PUSH
3) DELETE
4) REMOVE
2) POP
8.
A ____ is a special type of linked list in which objects can only be added to and
removed from the top of the list.
1) heap
3) queue
4) set
2) stack
85
9.
The function call ____ passes a copy of the complete emp structure to calcNet().
1) calcNet(struct emp);
3) calcNet(&emp);
4) calcNet(emp);
2) calcNet(*emp);
10.
The following function cycles through a linked list and displays its contents. Line 3 can
be replaced with ____.
1
2
3
4
5
6
7
8
1)
2)
11.
____ is equivalent to (*pointer).member.
1) *pointer.member
3) pointer->member
4) pointer@member
2) pointer>member
12.
Each member of a structure is accessed by giving both the structure name and
individual data item name, separated by a ____.
1) @
3) :
4) .
2) ->
13.
If you have declared a structure named Date, you can then make the name DATE a
synonym for the terms struct Date, by using the statement ____.
1) typedef struct Date DATE; 3) #define struct Date DATE
2) typedef DATE struct Date; 4) #define DATE struct Date
14.
The expression t1.nextaddr->name can be replaced by the equivalent expression
____.
1) (*t1.nextaddr).name
3) *(*t1.nextaddr).name
4) *((*t1.nextaddr).name)
2) (&t1.nextaddr).name
15.
____ reserves space for an array of n elements of the specified size.
1) malloc()
3) realloc()
calloc()
4) nalloc()
2)
16.
A(n) ____ is a set of structures in which each structure contains at least one member
whose value is the address of the next logically ordered structure in the list.
1) array
3) queue
4) linked list
2) stack
17.
The operation of removing a structure from a dynamically linked list is called a(n)
____.
1) POP
3) REMOVE
4) DELETE
2) SERVE
86
18.
In C, a record is referred to as a(n) ____.
1) data field
3) structure
4) tuple
2) union
19.
A union reserves sufficient memory locations to accommodate ____.
1) its smallest members data type
3) all of its members data types
4) none of its members data types
2) its largest members data type
20.
____ reserves the number of bytes requested by the argument passed to the function.
1) malloc()
3) realloc()
2) calloc()
4) balloc()
END OF EXAM
87
Lesson Contents
Overview
Objectives
Quick Quizzes
Additional Resources
Key Terms
Lesson Assignment
Chapter Notes
Overview
Chapter 14 introduces several additional capabilities of the C language: the typedef
declaration statement, the conditional preprocessor directives, the use of enumerated
constants, the ?: operator and the goto statement. You also learn about the bit
operators available in C, how to create and use macros and how to pass and use
command-line arguments. Finally, several common programming and compiler errors
are reviewed.
Objectives
Additional features
Bit operations
Macros
Command-line arguments
Common programming and compiler errors
88
Additional Features
Topic Tip
Topic Tip
Quick Quiz 1
1. Both the #ifndef and #ifdef directives permit ____________________ in that the
statements immediately following these directives, up to either the #else or #endif
directives, are compiled only if the condition is true, whereas the statements following
the #else are compiled only if the condition is false.
2. Are there any ternary operators in C? If so, give an example of a ternary operator.
3. The ____________________ statement creates a new name for an existing data type.
4. How can you create enumerated lists in C?
Bit Operations
Topic Tip
Topic Tip
You can use ~0 to learn what the largest possible integer in a computer system
is. See: www.cprogramming.com/tutorial/bitwise_operators.html.
Topic Tip
Bitwise operations are usually faster than other types of operations in a computer
(http://en.wikipedia.org/wiki/Bitwise_operation). For this reason, if efficiency
is crucial, you may consider using bit operations for division and multiplication
(when possible) instead of using / or *.
Quick Quiz 2
1. What are bit operators?
2. In an operation like op1 & op2, the 0s in op2 effectively mask, or eliminate, the
respective bits in op1, while the ones in op2 ____________________ the respective
bits in op1 through with no change in their values.
89
3. In a(n) ____________________ right shift (using the >> operator), each single shift to
the right corresponds to a division by 2.
4. How does the ^ operator work?
Macros
Topic Tip
Command-Line Arguments
Topic Tip
Note that if the full path name of the program is stored, pgm14.3 in Figure
14.11 should be replaced by its full path name.
Topic Tip
Note that if the full path name of the program is stored, the first character
displayed is the disk drive designation, which is usually C.
Quick Quiz 3
1. What is a macro?
2. What are command-line arguments?
3. The advantage of using a(n) ____________________ instead of a function is an
increase in execution speed.
4. Any argument typed on a command line is considered to be a(n)
____________________.
Additional Resources
1. C Preprocessor:
http://en.wikipedia.org/wiki/C_preprocessor
2. C Tutorial - Lesson 3: Constants:
http://cplus.about.com/od/beginnerctutoria1/l/aa031002b.htm
3. Bitwise Operators in C and C++:
www.cprogramming.com/tutorial/bitwise_operators.html
90
Key Terms
The typedef declaration statement permits constructing alternate names for an
existing C data type name. These alternate names are known as aliases.
In an arithmetic right shift (using the >> operator), each single shift to the right
corresponds to a division by 2.
The operators that are used to perform bit operations in C are known as bit operators.
Command-line arguments are arguments that are typed on the command line.
Both the #ifndef and #ifdef directives permit conditional compilation in that the
statements immediately following these directives, up to either the #else or #endif
directives, are compiled only if the condition is true, whereas the statements following
the #else are compiled only if the condition is false.
In an operation like op1 & op2, the 0s in op2 effectively mask, or eliminate, the
respective bits in op1, while the ones in op2 filter, or pass, the respective bits in op1
through with no change in their values.
For positive signed numbers, where the leftmost bit is 0, both arithmetic and logical
right shifts produce the same result.
When the equivalence created using a #define statement consists of more than a
single value, operator or variable, the symbolic name is referred to as a macro, and the
substitution of the text in place of the symbolic name is called a macro expansion or
macro substitution.
In an operation like op1 & op2, the variable op2 is called a mask.
AND operations are extremely useful in masking, or eliminating, selected bits from an
operand.
The conditional operator, ?:, is unique in C in that it is a ternary operator.
91
2.
3) &
4) []
3.
ARRAY first, second; is equivalent to the two definitions int first[100];
and int second[100]; if ____.
1) you are using a pre-ANSI C compiler
2) you are using a C/C++ compiler
3) the statement typedef int ARRAY[100]; is used before
4) the statement #define ARRAY int[100]; is used before
4.
For unsigned integers, each left shift (using the << operator) corresponds to ____.
1) multiplication by 2
3) multiplication by 4
4) division by 4
2) division by 2
5.
Enumerated lists are identified by the reserved word ____ followed by an optional,
user-selected name and a required list of one or more constants.
1) list
3) enumerate
4) enum
2) typedef
6.
A conditional expression uses the conditional operator, ____, and provides an alternate
way of expressing a simple if-else statement.
1) ->
3) ?
4) :
2) ?:
7.
The equivalence produced by a typedef statement can frequently be produced equally
well by a ____ statement.
1) enum
3) struct
#define
4) alias
2)
8.
____ bit operations are extremely useful in masking, or eliminating, selected bits from
an operand.
1) &
3) >>
|
4) <<
2)
92
9.
Explicit values can be assigned to each enumerated constant, with unspecified values
automatically continuing the integer sequence from the last specified value. For example, ____.
1) enum {Mon: 1, Tue, Wed, Thr, Fri, Sat, Sun};
2) enum {Mon, Tue, Wed, Thr, Fri, Sat, Sun};
Mon = 1;
3) enum {Mon = 1, Tue, Wed, Thr, Fri, Sat, Sun};
4) enum {Mon 1, Tue, Wed, Thr, Fri, Sat, Sun};
10.
11.
In an arithmetic right shift (using the >> operator), each right shift corresponds to ____.
1) multiplication by 2
3) multiplication by 4
4) division by 4
2) division by 2
12.
1 0 1 1 0 0 1 1 ____ 1 1 0 1 0 1 0 1 results in 1 0 0 1 0 0 0 1.
1) &
3) >>
4) <<
2) |
13.
1 0 1 1 0 0 1 1 ____ 1 1 0 1 0 1 0 1 results in 1 1 1 1 0 1 1 1.
1) &
3) >>
4) <<
2) |
16.
#define SQUARE(x) x * x
val = SQUARE(num1 + num2);
results in the equivalent statement ____.
1) val = num1 + (num2 * num1 + num2);
2) val = (num1 + num2 * num1) + num2;
3) val = (num1 + num2) * (num1 + num2);
4) val = num1 + num2 * num1 + num2;
17.
18.
3) ^
4) ~
19.
Upon encountering the command line pgm14.3 three blind mice, the
operating system stores it as a sequence of ____ strings.
1) one
3) four
4) five
2) three
20.
The ____ operator causes a bit-by-bit AND comparison between its two operands.
1) ~
3) &&
4) &
2) ^
21.
1 0 1 1 0 0 1 1 ____ 1 1 0 1 0 1 0 1 results in 0 1 1 0 0 1 1 0.
1) &
3) ^
4) ~
2) |
22.
3) aliases
4) macros
23.
Using even one ____ statement in a program is almost always a sign of bad
programming structure.
1) enum
3) goto
4) #define
2) typedef
24.
The statement ____ makes the name REAL a synonym for double.
1) typedef double REAL;
3) enum REAL double
4) typedef REAL double;
2) #define double REAL
25.
In the equivalence statement #define SQUARE(x) x * x, x is ____.
1) fixed
3) a variable
2) an error
4) an argument
END OF EXAM
94
Quiz 3
1. The technique used by professional software developers for understanding the problem that is
being solved and for creating an effective and appropriate software solution is called the
software development process.
2. selection
3. invocation
4. When writing a program, a repetition structure, which is also referred to as looping and
iteration, provides the ability for the same operation to be repeated based on the value of a
condition.
Chapter 2
Quiz 1
1. The names of functions, as well as all of the words permitted in a program that have
special meaning to the compiler, are collectively referred to as identifiers.
2. A function header line, which is always the first line of a function, contains three pieces
of information (1) what type of data, if any, is returned by the function, (2) the name of
the function, and (3) what type of data, if any, is sent into the function.
3. newline escape sequence
4. reserved word
keyword
reserved or keyword
Quiz 2
1. A data type is defined as a set of values and a set of operations that can be applied to
these values.
95
2. precision
3. An expression is any combination of operators and operands that can be evaluated to
yield a value.
4. Associativity
Quiz 3
1. Variables
2. An assignment statement tells the computer to assign a value to (that is, store a value in)
a variable.
3. Definition statements define or tell the compiler how much memory is needed for data
storage.
4. initialized
Chapter 3
Quiz 1
1. =
2. implicit
3. A previously stored number, if it has not been initialized to a specific and known value,
is frequently referred to as a garbage value.
4. Using the increment operator, ++, the expression variable = variable + 1 can be replaced
by the either the expression variable++ or ++variable. When the ++ operator appears
before a variable, it is called a prefix increment operator.
Quiz 2
1. prompt
2. buffer
3. Programs that detect and respond effectively to unexpected user input are formally
referred to as robust programs and informally as bullet-proof programs.
4. The basic approach to handling invalid data input is referred to as user-input validation,
which means validating the entered data either during or immediately after the data
have been entered, and then providing the user with a way of reentering any invalid
data.
Quiz 3
1. field width specifiers
2. Literal values that appear many times in the same program are referred to by
programmers as magic numbers.
3. equivalence
4. Literal data refers to any data within a program that explicitly identifies itself.
Chapter 4
Quiz 1
1. The term flow of control refers to the order in which a programs statements are
executed.
2. relational
96
3. conditions
4. If an expression has any non-0 value (true), !expression produces a 0 value
(false). If an expression is false to begin with (has a 0 value), !expression is
true and evaluates to 1.
Quiz 2
1. one-way
2. compound
3. Including one or more if-else statements within an if or if-else statement is
referred to as a nested if statement.
4. Indentation used within an if-else is always irrelevant as far as the compiler is
concerned. Whether the indentation exists or not, the compiler will, by default, associate
an else with the closest previous unpaired if, unless braces are used to alter this
default pairing.
Quiz 3
1. A switch statement is a specialized selection statement that can be used in place of an
if-else chain where exact equality to one or more integer constants is required.
2. case
3. Any number of case labels may be contained within a switch statement, in any
order. However, if the value of the expression does not match any of the case values,
no statement is executed unless the keyword default is encountered. The word
default is optional and operates the same as the last else in an if-else chain. If
the value of the expression does not match any of the case values, program execution
begins with the statement following the word default.
4. break
Chapter 5
Quiz 1
1. A section of code that is repeated is referred to as a loop, because after the last
statement in the code is executed, the program branches, or loops, back to the first
statement and starts another repetition through the code.
2. counter-controlled
3. The transfer of control back to the start of a while statement to reevaluate the
expression is known as a program loop.
4. sentinel-controlled
Quiz 2
1. sentinels
2. Ctrl and Z
3. A break statement, as its name implies, forces an immediate break, or exit, from
switch, while, for, and do-while statements only.
97
4. Although the initializing and altering lists can be omitted from a for statement,
omitting the tested expression results in an infinite loop.
Quiz 3
1. There are many situations in which it is very convenient to have a loop contained within
another loop. Such loops are called nested loops.
2. inner
3. do-while
4. There is one type of application that is ideally suited for a posttest loop, which is the
input data validation application.
Chapter 6
Quiz 1
1. A function that is called or summoned into action by its reference in another function is
a called function. A function that calls another function is referred to as the calling
function.
2. Arguments
actual arguments
actual parameters
3. When a function simply receives copies of the values of each of the arguments and
must determine where to store these values before it does anything else, this is known as
a pass by value (or a call by value).
4. Parameters
formal parameters
formal arguments
Quiz 2
1. stub
2. call by value
3. To return a value you use a return statement. For example, return
expression; or return (expression);
4. The prototype for a function with an empty parameter list requires either writing the
keyword void or nothing at all between the parentheses following the functions name.
Quiz 3
1. Random numbers are a series of numbers whose order cannot be predicted.
2. Pseudorandom numbers are numbers which are not really random, but are sufficiently
random for the task at hand.
3. caling
4. 15
Chapter 7
Quiz 1
1. Variables that are created inside a function and available only to the function are said to
be local to the function, or local variables.
98
2. Scope is defined as the section of the program where the variable is valid or known.
3. local
4. global
Quiz 2
1. storage class
2. Registers are high-speed storage areas physically located in the computers processing
unit.
3. Passing an address is referred to as a function pass by reference, because the called
function can reference, or access, the variable using the passed address.
4. pointer variable
pointer
pointer or pointer variable
Quiz 3
1. recursive
2. direct
3. A function can invoke a second function, which in turn invokes the first function; this
type of recursion is referred to as indirect or mutual recursion.
4. C allocates new memory locations for all function arguments and local variables as each
function is called. This allocation is made dynamically, as a program is executed, in a
memory area referred to as the stack.
Chapter 8
Quiz 1
1. An atomic variable, which is also referred to as a scalar variable, is a variable whose
value cannot be further subdivided or separated into a built-in data type.
2. A data structure, which is also known as an aggregate data type, is a data type with two
main characteristics. First, its values can be decomposed into individual data elements,
each of which is either atomic or another data structure. Second, it provides an access
scheme for locating individual data elements within the data structure.
3. one-dimensional
single-dimensional
single-subscript
4. element
Quiz 2
1. \0
2. zero
0
zero (0)
3. No. The values within auto local arrays are undefined.
4. True
Quiz 3
1. A two-dimensional array, or table, consists of both rows and columns of elements.
99
2. inner
3. row
4. Arrays of three, four, five, six, or more dimensions can be viewed as mathematical ntuples.
Chapter 9
Quiz 1
1.
2.
3.
4.
Quiz 2
1. It copies str2 to str1, including the '\0'.
2. It returns the uppercase equivalent if the character is lowercase; otherwise, it returns the
character unchanged.
3. ctype.h
4. stdlib.h
Quiz 3
1. The angle brackets, <>, tell the compiler to begin searching for the included file in the C
compiler system library directory, while the double quotes, "", tell the compiler to start
looking in the default directory where the program file is located.
2. right
3. The statement printf("|%-25.12s|","Have a Happy Day"); causes 12
characters to be left justified in a field of 25 characters.
4. True
Chapter 10
Quiz 1
1. A file is a collection of data that is stored together under a common name, usually on a
disk, magnetic tape, or CD-ROM.
2. A file stream is a one-way transmission path that is used to connect a file stored on a
physical device, such as a disk or CD-ROM, to a program.
3. Asterisk
*
asterisk (*)
4. NULL
Quiz 2
1.
2.
3.
4.
rewind()
The fseek() function allows the programmer to move to any position in the file.
stdio.h
When opening a file for output, if the name of an existing data file is used with
fopen(), the file will be destroyed when it is opened in write mode.
100
Quiz 3
1. Files that are referred to as binary files store numerical values using the computers
internal numerical code.
2. A disadvantage is that the file can no longer be inspected using either a word processing
or text editing program, which means that the ability to see the numerical values as
textual information is lost.
3. 'b'
4. address
&
Chapter 11
Quiz 1
1. addresses
2. One unique feature of pointers is that offsets may be included in expressions using
pointers. For example, the 1 in the expression *(gPtr + 1) is an offset. The offset is
the number of variables to skip over.
3. constant
4. A pointer access can always be replaced using subscript notation. For example, if
numPtr is declared as a pointer variable, the expression *(numPtr + i) can also
be written as numPtr[i]. This is true even though numPtr is not created as an array.
Quiz 2
1. A pointer, constructed either as a variable or function parameter, contains a value. With
pointers, however, the stored value is an address. Thus, by adding numbers to and
subtracting numbers from pointers, we can obtain different addresses.
2. data type
3. When numbers are added to pointers, a correct scaling is automatically accomplished
because the compiler converts the arithmetic operation pointer + number to
pointer + number * sizeof(data type being pointed to).
4. address
Quiz 3
1. nums[1][2]
2. The main difference in the definitions of message1 as an array and message2 as a
pointer is the way the pointer is created. Defining message1 using the declaration
static char message1[81] explicitly calls for a fixed amount of storage for the
array. This causes the compiler to create a pointer constant. Defining message2 using
the declaration char *message2 explicitly creates a pointer variable first. This
pointer is then used to hold the address of a string when the string is actually specified.
This difference in definitions has both storage and programming consequences (see
Figure 11.12).
3. int (*calc)()
101
4. The declaration char *seasons[4]; creates an array of four elements, where each
element is a pointer to a character.
Chapter 12
Quiz 1
1. A structures form consists of the symbolic names, data types, and arrangement of
individual data fields in the record. The structures contents consist of the actual data
stored in the symbolic names.
2. populating
3. The difference is best explained with an example. An array is a homogeneous data
structure, which means that each of its components must be of the same type. A record
is a heterogeneous data structure, which means that each of its components can be of
different data types.
4. static
Quiz 2
1. typedef
2. Parallel arrays are two or more arrays, where each array has the same number of
elements and the elements in each array are directly related by their position in the
arrays.
3. The problem with parallel arrays is that the correspondence between data items is easily
lost if only one of the arrays is reordered.
4. inner
Quiz 3
1. scalar
2. address
3. A union is a data type that reserves the same area in memory for two or more variables,
each of which can be a different data type.
4. A union reserves sufficient memory locations to accommodate its largest members data
type.
Chapter 13
Quiz 1
1. A linked list is a set of structures in which each structure contains at least one member
whose value is the address of the next logically ordered structure in the list.
2. In a linked list, rather than requiring each structure to be physically stored in the proper
order, each new structure is physically added either to the end of the existing list, or
wherever the computer has free space in its storage area. The structures are linked
together by including the address of the next structure in the structure immediately
preceding it. From a programming standpoint, the current structure being processed
contains the address of the next structure, no matter where the next structure is actually
stored. Such structures are also known as self-referencing structures.
3. NIL
4. (*t1.nextaddr).name
102
Quiz 2
1. C provides the four functions, malloc(), calloc(), realloc() and free(), to
control the dynamic allocation and release of memory space.
2. It reserves the number of bytes requested by the argument passed to the function. It
returns the address of the first reserved location as an address of a void data type, or
NULL if sufficient memory is not available.
3. stack
4. POP
Quiz 3
1. Items are removed from a queue in the order in which they were entered. Thus, a queue
is a first in, first out (FIFO) structure.
2. Placing a new item on top of the queue is formally referred to as enqueueing, and the
operation of removing an item from a queue is formally referred to as serving.
3. dynamically linked list
4. INSERT
Chapter 14
Quiz 1
1.
2.
3.
4.
conditional compilation
The conditional operator, ?:, is unique in C in that it is a ternary operator.
typedef
Enumerated lists are identified by the reserved word enum followed by an optional,
user-selected name and a required list of one or more constants.
Quiz 2
1. The operators that are used to perform bit operations in C are known as bit operators.
The bit operators in C are &, |, ^, ~, << and >>.
2. filter
pass
3. arithmetic
4. The exclusive OR operator, ^, performs a bit-by-bit comparison of its two operands.
The result of the comparison is determined by the following rule: The result of an
exclusive OR comparison is 1 if one and only one of the bits being compared is a 1;
otherwise the result is 0.
Quiz 3
1. When the equivalence created using a #define statement consists of more than a
single value, operator or variable, the symbolic name is referred to as a macro, and the
substitution of the text in place of the symbolic name is called a macro expansion or
macro substitution.
2. They are arguments that are typed on the command line.
3. macro
4. string
103
104