Assessments

You'll find the answers to all questions here.

Chapter 2 – Understanding Program Structure

  1. Preprocessor directives, function definition statement, function call statement, return statement, and block statement.
  2. Complex control statements, complex looping statements, and simple expression statements.
  3. Function identifier, function return type, function parameter list, function block or body, and return statement.
  4. Function prototype.
  5. The function definition has a function body; the function declaration, or prototype, does not.
  6. The return statement is not required when the function return type is void.

Chapter 3 – Working with Basic Data Types

  1. Integers (whole numbers), floating-point numbers (real numbers), characters, and Booleans.
  2. Char, signed char, unsigned char, short, short int, unsigned short int, int, long int, unsigned long int, float, double, long double, and _Bool.
  3. The size is determined by the type that indicates the range of values that a particular type can represent. Putting a really large value into a really small data type is identical to putting 10 pounds of pudding into a 2-pound sack; mayhem ensues.

Chapter 4 – Using Variables and Assignments

  1. Data type, identifier, and value.
  2. Constant variables, literal values, and defining symbol text.
  3. Variables are initialized to give them a known value before they might be accessed, thus preventing unpredictable results.
  4. Variables get their value via assignment with the = assignment operator.

Chapter 5 – Exploring Operators 
and Expressions

  1. No. Some operations do not make sense for all data types.
  2. Addition, subtraction, multiplication, and division.
  3. AND, OR, XOR, and NOT.
  4. You should use casting when you want to explicitly cast one data type into another without relying upon implicit rules of type conversion.
  5. In most cases, when expressions are simple, yes. However, when expressions become complex, we should explicitly determine the desired order of evaluation with the ( ) parentheses operator.

Chapter 6 – Exploring Conditional 
Program Flow

  1. No. The purpose of an if()… else… statement is to provide a branch so that either one or the other branch of the statement will only be executed.
  2. Not every instance of an if()… statement can be replaced with a switch()… statement because the conditional expression of an if()… statement can consist of multiple conditions resolving to a single Boolean value, whereas a switch()… statement generally tests for single values.
  3. Yes, but it is sometimes awkward and less expressive than using a switch()… statement.
  4. The best way to prevent the dangling else problem is to use brackets ({…}) for each branch of the if()… else… statement.

Chapter 7 – Exploring Loops and Iteration

  1. The four types of looping statements are for()…while()…, and do … while() loop statements and the goto label pair.
  2. In a counter-controlled loop, the number of iterations is known beforehand, so a counter is used. In a sentinel-controlled loop, the loop repeats until the sentinel value is true, signaling the end of looping.
  3. The minimum number of iterations for a while()… loop is zero. If the loop condition is false, the loop body never executes. The maximum number is an infinite loop when the loop condition is never false.
  4. The minimum number of iterations for a do … while() loop is one. The loop body will always execute once the loop condition is evaluated. The maximum number is an infinite loop when the loop condition is never false.
  5. The for()… loop is ideal for counter-controlled looping. The while()… and do … while() loops are ideal for sentinel-controlled looping. However, with a bit of "arm-twisting," any loop statementmay be used in any situation. Use the loop statement that best fits the situation.
  6. False. goto exists because the language creators felt there are some situations where goto is appropriate and makes the code clearer. However, use goto with caution.

Chapter 8 – Creating and Using Enumerations

  1. To group related constant values by their enumerated identifiers.
  2. No. If you do not explicitly set each value, the compiler assigns a value.
  3. No. Enumerated constants do not have to be unique within a group.
  4. Yes. Each case: condition must be unique within that switch()… statement.
  5. Enumerated constants are limited to integers or an expression that results in an integer value.

Chapter 9 – Creating and Using Structures

  1. struct someName s = {0};
  2. struct someName s1, s2;
  3. s2 = s1;
  4. Using struct someName s = { value1 , value2, … , value2 }; to initialize the components of a structure variable is the compact expression of this initialization, especially when there are many such structure variables to be initialized.
  5. The disadvantage is that such initialization is position-dependent. If the structure changes, the initialization may no longer be valid.
  6. Valid data types for the components of a structure are intrinsic C data types and enum types, as well as other structures. We will see later that a component may also be an array of any of these.

Chapter 10 – Creating Custom Data Types 
with typedef

  1. The typedef keyword allows us to create synonym types that add context to variables of that type.
  2. For enumeration and structure types, typedef provides a shorthand synonym. Instead of using enum List, we can use typedef to shorten it to just List. Instead of using struct Gizmo, we can use typedef to shorten it to just Gizmo. Then, we can declare variables of List and Gizmo synonym types.
  3. Essentially, there is no difference. The syntax for both is identical.
  4. The –Wall and –Werror switches instruct the compiler to treat any warning as an error and to cause all errors, no matter how trivial, to stop compilation.
  5. Anything that does not allocate memory (typedef, enum, struct, and function prototypes) goes into a header file. Anything that allocates memory or defines functions goes into the source file, not the header.

Chapter 11 – Working with Arrays

  1. An array definition has a data type, a base name, and a size (or a number of elements).
  2. A constant length array is one whose size can be determined at compile time – its size is a literal number, a #define symbol, a constant expression (such as 13-3), or an enumeration constant.
  3. A variable-length array is one whose size is determined at runtime by the value of a variable, the result of a variable expression, or the result of a function call.
  4. You can initialize a constant-length array when it is defined. You cannot initialize a VLA when it is defined.
  5. VLAs enable function parameters to receive arrays of any size so long as the size is also given as a parameter to the function.

Chapter 12 – Working with Multi-Dimensional Arrays

  1. A 1D array is also known as a vector or linear array.
  2. A 2D array is also known as a matrix.
  3. A 3D array can be considered a volume or 3D space.
  4. The last index in a 2D or 3D array represents the x axis or column number.
  5. Neither is preferable since, in some situations, CLAs are simpler to initialize, whereas in other situations, VLAs are more appropriate to initialize with a function.

Chapter 13 – Using Pointers

  1. A pointer variable has a value that is an address. That value can be 0 (NULL) up to the largest addressable byte in memory. 
  2. A pointer either has the type for the value it points to, or it has a void* type, which is unknown and must be provided to access the value it points to.
  3. The target of a pointer is either NULL (no target) or the location of an already-allocated memory location (such as an intrinsic variable, a structure variable, or an array variable).
  4. When any variable is passed via a function parameter, its value is copied to be used in the function body. For non-pointer variables, the values may be used directly. For pointer variables, the address is used to access the target value.
  5. De-referencing a pointer means using the address it contains to get or set a value at that address. A pointer variable only contains an address.
  6. A pointer can address any allocated piece of memory in a program's memory space. This can be a variable, a structure variable and its members, an array and its elements, a function, or a device. A pointer cannot address any intermediate values, say, from complex expressions, literal values, enumerated definitions and constants, or structure definitions that have not been allocated in memory.

Chapter 14 – Understanding Arrays and Pointers

  1. No. They are closely related but not identical.
  2. When an array index is incremented by one, the next array element is referenced.
  3. When a pointer to an array element is incremented by one, the address is incremented by the size of the data type of the array element.
  4. Yes. When an array is declared in this way, all elements are guaranteed to be contiguous.
  5. The elements of an array of pointers to other arrays may be contiguous but that would only be by chance. There is no guarantee that the subarrays will be contiguous to each other.
  6. No. When a pointer to an array is passed to a function, we can access the array elements using a pointer or array notation.
  7. No. The programmer must keep track of the size of any array.

Chapter 15 – Working with Strings

  1. Whitespace is a non-printable character that creates white space in the output. Space, tab, and vertical tab are examples of whitespace characters.
  2. A C string is (1) an array of characters (2) that's terminated by the NULL character.
  3. A C string array can be larger than the characters in the string; the end of the string is determined by the NULL character and any array elements after that can be ignored. A string array can never hold more characters (including the NUL character) than the size of the array.
  4. 'x' is a single character. "x" is a C string of one character with the NULL character.
  5. An empty string is a string with no characters in the array except for the NULL character. A NULL string is a declaration of a string array or a pointer to a string array that has never been allocated.
  6. The "Hello" literal string is only modifiable when it is used as an initializer to a constant-length or variable-length array. The characters are copied into the array's elements. Otherwise, a string literal cannot be modified.

Chapter 16 – Creating and Using More Complex Structures

  1. The compositions are as follows:
    1. Card is a simple structure of intrinsic and enumerated types.
    2. Hand is a structure that consists of an array of pointers.
    3. Deck is a structure that consists of an array of structures, an array of pointers, and intrinsic types.
  2. Stepwise refinement. Each step in the process takes a working program and adds features to it until the desired program is complete.
  3. Magic numbers are literal values that have special significance that represent real-world meaning. While magic numbers are a natural part of things we represent in our programs, we should strive to give added meaning to those numbers through the use of constant variables, enumerated constants, and preprocessor symbols.
  4. In one case, enumerated constants for Face and Suit that are related to each other use the e- prefix. In the other case, enumerated constants for array sizes are unrelated and use the k- prefix.

Chapter 17 – Understanding Memory Allocation and Lifetime

  1. In addition to an identifier, a data type, and a value, variables also have a storage class, a lifetime, and visibility (or scope).
  2. The four storage classes are auto, static, extern (global), and register.
  3. The lifetimes of static and external storage classes begin when the program loads (each is created) and end when the program ends (each is destroyed).
  4. A static variable is only visible within the block or compilation unit (file) where it is declared.
  5. An external (global) variable is visible to every function and block within the compilation unit (file) where it is declared.
  6. Automatic variables are destroyed/no longer accessible after execution leaves the block where they are declared.

Chapter 18 – Using Dynamic 
Memory Allocation

  1. malloc() allocates requested memory on the heap, as does calloc(). However, calloc() initializes the allocated memory to zero before returning.
  2. The lifetime of memory allocated on the heap begins when the memory is allocated and ends either when free() deallocates that memory or when the program ends.
  3. A memory leak occurs when a block of memory is no longer referenced via its pointer and the block has not been deallocated. It is unretrievable until the program exits.
  4. There are several important reasons to create test code:
    1. You prove the validity and correctness of your code.
    2. As the code changes, you can verify that the code still works as intended.
    3. Confidence in modifying code that has tests is higher.
    4. Tested code tends to require much less reworking and debugging.

Chapter 19 – Exploring Formatted Output

  1. printf() converts values (binary patterns) into characters for display on the console.
  2. No, values given to printf() are copied (call by value) and, therefore, cannot be changed by printf().
  3. When you specify an invalid format string, a compiler error should occur.
  4. No, printf() can only print the intrinsic types given in this chapter. Custom functions must be created to print the elements of a given structure.

Chapter 20 – Getting Input from the Command Line

  1. There are only two forms of main(): one that takes no parameters and one that takes an argument count and an argument vector.
  2. The argv parameter provides an array of strings entered on the command line to our program. They are strings only and may have to be converted to other data types in the argument processing loop.
  3. There is not one single standard command-line processor for C. Even getopt(), which is part of the C Standard Library, is system implementation-dependent. However, getopt(), as specified by the C standard, can be considered a minimum set of functionality and behavior; other implementations add additional capabilities.

Chapter 21 – Exploring Formatted Input

  1. A stream is a sequence of bytes transferred in one direction from its source to its target. C has three I/O streams: console input (or stdin), console output (or stdout), and console error output (or stderr). These three streams are declared in stdio.h.
  2. While there are many similarities between the two format specifiers, there are also some significant differences.
  3. First, use scanf() to convert input directly to the desired type with a format string; second, use fgets() to read input into a buffer and use sscanf() to convert data in the buffer to the desired type; or third, again read input into a buffer and use atoi() or atod() to convert data in that buffer to the desired type.
  4. Never use gets(), even if it is available.
  5. For older versions of C, use fgets() from stdin; for newer versions of C, use gets_s().

Chapter 22 – Working with Files

  1. A stream connects a device to the program for either reading or writing. A file is a stream that is persistent beyond the life of the program's execution.
  2. The three basic modes for file streams are reading, writing, and appending.
  3. A stream opened for reading moves data from its device into program memory.
  4. A stream opened for writing or appending moves data from program memory to its device.
  5. stdin, stdout, and stderr are console streams. A hard drive, a floppy drive, a CD, and a DVD are file-streaming devices. A microphone and speakers are audio-streaming devices. A network interface is both an input and output streaming device.

Chapter 23 – Using File Input and File Output

  1. Because of the variety of input and output, we needed to verify each possible execution path within our program.
  2. If a specified input file does not exist, it makes no sense to continue processing. The program must exit.
  3. When an output file is opened for writing, its current contents are cleared and a file is created with new data. When an output file is opened for appending, any new data is added to the end of the file; the pre-existing file data is preserved.
  4. Handling the final <newline> character when using fgets() depends upon the context in which fgets() is used. In the last chapter, we used a wrapper function around fgets(). In this chapter, we didn't need to do that, since the trimStr() function is always used after fgets(), which removes all leading and trailing whitespace.

Chapter 24 – Working with Multi-File Programs

  1. Yes, and this is often the best place to declare a structure.
  2. Yes, you can, but this is a really bad programming practice. You should define an array either in a function where it is used, in main(), or as a global variable (as we did with bRandomize).
  3. Yes, you can, but this is a really bad programming practice. Instead, declare function prototypes in the header and define the functions in the source file.
  4. You can define a function exactly once.
  5. To prevent name clashes, in the header file, use the #ifndef _HEADER_NAME_H_, #define _HEADER_NAME_H_, and #endif macros around the contents of the header file.
  6. Getting tricksy with the preprocessor not only takes an added amount of time to get right, but it also adds little to the overall reliability and maintainability of your programs. In other words, don't try to get tricksy with the preprocessor unless you have a good reason to and you absolutely must do so (there are no other good options).

Chapter 25 – Understanding Scope

  1. The three components of scope are visibility (what can access/call a thing), extent (lifetime of the thing), and linkage (visibility across multiple files).
  2. A compilation unit is a C source file and its included header files.
  3. If a source code file has the main() function and doesn't rely upon any other source files/compilation units, it may be a complete program. Not all compilation units are complete programs.
  4. The five types of visibility scope are block scope (the narrowest), function parameter scope (a part of block scope), file scope (visibility within a file), global scope (visibility across files, tied to linkage), and static scope (a means to limit scope).
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.151.158