Comp2 (midterm to week 7) Flashcards
What does computer do differently when it sees #include <filename.h> vs #include "filename.h"</filename.h>
The “” causes the preprocessor to begin searching for the file to be included in the same directory as the file being compiled.
The <> causes a search to be performed in an implementation-dependent manner, normally through predesignated compiler and system directories.
define PI = 3.141;
What is the error? Why?
= not allowed.
Everything to the right replaces the symbolic constant.
Can a symbolic constant be re-defined?
No.
What is a macro?
An operation defined as symbols?
What happens when a macro has arguments?
The arguments are substituted in the replacement text. I.e. the macro is called with certain values.
What happens for multiline macros and symbolic constants?
\ must be placed at the end of the line indicating the replacement text continues on the next line.
What must we do if passing c + 2 to the macro #define CIRCLE_AREA(x)(PI * x * x)
The PI and the two x’s must be enclosed in parenthesis to force the correct order of operation.
Another solution is to enclose macro arguments in parenthesis.
Why are macros obselete?
Macros were created to replace function calls with inline code to eliminate the function-call overhead.
For the macro #define CIRCLE_AREA(x)((PI) * (x) * (x)) By passing r++ explain why this macro is unsafe?
because the ++ operation would be executed twice which is not allowed in C so the second r++ would be undefined.
How do you specify that a function takes a variable number of arguments of any type?
Make … one of the parameters.
What library is used for variable length arguments? Describe the syntax requirements within the variable length argument function average which takes in the number of arguments and any number of numbers.
stdarg.h
double total = 0
va_list ap; // stores information needed by va_start and va_end;
va_start(ap, i); //i is the rightmost argument before the ellipsis
for (int j = 1; j <= i; j++){
total += va_arg(ap, double); //Adds the arguments of type double to total
}
va_end(ap); //clean-ip variable-length argument list
return total / i;
Correct format for a struct. Can a struct contain a struct of the same type? Workaround?
Name for said workaround?
struct name{
data
} typedef Name;
No but can contain a pointer to a struct of the same type. This is called a self-referential structure.
Can u compare structs? Why?
No because struct members are not necessarily stored in consecutive bytes of memory.
Syntax for using structure member operator and structure pointer operator.
struct card aCard;
struct card *cardPtr;
aCard.suit =
cardPtr->suit =
What is typedef Card equivalent to? Stylistic Tradition for typedef?
struct card
Capitilise fist letter of name after typedef