notes/education/software development/ECE1400/Chapter 15 Exercises.md

42 lines
1.7 KiB
Markdown
Raw Normal View History

2024-11-11 03:31:05 +00:00
> **1.** Section 15.1 listed several advantages of dividing a program into multiple source files.(a). Describe several other advantages
> (b). Describe some disadvantages
---
2024-11-11 03:36:05 +00:00
> **2.** Which of the following should *not* be put in a header file? Why not?
b. Function definitions - Functions should only be defined once, and this allows multiple files to share the same function definition
---
2024-11-11 03:41:05 +00:00
> **3.** We saw that writing `#include <file>` instead of `#include "file"` may not work if file is one that we've written. Would there be any problem with writing `$include "file"` instead of `#include <file>` if *file* is a system header?
Yes, `""` is a path relative to the current file, whereas `<>` is a path to the system's standard library headers.
---
> **4.** Assume that `debug.h` is a header file with the following contents...
>(a). What is the output when the program is executed?
2024-11-11 03:56:06 +00:00
2024-11-11 03:41:05 +00:00
```
2024-11-11 03:51:05 +00:00
Output if DEBUG is defined:
Value of i: 1
Value of j: 2
Value of i + j: 3
Value of 2 * i + j - k: 1
```
2024-11-11 03:56:06 +00:00
2024-11-11 03:51:05 +00:00
> (b). What is the output if the `#define` directive is removed from `testdebug.c`?
2024-11-11 03:56:06 +00:00
```
Output if DEBUG is not defined:
```
> (c). Explain why the output is different in parts (a) and (b)
2024-11-11 04:01:06 +00:00
When `DEBUG` is defined, any instances of the `PRINT_DEBUG` token are replaced with a `printf` call during compile time, but when it's not defined, they're replaced with nothing.
> (d). Is it necessary for the `DEBUG` macro to be defined *before* `debug.h` is included in order for `PRINT_DEBUG` to have the desired effect? Justify your answer.
Macro invocations are evaluated sequentially, and so if `DEBUG` was defined after `PRINT_DEBUG`, then any usages of `PRINT_EVALUATION` would be have like `DEBUG` was not defined.