Comp2 (midterm to week 7) Flashcards

1
Q

What does computer do differently when it sees #include <filename.h> vs #include "filename.h"</filename.h>

A

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.

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

define PI = 3.141;

What is the error? Why?

A

= not allowed.
Everything to the right replaces the symbolic constant.

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

Can a symbolic constant be re-defined?

A

No.

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

What is a macro?

A

An operation defined as symbols?

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

What happens when a macro has arguments?

A

The arguments are substituted in the replacement text. I.e. the macro is called with certain values.

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

What happens for multiline macros and symbolic constants?

A

\ must be placed at the end of the line indicating the replacement text continues on the next line.

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

What must we do if passing c + 2 to the macro #define CIRCLE_AREA(x)(PI * x * x)

A

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.

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

Why are macros obselete?

A

Macros were created to replace function calls with inline code to eliminate the function-call overhead.

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

For the macro #define CIRCLE_AREA(x)((PI) * (x) * (x)) By passing r++ explain why this macro is unsafe?

A

because the ++ operation would be executed twice which is not allowed in C so the second r++ would be undefined.

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

How do you specify that a function takes a variable number of arguments of any type?

A

Make … one of the parameters.

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

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.

A

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;

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

Correct format for a struct. Can a struct contain a struct of the same type? Workaround?
Name for said workaround?

A

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.

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

Can u compare structs? Why?

A

No because struct members are not necessarily stored in consecutive bytes of memory.

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

Syntax for using structure member operator and structure pointer operator.

A

struct card aCard;
struct card *cardPtr;
aCard.suit =
cardPtr->suit =

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

What is typedef Card equivalent to? Stylistic Tradition for typedef?

A

struct card

Capitilise fist letter of name after typedef

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

How do you access members in a nested anonymous struct?

A

Accessed the same way as a normal member, struct is treated as if it didn’t exist.

17
Q

Define an array of integers of size 5 using malloc.
What function is used to free the memory?

A

int *arr;
arr = (int *)malloc(5 * sizeof(int));

free(arr);

18
Q

Define an array of integers of size 5 using calloc.

A

int *arr;
arr = (int *)calloc(5, sizeof(int));

19
Q

What is returned by malloc and calloc if no memory is available? If succesful?

A

A null pointer
If succesful return a pointer to the allocated memory.

20
Q

What is the difference between malloc and calloc?

A

The memory assigned to malloc is not initialised but the memory assigned to calloc is, so calloc clears the memory it allocates.

21
Q

What does realloc do?

A

-Realloc changes the size of an object allocated by a previous malloc, calloc or realloc.
-Content is not modified if the new amount of memory allocated is larger than the amount allocated previously. Otherwise the contents are unchanged up to the new size.
-Takes in a ptr to the original object and a new size.
-If the ptr is null, works like malloc.
-If ptr is not null and size is greater than zero, realloc tries to allocate a new block of memory for the object.

22
Q

Difference between a union and a structure.

A

Members share the same storage space. Only one member can be referenced at a time.

23
Q

How is a union declared?

A

A union can only be assigned at declaration with a value of the same type as its first member.

24
Q

How much storage does a union require?

A

As much storage as is required by its largest member.