Basics of C Programming Language Flashcards

1
Q

What are the 3 general steps in creating an executable?

A
  1. Preprocessing
  2. Compiling
  3. Linking
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is preprocessing?

A

Before code is compiled it is given to a program called the preprocessor. The preprocessor edits and manipulates code, setting it up for the compiler. Typically the preprocessor looks for lines that begin with # and alters the code based on these directives.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is compiling?

A

Preprocessed code is given to the compiler, a program that translates C statements into machine instructions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is linking?

A

A linker combines the object code produced by the compiler with any additional code needed to create a complete execuatable. This additional code includes things like library functions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the three fundamental language features that C relies on?

A
  1. Directives
  2. Fucntions
  3. Statements
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a function?

A

A series of statements grouped together and given a name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does main return?

A

An integer status code

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a statement?

A

A command to be executed when the program runs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Definition: String literal

A

A sequence of characters surrounded by “”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the valid ways to write comments in C?

A

/* this is the only valid comment */

some compilers allow // but it is not part of standard C

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Definition: variable

A

A data storage location

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a variable’s type?

A

A specification of what type of data it will hold

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Definition: expression

A

a combination of explicit values, constants, variables, operators, and functions that are interpreted according to the particular rules of precedence and of association for a particular programming language, which computes and then produces (returns, in a stateful environment) another value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

True/False: An expression of the same type can be substituted wherever a value is needed.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Definition: format string

A

A string literal which specifies a method for rendering an arbitrary number of varied data type parameter(s) into a string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Definition: constant

A

A value that doesn’t change during execution

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Definition: Identifier

A

A symbol that represents an entity such as variables, functions, and macros.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Definition: token

A

A group of characters that can’t be broken up without changing their meaning

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Definition: conversion specification

A

A placeholder, preceded by a %, representing a value to be filled in during printed. The value following the % specifies how the value is convereted from internal form to printed form

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What forms can a conversion specification have? What do the values mean?

A
%m.pX or %-m.pX
m: minimum field width
p: precision
X: conversion specifier
-: left align
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What does p specify when used with %m.pd?

What if p is bigger than the length of the value to be printed?

A

Minimum number of digits. Output is zero-filled

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What is the conversion specifier: d

A

decimal integer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What is the conversion specifier: e

A

floating point in exponential form

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What does p indicate in %m.pe? What if p == 0?

A

How many digits appear after the decimal. The decimal is not displayed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

What is the conversion specifier: f

A

Floating point in fixed-decimal format

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

What is the meaning of p in %m.pf? What if p == 0?

A

Number of digits after the decimal. Decimal is not shown

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

What is the conversion specifier: g

A

Floating point in either exponential or fixed decimal format depending on the number of significant digits.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

Whats the difference between %f and %g when displaying data in fixed decimal format?

A

%g won’t show trailing zeros

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

How does scanf work?

A

Starts at the left of the format string. For each conversion specification it tries to locate an item of the appropriate type, skipping blank spaces if necessary. It reads until it finds a character that cannot be part of the string which it puts back into the buffer.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

What happens if scanf cannot read an item successfully?

A

scanf returns immediately without looking at the rest of the format string or remaining buffer data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

What does scanf look for when reading an integer?

A

First: a digit or minus sign

Reads until a nondigit

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

What does scanf look for when reading a floating point?

A
  1. a plus or minus sign (optional)
  2. a sequence of digits possibly containing a decimal point
  3. an exponent (optional) consisting of the letter e, and optional sign, and one or more digits
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

What does scanf do when it encounters a whitespace character?

A

One whitespace character in the format string will match any number of whitespace characters in the input

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

What does scanf do if it encounters a non-white-space character in a format string?

A

It compares it with the next input character? If the two match, then the non-white-space character is discarded and scanf continues processing the input string. If they do not match, scanf puts the character back in the buffer and aborts.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

Definition: arithmetic operators

A

Operators that perform some mathematical functions (+, -, *, /, %)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

What is the precedence for arithmetic operators?

A

unary +, unary -, *, /, %, +, -

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

What is operator associativity?

A

When dealing with multiple operators, precedence is not enough to determine order of operations. Associativity comes into play when an expression contains multiple operators of the same level of precedence, and determines which operation will be evaluated first.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

What is a left associative operator?

A

An operator that groups from left to right. Ex: i - j - k == (i -j) - k

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q

Which arithmetic operators are left associative?

A

binary +, binary -, *, /, %

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
40
Q

What is right associativity?

A

An operator that groups from right to left.. ex: -+x == -(+x)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
41
Q

Which arithmetic operators are right associative?

A

unary +, unary -

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
42
Q

Definition: lvalue

A

Lvalues are values that have addresses being programmatically accessible to the running program. They are variables or dereferenced references

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
43
Q

Definition: compound assignment

A

An assignment that uses the old value as part of the computation for the new value. Ex += *= …

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
44
Q

What is a logical expression?

A

An expression that results in true or false (nonzero or 0)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
45
Q

What are relational operators?

A

A binary operator that tests a relationship between two entities and produces true or false:

, <=, >=

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
46
Q

What is an equality operator?

A

A binary operator that tests equality or lack of equality between two entities:

!=

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
47
Q

What associativity do relational and equality operators have in C?

A

left

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
48
Q

What are the C logical operators? What do they produce? How do they evaluate operands?

A

!, &&, ||
They produce a 1 (true) or a 0 (false)
They evaluate any non-zero as true and 0 as false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
49
Q

What is a logical short circuit? Which operators perform this?

A

In something of the form: expr1 && expr2,
if expr1 is 0, expr2 is not evaluated and 0 is returned.

In an expression of the form:
expr1 || expr2, if expr1 is non-zero, expr2 is not evaluated and 1 is returned

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
50
Q

What is the associativity of the logical operators?

A

! is right associative

&&, || or left associative

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
51
Q

What is a compound statement?

A

A group of statements enclosed by {}, indicating to the compiler that the group is to be treated as a single statement

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
52
Q

What is the rule for determining which if statement an else belongs to?

A

An else is paired with the nearest unpaired if statement

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
53
Q

What is a conditional expression? What is the conditional operator in C?

A

Conditional expression is a compound expression that chooses between two expressions based on the truth value of the first one. evalExpr is evaluated, if its value is non-zero, trueExpr is evaluated and its result is the result of the entire expression. Viceversa for falseExpr.

evalExpr ? trueExpr : falseExpr ;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
54
Q

What is a controlling expression

A

An expression that is evaluated at some point during each iteration and determines if the loop will continue (expr != 0) or if it will discontinue(expr == 0)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
55
Q

What is the comma operator?

A

expr1, expr2

expr1 is evaluated and its result is dicarded. Then expr2 is evaluated and its value and type is returned.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
56
Q

How does the break statement work?

A

When executed, it transfers control just past the end of innermost loop or switch

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
57
Q

How does the continue statement work?

A

When executed, it transfers control to just before the end of the loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
58
Q

What is a null statement? What is it good for?

A

An empty statement -just a ;

It’s useful for creating a loop body that does nothing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
59
Q

What are the two categories of integers?

A

Signed (default) and unsigned

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
60
Q

What are the six combinations of modifiers with int?

A
short int
unsigned short int
int
unsigned int
long int
unsigned long int
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
61
Q

What is a heuristic for choosing modifiers with int to improve portability?

A

For values between -32,768 and 32,767, use int. For all other values use long int

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
62
Q

What is an integer constant?

A

A number that appears in the text of the program.

ex:
10, xABC, 017

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
63
Q

How are decimal, octal, and hexadecimal constants differentiated?

A

Decimal - contain digits 0-9 and do not start with a 0

Octal - contain 0-7 and start with a 0

Hex - contain 0-9 and a-f and start with a 0x

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
64
Q

How do you force the compiler to treat a an integer constant as long? Unsigned?

A

Follow it with L. Follow it with U.

ex: 15UL == 15ul == 15LU == 15lu

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
65
Q

What is the conversion specifier for reading/writing an unsigned decimal? unsigned octal? unsigned hex?

What if we want the short or long versions?

A

decimal: %u
octal: %o
hex: %x
short: %h[u,o,x,d]
long: %l[u,o,x,d]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
66
Q

What is the default type for floating point constants?

A

double

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
67
Q

What are the conversion specifications for single precision floating point values? Double precision? Long double?

A

single: %f, %g, %e
double: %lf, %lg, %le
long double: %Lf, %Lg, %Le

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
68
Q

When should you use l in conjunction with a floating point conversion specification?

A

Only with scanf. printf %[e,f,g] can print single and double precision

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
69
Q

How does C treat chars?

A

8 bit integers. The C specification does not specify whether they are signed or unsigned.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
70
Q

What is the conversion specifier for characters?

A

%c

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
71
Q

What functions can you for characters instead of scanf and printf?

A

getchar() and putchar(char ch)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
72
Q

What is the sizeof operator?

A

Allows a program to determine how much memory is required by a particular type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
73
Q

What considerations are there when printing a sizeof value?

A

The return type is implementation defined. The trick is cast it to a known type before printing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
74
Q

What is the difference between the two types of conversions in C?

A

Implicit conversions are performed automatically by the compiler. Explicit conversions are mandated by the programmer.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
75
Q

When is implicit conversion performed? (4)

A
  1. the operands in an arithmetic or logical expression do not match
  2. the type of the right side of an assignment doesn’t match the type of the lvalue
  3. the type of an argument of a function doesn’t match the type of the corresponding parameter
  4. the expression in a return statement doesn’t match the function’s return type
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
76
Q

What is usual arithmetic conversion?

A

When binary operators have mixed types, their values are converted to the narrowest type that will accommodate both values. The narrowest type is the one that requires the least number of bytes to store.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
77
Q

What is integral promotion?

A

When an integer type is converted to a less narrow integer type. If a short int is added to a long int, the short int is promoted to long.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
78
Q

When does integral promotion occur?

A

Whenever the compiler says it should occur:
Logically: when operating with mixed integer types. ex: int + long.. int is promoted to long

Also:
short a = 1;
short b = 2;
a += 2;
Will throw a warning because C specification says += returns an int and we are trying to store an int in a short.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
79
Q

What are the two cases for usual arithmetic conversions?

A
  1. The type of either operand is a floating type

2. Neither operand has a floating type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
80
Q

What is the promotion chart for floating point types?

A
long double
^
double
^
float
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
81
Q

What is the promotion chart for integers?

A
unsigned long int
^
long int
^
unsigned int
^
int
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
82
Q

What happens when an unsigned integer is mixed with a signed operand?

A

Signed operand will be treated as unsigned. The sign bit of the signed operand will be treated a normal bit and produce unexpected results. ex:
signed int i = -10;
unsigned int j = 10;
printf(“%d\n”, (i < j));

will print 0 instead of 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
83
Q

What conversion happens in an assignment statement?

A

If the rvalue type is different from the lvalue type, the rvalue type is converted to the value type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
84
Q

How do you cast?

A

( cast-to-type ) expression

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
85
Q

What is the purpose of typedef? How do you typedef?

A

To give alternate names to existing types.

typdef existing-type symbol

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
86
Q

Why not just use macros instead of typedefs?

A

Type definitions are more powerful. Array and pointer types cannot be defined as macros. Typedef names are subject to the same scope rules as variables. Macros are not and cause unexpected errors.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
87
Q

Definition: array

A

A data structure containing a number of data values, all of which have the same type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
88
Q

What is needed to declare an array?

A
  1. a type
  2. an identifier
  3. the size of the array enclosed in brackets OR empty brackets with an initializer
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
89
Q

What is an array initializer?

A

a list of constant expressions enclosed in {} and separated by commas

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
90
Q

How are multidimensional arrays stored in memory?

A

Row major order. Row 1 is ascending index order, then row 2, so on and so on.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
91
Q

What is a constant array?

A

An array declared const cannot be modified.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
92
Q

What is the assumed return type of a function that does not specify its return type?

A

int

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
93
Q

What is a function prototype?

A

A way to provide the compiler the way to call a function before we implement it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
94
Q

What is the difference between parameters and arguments?

A

Parameters appear in function definitions. They are dummy names for the values passed in the function call.

Arguments are expressions that appear in function calls.

Arguments supply parameters their value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
95
Q

What does “pass by value mean”?

A

In each function call, the evaluated argument’s value is assigned to the corresponding parameter

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
96
Q

What is main the implication of pass by value?

A

Changes made to the parameter does not affect the value of the argument.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
97
Q

How can you write a function that changes values?

A

Define a function that takes pointers to the values you want to alter.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
98
Q

What does the compiler do if it encounters arguments that don’t match the types of the parameters?

A

Case 1:
The compiler has seen a prototype for the function - The value of each argument is implicitly converted to the corresponding parameter’s type

Case 2:
The compiler has not seen a prototype for the function - The compiler performs default argument conversion. Floats are promoted to doubles, integral promotions are performed (char/short to int)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
99
Q

What happens when an array is passed to a function?

A

The function is given a pointer to the first element of the array.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
100
Q

What is the value that main returns?

A

A status code to the operating system

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
101
Q

What is the difference between return and exit?

A

Exit can be called from any function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
102
Q

What is a local variable?

A

A variable only accessible in the block in which it is defined. Typically inside a function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
103
Q

What are the default properties of a local variable?

A

Automatic storage duration

Block scope

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
104
Q

What is storage duration?

A

The portion of program execution during which storage for the variable exists.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
105
Q

What is automatic storage duration?

A

Storage for an auto variable is allocated when the program flow enters its block and is deallocated when flow leaves its block

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
106
Q

What is scope?

A

The portion of the program text in which a variable can be referenced.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
107
Q

What is block scope?

A

A variable with block scope is only visible from its point of declaration to the end of its block

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
108
Q

What is a static variable?

A

A variable with static storage duration has a permanent storage location. It retains its value throughout the execution of the program.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
109
Q

What properties do parameters have?

A

Same as local variables:

automatic storage duration and block scope

110
Q

What properties do global variables have?

A

Static storage duration

File scope

111
Q

What is file scope?

A

A variable with file scope is visible from the point of its declaration until the end of the file

112
Q

What is a block?

A

A compound statement that contains declarations

113
Q

What happens when a variable is declared inside a block that has the same identifier as a variable outside the block?

A

The new declaration hides the old one for the duration of the block

114
Q

What is a reference type?

A

Every pointer needs a reference type, or rather, a type that the pointer points to

115
Q

What two operators does C provide to work with pointers?

A

& - the address of operator, returns the location in memory of its operand

    • the indirection operator, “follows the pointer” to its value
116
Q

How do you prevent a function from changing the value of variable whose pointer was passed to the function?

A

Declare the parameter to be const

117
Q

What three forms of pointer arithmetic does C support?

A
  1. Adding an integer to pointer
  2. Subtracting an integer from a pointer
  3. Subtracting two pointers
118
Q

What does subtracting array pointers yield?

A

The number of elements between the pointers

119
Q

What is the meaning of the expression:

*p++

A

value of expression is *p, increment p later

120
Q

What is the meaning of the expression:

(*p)++

A

value of expression is *p, increment *p later

121
Q

What is the meaning of the expression:

*++p

A

increment p, value of expression is *p

122
Q

What is the meaning of the expression:

++*p

A

increment *p first, value of expression is *p

123
Q

How would you rewrite a[3] = j using pointers?

A

*(a + 3) = j

124
Q

What is the difference between using the array name and a pointer to the array?

A

You cannot assign a new value to the array name. Additionally, int a[10] sets aside 10 memory locations, int *a does not.

125
Q

Why is a[i] equivalent to i[a]

A

The compiler treats a[i] as *(a + i) which is the same as *(i + a)

126
Q

What is a string literal?

A

A sequence of characters enclosed in “”

127
Q

Why are we allowed to use a string literal whenever a char * is required?

A

C treats string literals as character arrays.

128
Q

What is the conversion specification to print or read a string?

A

%m.ps

m: minimum field width
p: print the first p characters
s: string

129
Q

Why do you not need to put a & in front of a string variable in scanf(“%s”, str)?

A

str is a character array, thus str is treated a pointer automatically

130
Q

What is important to remember about inputting strings with scanf?

A

A string read using scanf will never contain white-space

131
Q

What function should you use to read an entire line of text?

A

gets doesn’t skip whitespace, it stops reading when it sees a \n (does not include \n)gets doesn’t skip whitespace, it stops reading when it sees a \n (does not include \n)

132
Q

What is a ragged array?

A

An array whose rows are different lengths.

133
Q

Considering command-line arguments: what is stored at argv[argc]?

A

A null pointer

134
Q

Why is char *argv[] equivalent to char **argv for command line arguments?

A

The arguments are string literals. You reference string literals by pointers to their first character. If you have many arguments, then you need an array of character pointers to access them. C treats array names and pointers in parameters the exact same.

135
Q

What is the preprocessor?

A

A program that sets up the source code for the compiler

136
Q

What is a macro?

A

The #define preprocessor directive creates macros - names that represent something else. The preprocessor will expand these macros during preprocessing

137
Q

What are the 6 preprocessor conditional directives?

A

if, #ifdef, #ifndef, #elif, #else, #endif

138
Q

What are the 5 predefined macros?

A
\_\_LINE\_\_
\_\_FILE\_\_
\_\_DATE\_\_
\_\_TIME\_\_
\_\_STDC\_\_
139
Q

What is the #ifdef identifier operator equivalent to?

A

if defined(identifier)

140
Q

What is the #error directive?

A

Indicates a serious flaw in the program and causes most compilers to stop compiling. It prints the message associated with the directive

141
Q

What does the keyword extern do in conjunction with a variable declaration?

A

Informs the compiler that the variable is defined elsewhere and there is no need to allocate space for it.

142
Q

How do you protect a header file from being included multiple times?

A

endif

Enclose its contents in a #ifndef-#endif block. For example: a header file named stack.h:

#ifndef STACK_H
#define STACK_H

stuff

143
Q

When are header files compiled?

A

They aren’t. Their contents are compiled along with the file that includes them

144
Q

What does the linker do?

A

The linker combines object files created by the compiler into an executable image. The linker is responsible for resolving external references left behind by the compiler from function calls to other files and references to external variables.

145
Q

What is a makefile?

A

A file containing the necessary information to build a program. It lists the files and describes their dependencies.

146
Q

What is the format of a make file entry?

A

target : dependencies
command

example:
fmt : fmt.o word.o line.o
gcc -o fmt fmt.o word.o line.o

147
Q

How does the makefile know which files to recompile during a rebuild?

A

It checks the timestamps of the files to determine which are out of date.

148
Q

What is the primary difference between an array and a structure?

A

Structures can be composed of different types

149
Q

What is the primary difference between structs and unions?

A

Members of structs are stored at different memory locations, members of unions are stored at the same address.

150
Q

What is a namespace?

A

A container that provides context for the identifiers it holds, and allows for the disambiguation of homonym identifiers in different namespaces.

151
Q

How do we access a member of a struct?

A

identifier.member

152
Q

Are members of structs lvalues or rvalues?

A

lvalues

153
Q

How do you create a copy of struct?

A

structVar1 = structVar2

assuming they are the same type

154
Q

How do we define and use a structure tag?

A

define: struct identifier{ stuff };
use: struct identifier ..

155
Q

How we do define a type name for a struct?

A

typedef struct identifier{stuff} Typename;

156
Q

What is the downside to passing a structure to a function or returning a structure from a function? What is a logical improvement to this paradigm?

A

The stack frame must allocate space for the structure which can be costly if the structure is large. By passing or returning pointers we can improve performance.

157
Q

What is one of the primary uses of unions?

A

They can be used to save space in structures when mutually exclusive members exist.

158
Q

What is one of the major problems inherent with using unions? How do we rectify it?

A

There is no way to tell which member of the union was last changed, and therefore contains a meaningful value. The simplest solution is embed a union in a struct where the struct has a tag field indicating which member was last changed.

159
Q

What is an enumeration?

A

A type whose values are listed and named.

160
Q

What is one of the main functional differences between enumerations and #define constants

A

enumerations are subject to C’s scope rules

161
Q

What are the 3 ways to define enumerations?

A

enum identifier {…};
typedef enum {…} identifier;
typedef enum tag {…}identifer;

162
Q

How could you use enum to create a Boolean type?

A

typedef enum{FALSE, TRUE} Boolean;

163
Q

How do you alter the values associated with each constant in an enumeration?

A

typedef enum{CONST1 = …}identifier;

164
Q

What is dynamic memory allocation? Where is this memory allocated from.

A

The allocation of memory storage for use during the runtime of that program. Its allocated from the heap (usually)

165
Q

What is one of the primary benefits of dynamic memory allocation?

A

We can create data structures that grow and shrink during execution

166
Q

What are the three dynamic memory allocation functions and what header are they declared in?

A

malloc() calloc() realloc()

167
Q

What is the difference between malloc and calloc?

A

malloc allocates a block of memory but doesn’t initialize it. calloc allocates a block of memory and clears it

168
Q

What does realloc do?

A

Resizes a previously allocated block of memory

169
Q

What do malloc, calloc, and realloc return? What if the memory couldn’t be allocated?

A

void *
a generic memory pointer

if it couldn’t allocate the memory -it returns a null pointer

170
Q

What is crucial to do when using memory allocation functions?

A

Check to see if the value returned is a null pointer.

171
Q

If p is of type char *, why is it not necessary to cast the void * in
p = malloc(sizeof(“stuff”));

A

There is an implicit conversion when a variable of one type is assigned a variable of another type.

172
Q

How would we use malloc to allocate space for an array of n struct a’s?

A

struct a *arr = malloc(n * sizeof(struct a));

173
Q

How is calloc used?

A

void *calloc(size_t nelem, size_t size)

returns a generic pointer to a zeroed block of memory large enough for nelem number of elements each of size length

174
Q

How is realloc used?

A

void *realloc(void *ptr, size_t size)
returns a generic pointer to a block of memory previously allocated by a memory allocation function identified by ptr and resized to size

175
Q

What is garbage? How does it relate to memory leak? How do we handle this?

A

Garbage is memory that no longer has a pointer to it and therefore can no longer be referenced. Programs that have this scenario have a memory leak. C does not have a garbage collector so we are responsible for freeing memory blocks that are no longer needed.

176
Q

How is free used?

A
void free(void *ptr)
frees the memory identified by ptr. ptr must point to a block that has been previously allocated by a memory allocation function
177
Q

What is the dangling pointer problem?

A

When a dynamically allocated block is freed, the pointer still exists but does not point to anything. Attempting to use the pointer again can cause bugs.

178
Q

What is important to remember when a structure has a member that is a pointer to said structure?

A

We are required to use a structure tag, and cannot use a type definition.

179
Q

What is a linked list?

A

A data structure that has a data part and a pointer to the next node in the list.

180
Q

What is the process of creating a node?

A
  1. allocate memory for the node
  2. store the data in the node
  3. insert the node into the list
181
Q

How can you use malloc to create a node?

A

struct node *newNode = malloc(sizeof(struct node));

182
Q

What is the right arrow selection operator?

A

->

it is equivalent to (*identifier).element

183
Q

What are the three steps in deleting a node from a linked list?

A
  1. find the node
  2. make the node before the node to be deleted point to the node after the node to be deleted
  3. free the memory for the deleted node
184
Q

How do you create a function pointer?

A

return-type (id)(parameter-type, ..);
ex:
void (
pf)(int)
a function pointer to any function that returns void and takes a single int

185
Q

How do you use a function pointer?

A
1. define a function pointer
void (*fp)(int)
2. assign it a function 
fp = func;
(correct form is use &amp;func, but its not necessary)
3. call the function
fp(10);
you can use (*fp)(10)
186
Q

What the difference between a declaration and a definition?

A

A declaration introduces an identifier and describes its type, be it a type, object, or function. A declaration is what the compiler needs to accept references to that identifier.

A definition actually instantiates/implements this identifier. It’s what the linker needs in order to link references to those entities.

187
Q

What is the general form for declarations?

A

declaration-specifiers declarators;

188
Q

What are declaration specifiers? What 3 categories do they fall into?

A

Declaration specifiers describe the properties of of the items being declared.

They fall into three categories:
storage classes
type qualifiers
type specifiers

189
Q

What are the 4 storage classes? What is the restriction placed on their combination?

A

auto, static, extern, register

at most one may appear in a declaration

190
Q

What are the 2 type qualifiers? What is the restriction placed on their combination?

A

const, volatile

any combination may appear in a declaration

191
Q

What are the type specifiers?

A

void, char, short, int, long, float, double, signed, unsigned, struct, union, enum

192
Q

What are declarators?

A

In short: identifiers

variable names, array names, pointer names, function names, etc.

193
Q

What is the storage duration, scope, and linkage of: a variable declared inside a function

A

auto
block
none

194
Q

What is the storage duration, scope, and linkage of: variables declared outside any block

A

static
file
external

195
Q

What is the storage duration, scope, and linkage of: a static variable declared outside any block

A

static
file
internal

196
Q

What is the storage duration, scope, and linkage of: a static variable declared inside a block

A

static
block
none

197
Q

What does the extern storage class enable?

A

The sharing of the same variable between files

198
Q

What does putting extern in front of a variable do?

A

Indicates to the compiler that the variable is defined somewhere else in the program, and space should not be allocated for it.

199
Q

What storage class do extern variables always have?

A

static

200
Q

What determines an extern variables scope?

A

The placement of its declaration. Inside a block means block scope, otherwise file scope.

201
Q

What does declaring a variable to have the storage class register mean?

A

It requests a variable be stored in a cpu register.

202
Q

Where is the only legal place for a register variable to be declared?

A

In a block

203
Q

What is the storage storage duration, scope, and linkage of a register variable?

A

Same as an auto variable.
auto
block
no linkage

204
Q

What is a crucial difference between an auto variable and a register variable?

A

You cannot use & with a register variable

205
Q

Why would you use the register storage class?

A

Because registers are accessed more quickly than memory, you can use them for variables that are accessed/changed frequently like loop counters

206
Q

What does extern indicate in conjunction with function declarations/definitions?

A

It specifies that the function has external linkage, allowing it to be called from other files.

207
Q

What does static in conjunction with function declarations/definitions mean?

A

Specifies the function has internal linkage -the function may be called only within the file in which it is defined.

208
Q

What storage class is assumed for functions if no storage class is defined?

A

extern, and thus specifying a function to be extern is redundant

209
Q

What is the type qualifer const used for?

A

To declare objects that are read-only

210
Q

What does the volatile type qualifer indicate?

A

It tells the compiler that the value of the variable may change at any time–without any action being taken by the code the compiler finds nearby. This has implications especially when dealing with compiler optimization

211
Q

What is the rule of thumb for deciphering complex declarations?

A
  1. Read from the inside out - find the identifier and start from there
  2. When there is a choice, favor [] and () over * - if the identifier is *id[] it is an array of pointers not a pointer to an array
212
Q

What is the restriction on initializers for static variables?

A

The initializer must be constant

213
Q

What exactly is the difference between “scope” and “linkage”?

A

Scope is for the benefit of the compiler, while linkage i for the benefit of the linker. The compiler uses the scope of an identifier to determine whether or not it’s legal to refer to the identifier at a given point in a file. When the compiler translates a source file into object code, it notes which names have external linkage, eventually storing these names in a table inside the object file. Thus, the linker has access to names with external linkage; names with internal linkage or no linkage are invisible to the linker.

214
Q

Why can’t const objects be used in constant expressions?

A

A const object is only guaranteed to stay constant during its lifetime, not throughout the execution of the program.

215
Q

What is a module?

A

A collection of services, some of which are made available to other parts of the program (clients)

216
Q

What is a module interface?

A

A common means for two unrelated modules to interact.

217
Q

What are the two properties that modules should have?

A

High cohesion, and low coupling

218
Q

What is cohesion?

A

The extent to which elements of a module are related.

219
Q

What is coupling?

A

The extent to which modules are dependent on each other.

220
Q

What are the 4 bitwise operators from highest precedence to lowest precedence?

A

~
&

|

221
Q

How do you set a bit in a bit string?

A

Do a bit wise or with a zeroed string but with a 1 in the position of the bit you want to set

222
Q

How do you clear a bit?

A

Do a bitwise and with a string with all 1’s but with a zero in the position you want to clear

223
Q

How do you flip a bit?

A

Do a bitwise XOR with a zeroed string with a 1 in the position you want to flip

224
Q

How can you use bitshifts to create a bit mask?

A

If you want a 1 in position j you would do:
i = (1 &laquo_space;j)

If you want a zero in position j:
i = ~(1 &laquo_space;j)

225
Q

How do you declare a structure whose members represent bit fields?

A

struct identifier{
(unsigned) int: #ofbit
};

unsigned is optional

226
Q

Why would we need a type qualifier such as volatile?

A

If we have a pointer to a volatile memory location, the data at that location can change without program interference. If we write a program that has a pointer to that location and we are continually checking it -the compiler might recognise that we aren’t changing the pointer or the pointee and optimize the code by fetching the value once and storing it in a register. This would not be what we want because we would only have the value of the volatile memory address at a specific point in time, and not as it continuously changes.

227
Q

What is a stream in C?

A

Any source of input or any destination for output

228
Q

How are streams accessed in C?

A

Through file pointers… FILE *fp

229
Q

What are the 3 ready-to-use streams provided by stdio.h

A

stdin - keyboard
stdout - screen
stderr - screen

230
Q

What are the two types of files supported by stdio.h? What is the difference between them?

A

Text files - Bytes represent characters, making it human-readable

Binary files - Bytes represent other types of data.. ints, floats, etc

231
Q

What are the 6 mode strings? How do you make a mode string for a binary file?

A

r - reading
w - writing (file need not exist)
a - appending (file need not exist)
r+ - reading and writing starting at beginning
w+ - reading and writing (truncate if exists)
a+ - reading and writing (append if exists)

To make a binary mode string, add b to the mode strings listed above.

232
Q

What does freopen do? What is its most common use?

A

Attaches a different file to a stream that’s already open. Its most common use is to associate a file with one of the standard streams.

233
Q

What is freopen’s normal return value?

A

It’s third argument, a file pointer to a stream like stdout.

234
Q

What does tmpfile from do?

A

Returns a pointer to a file that will remain open until its closed or until the program ends.

235
Q

How does output buffering work?

A

Data to be stored is written to a buffer. When the buffer is full or the stream is closed, the buffer is flushed (written to device).

236
Q

How does input buffering work?

A

Input is put into an input buffer, and input is read from this buffer as opposed to reading from the actual device.

237
Q

What does fflush do? What arguments does it take? What does it return?

A

Flushes a file’s buffer.

Takes a file pointer or NULL, NULL flushes all buffers.

If it is successful, it returns 0. If there is an error it returns EOF.

238
Q

What is setvbuf used for?

A

Allows us to change the way a stream is buffered and to control the size and location of the buffer.

239
Q

What is full buffering?

A

characters are transmitted to the system as a block when a buffer is filled

240
Q

What is line buffering?

A

characters are transmitted to the system as a block when a new-line character is encountered. Line buffering is meaningful only for text streams and UNIX file system files.

241
Q

What is no buffering?

A

characters are transmitted to the system as they are written. Only regular memory files and UNIX file system files support the no buffering mode.

242
Q

What is the benefit of creating a buffer with automatic storage duration? What is the benefit of using a dynamically allocated buffer?

A

Using an automatic variable allows space to be reclaimed automatically at the end of the block.

Using dynamically allocated space allows us to reclaim the space when we no longer need the buffer.

243
Q

What is the restriction placed on using setvbuf

A

It must be called after the stream has been opened but before any other operations are performed on it.

244
Q

What is assert used for?

A

Allows a program to monitor its own behavior and detect possible programs at an early stage.

245
Q

What is an assertion>

A

An expression that we expect to be true under normal circumstances.

246
Q

How does assert work?

A

It takes an integer argument(an assertion) that it tests when it executes. If the argument in non-zero: assert does nothing. If it is zero assert prints a message to stderr and calls the abort function to terminate the program.

247
Q

How do you disable assert using 1 line of code?

A

Define the macro NDEBUG prior to including

#define NDEBUG
#include
248
Q

What is the errno variable? What header is it declared in?

A

Some functions indicate failure by storing an error code in the errno variable.

errno is defined in

249
Q

What is important to remember when checking if a function has altered errno?

A

You must clear errno before using a function that might change errno. If an error code has been stored in errno prior to calling a function, and the recent function does not return an error code, errno will retain the previous error code. Library functions do not clear errno.

250
Q

What does the signal.h header do?

A

provides facilities for handling exceptional conditions, known as signals

251
Q

What are the two categories that signals fall into?

A

Run-time errors

External events

252
Q

What does it mean that signals can happen asynchronously?

A

Signals can be raised at any time during program execution, not just at certain points known to the programmer.

253
Q

What does the signal function do?

A

Installs a signal handling function for use later if a given signal should occur.

254
Q

What are the two parameters to the signal function?

A

The first is the code for a particular signal (usually a macro)
The second is a function pointer to a function that will handle the signal

255
Q

What argument must every signal handling function be passed?

A

An int for the signal code that will be passed to the handler.

256
Q

What happens if the signal handling function returns?

A

Execution resumes from the point where the signal occurred.

257
Q

What are the two predefined signal handlers provided by signal.h?

A

SIG_DFL : the default handler -usually causes program termination

SIG_IGN : specifies the signal code should be ignored should it arise

258
Q

What is the SIG_ERR macro used for?

A

It’s used to test whether a signal handler can be installed. If it can’t signal returns SIG_ERR and stores a positive value in errno.

259
Q

What prevents infinite recursion if the function handler raises a signal?

A

C requires that when a handler is called for signals other than SIGILL, the handler be reset to SIG_DFL or blocked in some other way.

260
Q

What has to happen in order for a signal to be handled twice by the same handler?

A

The signal handler has to be reinstalled. This can be done at the end of the handler’s block.

261
Q

What does the raise function do? What is its argument? What does it return?

A

raises causes a signal to occur

its argument is the signal code to be raised

its return value is 0 for success, nonzero for failure

262
Q

What does the setjmp.h header do?

A

Makes it possible for one function to jump directly to another function without returning. It’s primarily used for error handling

263
Q

What does the setjmp.h header do?

A

Makes it possible for one function to jump directly to another function without returning.

264
Q

What does the setjmp function do? How do we use it? What does the longjmp function do? How do we use it?

A

setjmp marks a place in a program so we can jump to it later from longjmp. We pass setjmp a variable of type jmp_buf, it then returns 0. longjmp returns to the point where we used setjmp. we use it by passing it the jmp_buf variable we used with setjmp and another int value for the status code.

265
Q

What does the float.h header provide?

A

Macros that define the range and accuracy of the floating types. It provides no types or functions

266
Q

What does the limits.h header provide? What doesn’t it provide?

A

Macros that define the range of each integer and character type. It provides no types or functions.

267
Q

What is does the ctype.h header contain?

A

2 kinds of functions:
character testing functions
character case-mapping functions

268
Q

What is the stdarg.h header used for?

A

Writing functions of variable length argument lists.

269
Q

What are the 4 components needed to create a function with a variable length argument list?

A

va_list - needed to iterate through the arguments
va_start - needed to indicate where the arg list starts
va_arg - fetches the arguments and advances the pointer
va_end - required to clean up before the function can return

270
Q

What is linkage?

A

The extend to which a variable can be shared by different parts of a program

271
Q

What are the different types of linkage?

A

external - shared by several files
internal - shared by functions in a file
no linkage - belongs to a single function