From fc5971f516b81397f9a283d7465d2799c7f6a8e3 Mon Sep 17 00:00:00 2001 From: Joshua Potter Date: Tue, 21 Jan 2025 08:33:35 -0700 Subject: [PATCH] Better organize the collection of C17 notes. --- .../plugins/obsidian-to-anki-plugin/data.json | 17 +- notes/_journal/2024/2024-09/2024-09-28.md | 2 +- notes/_journal/2024/2024-10/2024-10-03.md | 2 +- notes/_journal/2024/2024-11/2024-11-23.md | 2 +- notes/_journal/2024/2024-11/2024-11-24.md | 2 +- notes/_journal/2025-01-20.md | 9 - notes/_journal/2025-01-21.md | 9 + notes/_journal/2025-01/2025-01-20.md | 13 + notes/c17/declarations.md | 551 +++- notes/c17/functions.md | 666 ----- notes/c17/pointers.md | 2 +- notes/c17/storage.md | 187 +- notes/c17/typedefs.md | 160 -- notes/c17/types/compatible.md | 205 ++ notes/c17/types/conversions.md | 314 +++ notes/c17/types/derived.md | 879 ------ notes/c17/types/index.md | 2459 +++++++++++++++-- notes/c17/types/simple.md | 1486 ---------- notes/linkers/relocatable.md | 4 +- notes/set/cardinality.md | 72 + notes/trigonometry/index.md | 382 ++- notes/trigonometry/unit-circle.md | 8 +- notes/x86-64/procedures.md | 2 +- 23 files changed, 3913 insertions(+), 3520 deletions(-) delete mode 100644 notes/_journal/2025-01-20.md create mode 100644 notes/_journal/2025-01-21.md create mode 100644 notes/_journal/2025-01/2025-01-20.md delete mode 100644 notes/c17/functions.md delete mode 100644 notes/c17/typedefs.md create mode 100644 notes/c17/types/compatible.md create mode 100644 notes/c17/types/conversions.md delete mode 100644 notes/c17/types/derived.md delete mode 100644 notes/c17/types/simple.md diff --git a/notes/.obsidian/plugins/obsidian-to-anki-plugin/data.json b/notes/.obsidian/plugins/obsidian-to-anki-plugin/data.json index 4f8f50a..eb459c5 100644 --- a/notes/.obsidian/plugins/obsidian-to-anki-plugin/data.json +++ b/notes/.obsidian/plugins/obsidian-to-anki-plugin/data.json @@ -373,7 +373,7 @@ "c17/strings.md": "b021b6f18f865aa89b3088907cdaae94", "c17/index.md": "78e0ebc5a91d58b297b25346a654d398", "c17/escape-sequences.md": "a8b99070336878b4e8c11e9e4525a500", - "c17/declarations.md": "9dbaea14e28d00704cfd0b027efe8c97", + "c17/declarations.md": "d0d0b4ce2d2608dbfffa6182593eae7a", "algorithms/sorting/merge-sort.md": "6506483f7df6507cee0407bd205dbedd", "_journal/2024-02-24.md": "9bb319d5014caf962a9ce3141076cff4", "_journal/2024-02/2024-02-23.md": "0aad297148e8cc4058b48b7e45787ca7", @@ -835,7 +835,7 @@ "c17/enums.md": "9414fb67aa256a0a11b7240534c67bf6", "c17/derived-types.md": "6fb8f23a2423f05d5bdccb6672a32e38", "c17/basic-types.md": "7c6653bf6dc24c2f2aa72fc95c4f7875", - "c17/types/simple.md": "ee418366c1af73c0eea48b6cc024651f", + "c17/types/simple.md": "d4b5dbc6d543f737182cb2f5cf02f2fc", "c17/types/enumerated.md": "e1f70a30677c776b7b44ac3e0ff4e76d", "c17/types/derived.md": "fd1ecb61bed82fb6e44c170b023e5eeb", "c17/types/basic.md": "5064e21e683c0218890058882e06b6f3", @@ -987,7 +987,7 @@ "_journal/2024-11/2024-11-21.md": "951b6034d60a40dbd8201c50abf0dbb9", "_journal/2024-11/2024-11-20.md": "951b6034d60a40dbd8201c50abf0dbb9", "_journal/2024-11/2024-11-19.md": "d879f57154cb27cb168eb1f1f430e312", - "set/cardinality.md": "4c76186740c2ad2ae29ca9b8d9343065", + "set/cardinality.md": "f927d045d2231ff59f63e59ff6dd0406", "geometry/area.md": "7f947bb5ac782495a1fb4a63bb2463e7", "_journal/2024-11-23.md": "911f82ab8aede5ecdb96493aef64b0b9", "_journal/2024-11/2024-11-22.md": "51117030e2364dbce3a8d507dead86ae", @@ -1082,7 +1082,7 @@ "_journal/2024-12/2024-12-23.md": "72b0964a8a5ed8ba0acf7fe10b5de279", "_journal/2024-12-25.md": "1717d37b074df58175ec0272adc278de", "_journal/2024-12/2024-12-24.md": "dcd3bd8b82ca4d47a9642a46d8bece0d", - "linkers/relocatable.md": "ac24efbabe07222a89acb2fd5135cdb3", + "linkers/relocatable.md": "c64223f3fdd0e17be3d73977757da52a", "data-models/federation.md": "1d92747304186bd2833a00a488fcac48", "_journal/2024-12-26.md": "022aeaf68d46fd39b23aca9c577f3f41", "_journal/2024-12/2024-12-25.md": "1717d37b074df58175ec0272adc278de", @@ -1465,15 +1465,18 @@ "_journal/2025-01-16.md": "e3a21059205784a4e88bfe3b4deac7f7", "_journal/2025-01-17.md": "08a5f05bb572db9495bfc2b4feb8e0a9", "_journal/2025-01/2025-01-16.md": "e3a21059205784a4e88bfe3b4deac7f7", - "trigonometry/index.md": "b48577b0c5c6e4d587d597a0fcd08b0f", + "trigonometry/index.md": "bba87fba2825095cd727c7561906e4dc", "geometry/circle.md": "9469e5512a6a92a962985eab99d00606", "_journal/2025-01-18.md": "8655fbf94aeec13efe9b6d2087c1f37e", "_journal/2025-01/2025-01-17.md": "08a5f05bb572db9495bfc2b4feb8e0a9", "_journal/2025-01-19.md": "a37c6f534cf5e272619c5f813974afcf", "_journal/2025-01/2025-01-18.md": "7a1655887093f37ffe86309d90459b3b", - "_journal/2025-01-20.md": "2478f31199c1f0c2ffae2616b1ca435d", + "_journal/2025-01-20.md": "705040c268bd54d363753b09bfac37cf", "_journal/2025-01/2025-01-19.md": "65a56dcadec4f36c3a801cfb0de1a391", - "trigonometry/unit-circle.md": "4a8fb4b5ac1bc2b645f18b0b8079897c" + "trigonometry/unit-circle.md": "2d4fea38b543797f3d2237b645a07642", + "c17/types/compatible.md": "0aceb7385d878feacc7a70b9839d3450", + "_journal/2025-01-21.md": "d6475cfff346315718a8ef6ef1ff408f", + "_journal/2025-01/2025-01-20.md": "a206195abd13b7020e703bdcf11bdcdd" }, "fields_dict": { "Basic": [ diff --git a/notes/_journal/2024/2024-09/2024-09-28.md b/notes/_journal/2024/2024-09/2024-09-28.md index 67f2e67..0a2e475 100644 --- a/notes/_journal/2024/2024-09/2024-09-28.md +++ b/notes/_journal/2024/2024-09/2024-09-28.md @@ -9,5 +9,5 @@ title: "2024-09-28" - [ ] Korean (Read 1 Story) * Walked through the proof of the recursion theorem on $\omega$. -* Additional notes on [[simple#Exact-Width Integer Types|exact-width integer types]]. +* Additional notes on [[types/index#Exact-Width|exact-width integer types]]. * Severy more notes on C types/limits/etc. \ No newline at end of file diff --git a/notes/_journal/2024/2024-10/2024-10-03.md b/notes/_journal/2024/2024-10/2024-10-03.md index 18aa0fe..52a0211 100644 --- a/notes/_journal/2024/2024-10/2024-10-03.md +++ b/notes/_journal/2024/2024-10/2024-10-03.md @@ -8,4 +8,4 @@ title: "2024-10-03" - [ ] Sheet Music (10 min.) - [ ] Korean (Read 1 Story) -* Begin adding notes on C [[derived|derived types]]. \ No newline at end of file +* Begin adding notes on C [[c17/types/index#Derived Types|derived types]]. \ No newline at end of file diff --git a/notes/_journal/2024/2024-11/2024-11-23.md b/notes/_journal/2024/2024-11/2024-11-23.md index 054b761..76e397f 100644 --- a/notes/_journal/2024/2024-11/2024-11-23.md +++ b/notes/_journal/2024/2024-11/2024-11-23.md @@ -9,4 +9,4 @@ title: "2024-11-23" - [ ] Korean (Read 1 Story) * Notes on Apostol's axiomatic definition of [[area]]. -* More notes on [[derived#Pointers|pointers]]. \ No newline at end of file +* More notes on [[pointers|pointers]]. \ No newline at end of file diff --git a/notes/_journal/2024/2024-11/2024-11-24.md b/notes/_journal/2024/2024-11/2024-11-24.md index 389949b..723da87 100644 --- a/notes/_journal/2024/2024-11/2024-11-24.md +++ b/notes/_journal/2024/2024-11/2024-11-24.md @@ -8,4 +8,4 @@ title: "2024-11-24" - [ ] Sheet Music (10 min.) - [ ] Korean (Read 1 Story) -* Notes on the [[derived#NULL|NULL]] macro. \ No newline at end of file +* Notes on the [[pointers#NULL|NULL]] macro. \ No newline at end of file diff --git a/notes/_journal/2025-01-20.md b/notes/_journal/2025-01-20.md deleted file mode 100644 index f999276..0000000 --- a/notes/_journal/2025-01-20.md +++ /dev/null @@ -1,9 +0,0 @@ ---- -title: "2025-01-20" ---- - -- [ ] Anki Flashcards -- [x] KoL -- [x] OGS -- [ ] Sheet Music (10 min.) -- [ ] Korean (Read 1 Story) \ No newline at end of file diff --git a/notes/_journal/2025-01-21.md b/notes/_journal/2025-01-21.md new file mode 100644 index 0000000..a34703f --- /dev/null +++ b/notes/_journal/2025-01-21.md @@ -0,0 +1,9 @@ +--- +title: "2025-01-21" +--- + +- [x] Anki Flashcards +- [x] KoL +- [x] OGS +- [ ] Sheet Music (10 min.) +- [ ] Korean (Read 1 Story) \ No newline at end of file diff --git a/notes/_journal/2025-01/2025-01-20.md b/notes/_journal/2025-01/2025-01-20.md new file mode 100644 index 0000000..eb7c5c0 --- /dev/null +++ b/notes/_journal/2025-01/2025-01-20.md @@ -0,0 +1,13 @@ +--- +title: "2025-01-20" +--- + +- [x] Anki Flashcards +- [x] KoL +- [x] OGS +- [ ] Sheet Music (10 min.) +- [ ] Korean (Read 1 Story) + +* More flashcards on $\sin$, $\cos$, and the [[trigonometry/index#Pythagorean Identity|Pythagorean Identity]]. +* Watched [Space Filling Curves](https://www.youtube.com/watch?v=3s7h2MHQtxc), which provided a concrete example of $\mathbb{R} \times \mathbb{R} \approx \mathbb{R}$. +* Begin adding notes on [[compatible|compatible types]]. \ No newline at end of file diff --git a/notes/c17/declarations.md b/notes/c17/declarations.md index db0ec35..fe8569f 100644 --- a/notes/c17/declarations.md +++ b/notes/c17/declarations.md @@ -230,7 +230,7 @@ A **definition** is a declaration that causes storage to be reserved for the obj %%ANKI Basic A definition contains how many declarators? -Back: One. +Back: One or more. Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). END%% @@ -349,6 +349,555 @@ Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co END%% +## Prototypes + +There exist two ways for a function declaration to use declarators: **parameter type lists** and **identifier type lists**. To make the distinction clear, consider the following ways of defining an `add` function: + +```c +int f(int x, int y) { return x + y; } // Paramter type list +int f(x, y) int x; int y; { return x + y } // Identifier type list +``` + +A function **prototype** is a function declaration that specifies a function signature. There are three important points to make note of: + +* Empty identifier lists are interpreted as "the compiler has not been told what this function's arguments are." +* The standard prohibits declaring functions with a non-empty identifier list. +* Empty parameter lists are not allowed. + +Therefore: + +```c +// Uses an empty identifer list. This declares a function `foo` +// that takes an unknown specification of arguments. +void foo(); +// Uses a non-empty identifier list. Compiler error. +void foo(x, y); +// Uses a non-empty identifier list. Compiler error. +void foo(x, y) int x; int y; +// Uses a non-empty identifier list. Definitions allow this. +void foo(x, y) int x; int y; { } +// Uses a non-empty parameter list. This prototypes a function +// `foo` that takes no arguments. +void foo(void); +// Uses a non-empty parameter list. This prototypes and defines +// a function `foo` that takes no arguments. +void foo(void) {} +``` + +Together these points imply a function prototype *must* use a parameter type list. + +%%ANKI +Basic +Which of prototypes or declarations are more general? +Back: Declarations. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +What two ways are parameters declared in function declarations and definitions? +Back: Identifier type lists and parameter type lists. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +Which of identifier type lists and/or parameter type lists are considered obsolete? +Back: Identifier type lists. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +Define an addition function using identifier type lists. +Back: +```c +int add(x, y) int x; int y; { return x + y; } +``` +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +Define an addition function using parameter type lists. +Back: +```c +int add(int x, int y) { return x + y; } +``` +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +Is the following a prototype or a declaration? +```c +void foo(); +``` +Back: A declaration. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +What compilation error does the following raise? +```c +void foo(); +``` +Back: N/A. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +What compilation error does the following raise? +```c +void foo(x, y); +``` +Back: A function declaration cannot have a non-empty identifier list. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +What compilation error does the following raise? +```c +void foo(x, y) int x; int y; +``` +Back: A function declaration cannot have a non-empty identifier list. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +What compilation error does the following raise? +```c +void foo(x, y) int x; int y; {} +``` +Back: N/A. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +What function prototype is declared in the following? +```c +void foo(x, y) int x; int y; {} +``` +Back: N/A. No prototype has been declared. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +What compilation error does the following raise? +```c +void foo(); + +int main(void) { foo(1); } +``` +Back: N/A. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +Is the following a prototype, declaration, both, or neither? +```c +void f(); +``` +Back: A declaration. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +Is the following a prototype, declaration, both, or neither? +```c +void f(void); +``` +Back: Both. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +What compilation error does the following raise? +```c +void foo(void); +``` +Back: N/A. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +What compilation error does the following raise? +```c +void foo(void); + +int main(void) { foo(1); } +``` +Back: Too many arguments to function call `foo`. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +What function prototype is declared in the following? +```c +void foo(int x, int y); +``` +Back: `void foo(int, int)` +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +What function prototype is declared in the following? +```c +void foo(int x, int y) {} +``` +Back: `void foo(int, int)` +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +How many arguments does the following declaration specify? +```c +void foo(); +``` +Back: Some number unknown to the compiler. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +How many arguments does the following declaration specify? +```c +void foo(void); +``` +Back: Zero. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +Why might the following snippet raise a compilation error? +```c +int foo(); +int foo(int a); +``` +Back: N/A. It likely wouldn't. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +*Why* might the following snippet raise a compilation error? +```c +int foo(); +int foo(float a); +``` +Back: Conflicting types. The first `foo` declares any `float` passed to it default promotes to a `double`. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +*Why* might the following snippet raise a compilation error? +```c +int foo(); +int foo(char a); +``` +Back: Conflicting types. The first `foo` declares any `char` passed to it default promotes to an `int`. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +*Why* might the following snippet raise a compilation error? +```c +int foo(); +int foo(double a); +``` +Back: N/A. It likely wouldn't. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +### main + +`main` is a special function serving as the entrypoint to C programs. It can have several different prototypes, but the following two are always possible: + +```c +int main(void); +int main(int argc, char* argv[argc+1]); +``` + +The only two return values guaranteed to work on all platform is `EXIT_SUCCESS` and `EXIT_FAILURE`. Reaching the end of `main` is equivalent to a `reutrn` with value `EXIT_SUCCESS`. + +%%ANKI +Basic +Which function serves as the entrypoint of C programs? +Back: `main` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +How many possible valid prototypes of `main` are available? +Back: Indeterminate. Depends on the system. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +How many "official" prototypes of `main` are available? +Back: Two. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What are the "official" prototypes of `main`? +Back: +```c +int main(void); +int main(int argc, char* argv[argc + 1]); +``` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What are the "official" prototypes of `main`? +Back: +```c +int main(void); +int main(int argc, char* argv[argc + 1]); +``` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). +END%% + +%%ANKI +Basic +What are the only portable values that `main` can return? +Back: `EXIT_SUCCESS` and `EXIT_FAILURE`. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Which library defines `EXIT_SUCCESS`? +Back: `` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Which library defines `EXIT_FAILURE`? +Back: `` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What happens when `main` does not explicitly return a value? +Back: `EXIT_SUCCESS` is implicitly returned. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Cloze +Returning {1:`s`} in {1:`main`} is equivalent to invoking function {2:`exit`} with argument {2:`s`}. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Which library declares the following prototype? +```c +noreturn void exit(int) +``` +Back: `` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What does keyword `_Noreturn` indicate? +Back: The associated callee will never return control back to the caller. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What kind of syntactical construct is `_Noreturn`? +Back: A special keyword. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What kind of syntactical construct is `noreturn`? +Back: A macro. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Cloze +{1:`_Noreturn`} is a {2:keyword} whereas {2:`noreturn`} is a {1:macro}. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Which library is `noreturn` defined in? +Back: `` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Consider the following prototype. What is the value of `argv[0]`? +```c +int main(int argc, char* argv[argc + 1]); +``` +Back: The name of the program invocation. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Consider the following prototype. What is the value of `argv[argc]`? +```c +int main(int argc, char* argv[argc + 1]); +``` +Back: `0` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Consider the following prototype. What is the value of `argv[1]`? +```c +int main(int argc, char* argv[argc + 1]); +``` +Back: `0` if `argc == 1` else the first argument to the program. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Consider the following prototype. What is the minimum value of `argc`? +```c +int main(int argc, char* argv[argc + 1]); +``` +Back: `1` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Consider the following prototype. What is the minimum length of `argv`? +```c +int main(int argc, char* argv[argc + 1]); +``` +Back: `2` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +### Variable-Length Arrays + +Within a function prototype, a parameter can denote a [[c17/types/index#Variable-Length|VLA]] using `[*]` syntax. For example, the following prototypes are all (more or less) equivalent: + +```c +int sum2d(int , int , int a[*][*]); +int sum2d(int n, int , int a[n][*]); +int sum2d(int , int m, int a[*][m]); +int sum2d(int n, int m, int a[n][m]); +int sum2d(int , int , int a[][*]); +int sum2d(int , int , int (*a)[*]); +int sum2d(int , int m, int (*a)[m]); +``` + +%%ANKI +Basic +In what context are VLAs declared with `[*]` actually useful? +Back: Function prototypes. +Reference: https://stackoverflow.com/a/17371914 + +END%% + +%%ANKI +Basic +Maintaining array syntax, rewrite the following without parameter names. +```c +int sum2d(int n, int m, int a[n][m]); +``` +Back: +```c +int sum2d(int, int, int a[*][*]); +``` +Reference: https://stackoverflow.com/a/17371914 + +END%% + +%%ANKI +Basic +*Why* is the first `*` considered redundant in the following? +```c +int sum2d(int, int, int a[*][*]); +``` +Back: Array decay. +Reference: https://stackoverflow.com/a/17371914 + +END%% + ## Precedence Rules Declarations can be read by complying with the precedence rules outlined below: diff --git a/notes/c17/functions.md b/notes/c17/functions.md deleted file mode 100644 index 49288a8..0000000 --- a/notes/c17/functions.md +++ /dev/null @@ -1,666 +0,0 @@ ---- -title: Functions -TARGET DECK: Obsidian::STEM -FILE TAGS: c17::function -tags: - - c17 ---- - -## Overview - -A function `f` without a following opening `(` is converted to a pointer to its start. This is called **function decay**. - -%%ANKI -Basic -What is the effect of function decay? -Back: Evaluation of a function `f` without a following opening `(` is converted to a pointer to its start. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What name is given to the implicit conversion of a function to a pointer? -Back: Function decay. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -According to Gustedt, what C feature explains why are there no "function values"? -Back: Function-to-pointer decay. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Why can't functions directly be made arguments to functions? -Back: Because function arguments decay to pointers. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Cloze -{1:Function pointers} are to {2:`(...)`} whereas {2:pointers} are to {1:`[...]`}. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -In what order are decays, dereferences, address ofs, and calls performed in the following? -```c -f(3); -``` -Back: Decay, call. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -In what order are decays, dereferences, address ofs, and calls performed in the following? -```c -(&f)(3); -``` -Back: Address of, call. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -In what order are decays, dereferences, address ofs, and calls performed in the following? -```c -(*f)(3); -``` -Back: Decay, dereference, decay, call. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -In what order are decays, dereferences, address ofs, and calls performed in the following? -```c -(*&f)(3); -``` -Back: Address of, dereference, decay, call. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -In what order are decays, dereferences, address ofs, and calls performed in the following? -```c -(&*f)(3); -``` -Back: Decay, dereference, address of, call. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Cloze -{1:Pointers} refer to {2:arrays} whereas {2:function pointers} refer to {1:functions}. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -## Prototypes - -There exist two ways for a function [[c17/declarations|declaration]] to use declarators: **parameter type lists** and **identifier type lists**. To make the distinction clear, consider the following ways of defining an `add` function: - -```c -int f(int x, int y) { return x + y; } // Paramter type list -int f(x, y) int x; int y; { return x + y } // Identifier type list -``` - -A function **prototype** is a function declaration that specifies a function signature. There are three important points to make note of: - -* Empty identifier lists are interpreted as "the compiler has not been told what this function's arguments are." -* The standard prohibits declaring functions with a non-empty identifier list. -* Empty parameter lists are not allowed. - -Therefore: - -```c -// Uses an empty identifer list. This declares a function `foo` -// that takes an unknown specification of arguments. -void foo(); -// Uses a non-empty identifier list. Compiler error. -void foo(x, y); -// Uses a non-empty identifier list. Compiler error. -void foo(x, y) int x; int y; -// Uses a non-empty identifier list. Definitions allow this. -void foo(x, y) int x; int y; { } -// Uses a non-empty parameter list. This prototypes a function -// `foo` that takes no arguments. -void foo(void); -// Uses a non-empty parameter list. This prototypes and defines -// a function `foo` that takes no arguments. -void foo(void) {} -``` - -Together these points imply a function prototype *must* use a parameter type list. - -%%ANKI -Basic -Which of prototypes or declarations are more general? -Back: Declarations. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -What two ways are parameters declared in function declarations and definitions? -Back: Identifier type lists and parameter type lists. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -Which of identifier type lists and/or parameter type lists are considered obsolete? -Back: Identifier type lists. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -Define an addition function using identifier type lists. -Back: -```c -int add(x, y) int x; int y; { return x + y; } -``` -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -Define an addition function using parameter type lists. -Back: -```c -int add(int x, int y) { return x + y; } -``` -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -Is the following a prototype or a declaration? -```c -void foo(); -``` -Back: A declaration. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -What compilation error does the following raise? -```c -void foo(); -``` -Back: N/A. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -What compilation error does the following raise? -```c -void foo(x, y); -``` -Back: A function declaration cannot have a non-empty identifier list. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -What compilation error does the following raise? -```c -void foo(x, y) int x; int y; -``` -Back: A function declaration cannot have a non-empty identifier list. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -What compilation error does the following raise? -```c -void foo(x, y) int x; int y; {} -``` -Back: N/A. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -What function prototype is declared in the following? -```c -void foo(x, y) int x; int y; {} -``` -Back: N/A. No prototype has been declared. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -What compilation error does the following raise? -```c -void foo(); - -int main(void) { foo(1); } -``` -Back: N/A. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -Is the following a prototype, declaration, both, or neither? -```c -void f(); -``` -Back: A declaration. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -Is the following a prototype, declaration, both, or neither? -```c -void f(void); -``` -Back: Both. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -What compilation error does the following raise? -```c -void foo(void); -``` -Back: N/A. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -What compilation error does the following raise? -```c -void foo(void); - -int main(void) { foo(1); } -``` -Back: Too many arguments to function call `foo`. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -What function prototype is declared in the following? -```c -void foo(int x, int y); -``` -Back: `void foo(int, int)` -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -What function prototype is declared in the following? -```c -void foo(int x, int y) {} -``` -Back: `void foo(int, int)` -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -How many arguments does the following declaration specify? -```c -void foo(); -``` -Back: Some number unknown to the compiler. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -How many arguments does the following declaration specify? -```c -void foo(void); -``` -Back: Zero. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -Why might the following snippet raise a compilation error? -```c -int foo(); -int foo(int a); -``` -Back: N/A. It likely wouldn't. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -*Why* might the following snippet raise a compilation error? -```c -int foo(); -int foo(float a); -``` -Back: Conflicting types. The first `foo` declares any `float` passed to it default promotes to a `double`. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -*Why* might the following snippet raise a compilation error? -```c -int foo(); -int foo(char a); -``` -Back: Conflicting types. The first `foo` declares any `char` passed to it default promotes to an `int`. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -*Why* might the following snippet raise a compilation error? -```c -int foo(); -int foo(double a); -``` -Back: N/A. It likely wouldn't. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -### main - -`main` is a special function serving as the entrypoint to C programs. It can have several different prototypes, but the following two are always possible: - -```c -int main(void); -int main(int argc, char* argv[argc+1]); -``` - -The only two return values guaranteed to work on all platform is `EXIT_SUCCESS` and `EXIT_FAILURE`. Reaching the end of `main` is equivalent to a `reutrn` with value `EXIT_SUCCESS`. - -%%ANKI -Basic -Which function serves as the entrypoint of C programs? -Back: `main` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -How many possible valid prototypes of `main` are available? -Back: Indeterminate. Depends on the system. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -How many "official" prototypes of `main` are available? -Back: Two. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What are the "official" prototypes of `main`? -Back: -```c -int main(void); -int main(int argc, char* argv[argc + 1]); -``` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What are the "official" prototypes of `main`? -Back: -```c -int main(void); -int main(int argc, char* argv[argc + 1]); -``` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). -END%% - -%%ANKI -Basic -What are the only portable values that `main` can return? -Back: `EXIT_SUCCESS` and `EXIT_FAILURE`. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Which library defines `EXIT_SUCCESS`? -Back: `` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Which library defines `EXIT_FAILURE`? -Back: `` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What happens when `main` does not explicitly return a value? -Back: `EXIT_SUCCESS` is implicitly returned. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Cloze -Returning {1:`s`} in {1:`main`} is equivalent to invoking function {2:`exit`} with argument {2:`s`}. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Which library declares the following prototype? -```c -noreturn void exit(int) -``` -Back: `` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What does keyword `_Noreturn` indicate? -Back: The associated callee will never return control back to the caller. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What kind of syntactical construct is `_Noreturn`? -Back: A special keyword. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What kind of syntactical construct is `noreturn`? -Back: A macro. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Cloze -{1:`_Noreturn`} is a {2:keyword} whereas {2:`noreturn`} is a {1:macro}. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Which library is `noreturn` defined in? -Back: `` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Consider the following prototype. What is the value of `argv[0]`? -```c -int main(int argc, char* argv[argc + 1]); -``` -Back: The name of the program invocation. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Consider the following prototype. What is the value of `argv[argc]`? -```c -int main(int argc, char* argv[argc + 1]); -``` -Back: `0` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Consider the following prototype. What is the value of `argv[1]`? -```c -int main(int argc, char* argv[argc + 1]); -``` -Back: `0` if `argc == 1` else the first argument to the program. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Consider the following prototype. What is the minimum value of `argc`? -```c -int main(int argc, char* argv[argc + 1]); -``` -Back: `1` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Consider the following prototype. What is the minimum length of `argv`? -```c -int main(int argc, char* argv[argc + 1]); -``` -Back: `2` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -### Variable-Length Arrays - -Within a function prototype, a parameter can denote a [[derived#Variable-Length|VLA]] using `[*]` syntax. For example, the following prototypes are all (more or less) equivalent: - -```c -int sum2d(int , int , int a[*][*]); -int sum2d(int n, int , int a[n][*]); -int sum2d(int , int m, int a[*][m]); -int sum2d(int n, int m, int a[n][m]); -int sum2d(int , int , int a[][*]); -int sum2d(int , int , int (*a)[*]); -int sum2d(int , int m, int (*a)[m]); -``` - -%%ANKI -Basic -In what context are VLAs declared with `[*]` actually useful? -Back: Function prototypes. -Reference: https://stackoverflow.com/a/17371914 - -END%% - -%%ANKI -Basic -Maintaining array syntax, rewrite the following without parameter names. -```c -int sum2d(int n, int m, int a[n][m]); -``` -Back: -```c -int sum2d(int, int, int a[*][*]); -``` -Reference: https://stackoverflow.com/a/17371914 - -END%% - -%%ANKI -Basic -*Why* is the first `*` considered redundant in the following? -```c -int sum2d(int, int, int a[*][*]); -``` -Back: Array decay. -Reference: https://stackoverflow.com/a/17371914 - -END%% - -## Bibliography - -* “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). -* Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). \ No newline at end of file diff --git a/notes/c17/pointers.md b/notes/c17/pointers.md index 798381b..84d30ac 100644 --- a/notes/c17/pointers.md +++ b/notes/c17/pointers.md @@ -562,7 +562,7 @@ END%% ## Aliasing -Accessing the same object through different pointers is called **aliasing**. With the exclusion of [[simple#Character Types|character types]], only pointers of the same base type may alias. +Accessing the same object through different pointers is called **aliasing**. With the exclusion of [[types/index#Character Types|character types]], only pointers of the same base type may alias. %%ANKI Basic diff --git a/notes/c17/storage.md b/notes/c17/storage.md index cfdf6f5..8190a24 100644 --- a/notes/c17/storage.md +++ b/notes/c17/storage.md @@ -357,7 +357,7 @@ Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co END%% -For these objects that do not have a [[derived#Variable-Length|VLA]] type, its lifetime extends from entry into its enclosing block until execution of that block ends. Its initial value is indeterminate. If initialization is specified, it is performed each time the declaration or compound literal is reached in the execution of the block. +For these objects that do not have a [[c17/types/index#Variable-Length|VLA]] type, its lifetime extends from entry into its enclosing block until execution of that block ends. Its initial value is indeterminate. If initialization is specified, it is performed each time the declaration or compound literal is reached in the execution of the block. For these objects that do have a VLA type, its lifetime extends from the declaration of the object until execution of the program leaves the scope of the declaration. @@ -675,7 +675,7 @@ END%% In contrast to `auto`, there also exists a `register` [[#Storage-Class Specifiers|storage-class specifier]]. The `&` operator is not allowed for variables declared with `register` meaning variables declared with `register` can't [[pointers#Aliasing|alias]]. -Because of [[derived#Arrays|array-to-pointer decay]], arrays with storage-class `register` are useless. +Because of [[types/index#Arrays|array-to-pointer decay]], arrays with storage-class `register` are useless. %%ANKI Basic @@ -984,7 +984,6 @@ END%% Storage-class specifiers are used to control an object's storage duration and an identifier's linkage. There are six storage-class specifiers: * `typedef` - * Discussed in [[typedefs]]. * Is called a "storage-class specifier" for syntactic convenience only. * `extern` * Discussed in [[linkage]] and [[#Static]]. @@ -1007,22 +1006,6 @@ Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open END%% -%%ANKI -Basic -Which storage-class specifier isn't *really* a storage-class specifier? -Back: `typedef` -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -Syntactically, what makes `typedef` a special storage-class specifier? -Back: It is labeled a "storage-class specifier" for syntactic convenience. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - %%ANKI Basic A `_Thread_local` storage-class specifier can appear next to what other specifier(s)? @@ -1079,7 +1062,171 @@ Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open END%% +### typedefs + +The `` header defines a few standard `typedef`s: + +* `ptrdiff_t`: the signed integer type of the result of subtracting two pointers. +* `size_t`: the unsigned integer type of the result of the `sizeof` operator. + +The standard often uses `typedef`s ending with `_t`. + +%%ANKI +Basic +Which storage-class specifier isn't *really* a storage-class specifier? +Back: `typedef` +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +Syntactically, what makes `typedef` a special storage-class specifier? +Back: It is labeled a "storage-class specifier" for syntactic convenience. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +What is the type of `x` in the following? +```c +#define int_ptr int * +int_ptr x, y; +``` +Back: `int *` +Reference: Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994. + +END%% + +%%ANKI +Basic +What is the type of `y` in the following? +```c +#define int_ptr int * +int_ptr x, y; +``` +Back: `int` +Reference: Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994. + +END%% + +%%ANKI +Basic +What is the type of `x` in the following? +```c +typedef int_ptr int * +int_ptr x, y; +``` +Back: `int *` +Reference: Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994. + +END%% + +%%ANKI +Basic +What is the type of `y` in the following? +```c +typedef int_ptr int * +int_ptr x, y; +``` +Back: `int *` +Reference: Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994. + +END%% + +%%ANKI +Basic +What header defines `size_t`? +Back: `` +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +What header defines `ptrdiff_t`? +Back: `` +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +`ptrdiff_t` is used as the type of what result? +Back: Subtracting two pointers. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +`size_t` is used as the type of what result? +Back: The `sizeof` operation. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +Is `ptrdiff_t` signed or unsigned? +Back: Signed. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +When can two pointers be subtracted? +Back: Only if both refer to elements of the same array object. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the result of `p - q` in the following? +```c +double A[4] = { 0.0, 1.0, 2.0, -3.0 }; +double* p = &A[1]; +double* q = &A[3]; +``` +Back: `-2` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the result of `p - q` in the following? +```c +double A[4] = { 0.0, 1.0, 2.0, -3.0 }; +double* p = &A[3]; +double* q = &A[1]; +``` +Back: `2` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Is `size_t` signed or unsigned? +Back: Unsigned. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +The C standard typically suffixes `typedef`s with what? +Back: `_t` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + ## Bibliography * “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). -* Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). \ No newline at end of file +* Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). +* Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994. \ No newline at end of file diff --git a/notes/c17/typedefs.md b/notes/c17/typedefs.md deleted file mode 100644 index ba302d1..0000000 --- a/notes/c17/typedefs.md +++ /dev/null @@ -1,160 +0,0 @@ ---- -title: Typedefs -TARGET DECK: Obsidian::STEM -FILE TAGS: c17::type -tags: - - c17 ---- - -## Overview - -The `` header defines a few standard `typedef`s: - -* `ptrdiff_t`: the signed integer type of the result of subtracting two pointers. -* `size_t`: the unsigned integer type of the result of the `sizeof` operator. - -The standard often uses `typedef`s ending with `_t`. - -%%ANKI -Basic -What is the type of `x` in the following? -```c -#define int_ptr int * -int_ptr x, y; -``` -Back: `int *` -Reference: Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994. - -END%% - -%%ANKI -Basic -What is the type of `y` in the following? -```c -#define int_ptr int * -int_ptr x, y; -``` -Back: `int` -Reference: Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994. - -END%% - -%%ANKI -Basic -What is the type of `x` in the following? -```c -typedef int_ptr int * -int_ptr x, y; -``` -Back: `int *` -Reference: Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994. - -END%% - -%%ANKI -Basic -What is the type of `y` in the following? -```c -typedef int_ptr int * -int_ptr x, y; -``` -Back: `int *` -Reference: Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994. - -END%% - -%%ANKI -Basic -What header defines `size_t`? -Back: `` -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -What header defines `ptrdiff_t`? -Back: `` -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -`ptrdiff_t` is used as the type of what result? -Back: Subtracting two pointers. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -`size_t` is used as the type of what result? -Back: The `sizeof` operation. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -Is `ptrdiff_t` signed or unsigned? -Back: Signed. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -When can two pointers be subtracted? -Back: Only if both refer to elements of the same array object. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What is the result of `p - q` in the following? -```c -double A[4] = { 0.0, 1.0, 2.0, -3.0 }; -double* p = &A[1]; -double* q = &A[3]; -``` -Back: `-2` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What is the result of `p - q` in the following? -```c -double A[4] = { 0.0, 1.0, 2.0, -3.0 }; -double* p = &A[3]; -double* q = &A[1]; -``` -Back: `2` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Is `size_t` signed or unsigned? -Back: Unsigned. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -The C standard typically suffixes `typedef`s with what? -Back: `_t` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -## Bibliography - -* “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). -* Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). -* Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994. \ No newline at end of file diff --git a/notes/c17/types/compatible.md b/notes/c17/types/compatible.md new file mode 100644 index 0000000..7937f98 --- /dev/null +++ b/notes/c17/types/compatible.md @@ -0,0 +1,205 @@ +--- +title: Compatible Types +TARGET DECK: Obsidian::STEM +FILE TAGS: c17::type +tags: + - c17 +--- + +## Overview + +Two declarations with different types may still refer to the same object or function if they have **compatible types**. + +A **composite type** is constructed from two compatible types. It is the type that is compatible with both of the original types, including every available array size and every available parameter list from the original types. + +%%ANKI +Basic +What are compatible types? +Back: Types that can be used to refer to the same object or function. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Cloze +A {composite} type is constructed from two {compatible} types. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +## Simple Types + +Disregarding enumerations, two different simple types are never compatible. For two simple types to be compatible, they must use the same qualifiers and their unqualified base types must be the same. + +%%ANKI +Basic +Explain why `int` and `signed int` are compatible types or not. +Back: Compatible. `int` is an alias for `signed int`. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +Explain why `int` and `unsigned int` are compatible types or not. +Back: Incompatible. They have different signedness. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +Explain why `unsigned int` and `signed int` are compatible types or not. +Back: Incompatible. They have different signedness. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +Explain why `const int` and `int` are compatible types or not. +Back: Incompatible. Type qualifiers must match. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +Explain why `const int` and `const` are compatible types or not. +Back: Compatbile. `const` without a type specifier assumes a `const int` type. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +Explain why `const int` and `volatile int` are compatible types or not. +Back: Incompatible. Type qualifiers must match. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +Explain why `float` and `double` are compatible types or not. +Back: Incompatible. Two different simple types are never compatible. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +### Enumerations + +An enumeration type must be compatible with a `char`, signed integer type, or unsigned integer type. The choice of type is implementation-defined, but shall be capable of representing the values of all the members of the enumeration. + +%%ANKI +Basic +An enumerated type must be compatible with what three possibilities? +Back: A `char`, a signed integer type, or an unsigned integer type. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +What is the type given to an enumerated type? +Back: N/A. This is implementation-defined. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +What size restriction is enforced on the implementation-defined enumerated type? +Back: It must be capable of representing the values of all the members of the `enum`. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +What kind of integer expressions are enumeration constants limited to? +Back: Integer constant expressions with value representable as an `int`. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +Besides being an ICE, what other condition is expected on enumeration constants? +Back: The ICE evaluates to an integer that fits within an `int`. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +If two enumeration types are declared in separate translation units, they are compatible if they are declared with the same tag (or lack thereof) and there exists a one-to-one correspondence between their members such that each pair of corresponding members are declared with compatible types. Additionally, corresponding members must have the same values. + +%%ANKI +Basic +Explain why the following `enums`, defined in different TUs, have compatible types or not. +```c +enum x { a = 1 }; + +enum x { a = 1 }; +``` +Back: Compatible. They are exactly the same. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +Explain why the following `enums`, defined in different TUs, have compatible types or not. +```c +enum x { a = 1 }; + +enum x { a = 2 }; +``` +Back: Incompatible. Corresponding members must have the same values. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +Explain why the following `enums`, defined in different TUs, have compatible types or not. +```c +enum x { a, b }; + +enum x { b, a }; +``` +Back: Incompatible. Corresponding members must have the same values. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +Explain why the following `enums`, defined in different TUs, have compatible types or not. +```c +enum x { a = 0, b }; + +enum x { b = 1, a = 0 }; +``` +Back: Compatible. Corresponding members have the same values. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +Explain why the following `enums`, defined in different TUs, have compatible types or not. +```c +enum x { a, b }; + +enum y { a, b }; +``` +Back: Incompatible. Tags `x` and `y` do not match. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +## Derived Types + +## Bibliography + +* “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). \ No newline at end of file diff --git a/notes/c17/types/conversions.md b/notes/c17/types/conversions.md new file mode 100644 index 0000000..44169f1 --- /dev/null +++ b/notes/c17/types/conversions.md @@ -0,0 +1,314 @@ +--- +title: Conversions +TARGET DECK: Obsidian::STEM +FILE TAGS: c17::type +tags: + - c17 +--- + +## Overview + +> Several operators convert operand values from one type to another automatically. + +The above quote refers to **implicit conversions**. Alternatively, we can use cast operations to perform **explicit conversions**. + +## Usual Arithmetic Conversions + +As a general rule, the result of an operation has the type of the operand with wider range. The usual arithmetic conversions behave according to the following pattern: + +1. Determine a **common real type** for the operands and result. +2. Convert each operand, without change of type domain, to a type with real type matching the common real type. +3. Unless explicitly stated otherwise, the common real type is the corresponding real type of the result, whose type domain is that of the operands if they are the same and complex otherwise. + +Common real types are prioritized in the following order: + +1. `long double` +2. `double` +3. `float` +4. If both operands have the same signedness, the higher ranked type. +5. If the unsigned operand has rank $\geq$ than that of the other, the unsigned type. +6. If the signed operand's includes that of the other, the signed type. +7. The unsigned integer type corresponding to that of the signed integer type. + +%%ANKI +Basic +What is the common real type of `a` and `b`? +```c +long double a; +long double complex b; +``` +Back: `long double` +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +What is the common real type of `a` and `b`? +```c +long double a; +long complex b; +``` +Back: `long double` +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +What is the type domain of `a + b`? +```c +long double a; +long complex b; +``` +Back: Complex. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +What is the common real type of `a` and `b`? +```c +double complex a; +long b; +``` +Back: `double` +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +What is the type domain of `a + b`? +```c +long double a; +long complex b; +``` +Back: Complex. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). +END%% + +%%ANKI +Basic +What is the common real type of `a` and `b`? +```c +float a; +double b; +``` +Back: `double` +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +What is the common real type of `a` and `b`? +```c +long long a; +float b; +``` +Back: `float` +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +What is the type domain of `a + b`? +```c +long long a; +float b; +``` +Back: Real. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +*Why* is the common real type of `a` and `b` equal to `float`? +```c +long long a; +float b; +``` +Back: Because floating-point types have priority over integer types. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +What is the type domain of `a + b`? +```c +long long a; +long long b; +``` +Back: Real. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +What is the common real type of `a` and `b`? +```c +long long a; +long long b; +``` +Back: `long long` +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +What is the common real type of `a` and `b`? +```c +unsigned int a; +signed short b; +``` +Back: `unsigned int` +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +What is the common real type of `a` and `b`? +```c +unsigned a; +signed int b; +``` +Back: `unsigned` +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +*Why* is the common real type of `a` and `b` equal to `unsigned`? +```c +unsigned a; +signed int b; +``` +Back: Because `unsigned` has rank $\geq$ that of `signed int`. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +What is the type domain of `a + b`? +```c +unsigned a; +signed int b; +``` +Back: Real. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +What is the type domain of `a + b`? +```c +unsigned short a; +signed int b; +``` +Back: Real. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +What is the common real type of `a + b`? +```c +unsigned short a; +signed int b; +``` +Back: Indeterminate. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +*Why* is the common real type of `a + b` indeterminate? +```c +unsigned short a; +int b; +``` +Back: Because the rank of `unsigned short` is $<$ that of `int`. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +What *might* the common real type of `a + b` be? +```c +unsigned short a; +signed int b; +``` +Back: `signed int` or `unsigned int`. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +When is the common real type of `a + b` equal to `signed int`? +```c +unsigned short a; +signed int b; +``` +Back: When `signed int` can represent all the values of `unsigned short`. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +When is the common real type of `a + b` equal to `unsigned int`? +```c +unsigned short a; +signed int b; +``` +Back: When `signed int` cannot represent all the values of `unsigned short`. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +When is the common real type of `a + b` equal to `unsigned short`? +```c +unsigned short a; +signed int b; +``` +Back: N/A. It never is. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +Suppose `a` and `b` has signed and unsigned types respectively. When is `a + b` signed? +Back: When `b`'s type has lower rank and the range of `b` is included in the range of `a`. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +Suppose `a` and `b` have signed and unsigned types respectively. When is `a + b` unsigned? +Back: When `b`'s type has higher rank or the range of `a` cannot fit the range of `b`. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +## Bibliography + +* “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). \ No newline at end of file diff --git a/notes/c17/types/derived.md b/notes/c17/types/derived.md deleted file mode 100644 index 0457557..0000000 --- a/notes/c17/types/derived.md +++ /dev/null @@ -1,879 +0,0 @@ ---- -title: Derived Types -TARGET DECK: Obsidian::STEM -FILE TAGS: c17::type -tags: - - c17 ---- - -## Overview - -A type is said to be **derived** if they are defined relative to other types. - -%%ANKI -Basic -What is a derived type? -Back: A type defined relative to other types. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -## Aggregate Data Types - -The **aggregate data types** are so called because they combine multiple instances of one or several other data types. - -%%ANKI -Basic -What are the two aggregate data types? -Back: Arrays and structures. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Why are the aggregate data types named the way they are? -Back: They combine multiple instances of one or several other data types. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -### Arrays - -An array is a contiguous sequence of objects. An array is either a **fixed-length array** or a **variable-length array**. - -%%ANKI -Cloze -{Arrays} combine items that all have the same {base type}. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What is the base type of the following array? -```c -double a[4]; -``` -Back: `double` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What is the base type of the following array? -```c -double a[4][4]; -``` -Back: `double[4]` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -How are parentheses introduced to the following declaration without affecting meaning? -```c -double a[4][4]; -``` -Back: -```c -double (a[4])[4]; -``` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Consider the following. How many objects make up `a[0]`? -```c -double a[M][N]; -``` -Back: `N` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Consider the following. How many objects make up `a[M - 1]`? -```c -double a[M][N]; -``` -Back: `N` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Consider the following. How many objects make up `a[N - 1]`? -```c -double a[M][N]; -``` -Back: Indeterminate. This may not be a valid array access. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Consider the following where `N < M`. How many objects make up `a[N - 1]`? -```c -double a[M][N]; -``` -Back: `N` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Consider the following where `M < N`. How many objects make up `a[N - 1]`? -```c -double a[M][N]; -``` -Back: N/A. Invalid array access. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Cloze -Array `a[M][N]` is a sequence of {1:`M`} objects each containing {1:`N`} objects. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Arrays fall under what two categories? -Back: Fixed-length and variable-length. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Replace `EXPR` in the following with an expression to compute the length of `A`. -```c -int A[N]; -size_t len = EXPR; -``` -Back: `(sizeof A) / (sizeof A[0])` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What two syntactic forms does the `sizeof` operator come in? -Back: With and without parentheses. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -When can the `sizeof` operator elide parentheses? -Back: When operating on an object. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -When does the `sizeof` operator *require* parentheses? -Back: When operating on a type. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Apply the possible syntactic forms of the `sizeof` operator to object `a`. -Back: -```c -sizeof a -sizeof(a) -``` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Apply the possible syntactic forms of the `sizeof` operator to type `int`. -Back: -```c -sizeof(int) -``` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Suppose I pass the following array to a function. Which dimension is lost? -```c -double a[M][N][P]; -``` -Back: `M` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Why shouldn't you use the `sizeof` operator on array parameters to functions? -Back: The innermost dimension of an array parameter to a function is lost. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -How is the following array parameter rewritten using pointer syntax? -```c -void foo(int a[const]) {} -``` -Back: -```c -void foo(int *const a) {} -``` -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -How is the following array parameter rewritten using pointer syntax? -```c -void foo(const int a[]) {} -``` -Back: -```c -void foo(const int *a) {} -``` -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -How is the following pointer parameter rewritten using array syntax? -```c -void foo(int *restrict a) {} -``` -Back: -```c -void foo(int a[restrict]) {} -``` -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -How is the following pointer parameter rewritten using array syntax? -```c -void foo(volatile int *a) {} -``` -Back: -```c -void foo(volatile int a[]) {} -``` -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -What does the `3` keyword guarantee to the compiler? -```c -void bar(int a[3]); -``` -Back: N/A. It is ignored. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -*Why* might you see e.g. `3` like in the following prototype? -```c -void bar(int a[3]); -``` -Back: It serves as documentation. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -In the following, what does the `static` keyword guarantee to the compiler? -```c -void bar(int a[static 3]); -``` -Back: The minimum number of elements for the compiler to assume `a` contains. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -*Why* does the following produce a compilation error? -```c -void foo(int a[3]); - -int main() { - int a[] = { 1, 2 }; - foo(a); -} -``` -Back: N/A. It doesn't. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -*Why* does the following produce a compilation error? -```c -void foo(int a[static 3]); - -int main() { - int a[] = { 1, 2 }; - foo(a); -} -``` -Back: Because the argument to `foo` does not have at least `3` elements. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -*Why* does the following produce a compilation error? -```c -void foo(int a[static 3]); - -int main() { - int a[] = { 1, 2, 3, 4 }; - foo(a); -} -``` -Back: N/A. It doesn't. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -*Why* does the following produce a compilation error? -```c -void foo(int a[3]); - -int main() { - foo(0); -} -``` -Back: N/A. It doesn't. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -*Why* does the following produce a compilation error? -```c -void foo(int a[static 1]); - -int main() { - foo(0); -} -``` -Back: Because `static` indicates a valid pointer must be passed to `foo`. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -Add initializer with first element set to `1` using designated initialization. -```c -int example[3]; -``` -Back: -```c -int example[3] = { [0] = 1 }; -``` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Add initializer with first element set to `1` *without* using designated initialization. -```c -int example[3]; -``` -Back: -```c -int example[3] = { 1 }; -``` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What is the value of `example[1]` in the following? -```c -int example[2] = { [0] = 1 }; -``` -Back: `0` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Are arrays passed by reference or value? -Back: Reference. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Is the following an FLA or VLA? -```c -double a[4]; -``` -Back: FLA. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Is the following an FLA or VLA? -```c -double a[]; -``` -Back: N/A. This is invalid syntax. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Is the following an FLA or VLA? -```c -double a[] = { 0 }; -``` -Back: FLA. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Is the following an FLA or VLA? -```c -int n = 2; -double a[n]; -``` -Back: VLA. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Is the following an FLA or VLA? -```c -#define n 2 -double a[n]; -``` -Back: FLA. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Is the following an FLA or VLA? -```c -enum { n = 2 }; -double a[n]; -``` -Back: FLA. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Is the following an FLA or VLA? -```c -int n = 2; -double a[n] = {0}; -``` -Back: N/A. Variable-sized objects may not be initialized. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -Evaluation of an array `A` returns `&A[0]`, i.e. a [[#Pointers|pointer]] to the first array element. This is called **array decay**. - -%%ANKI -Basic -What is the effect of array decay? -Back: Evaluation of an array `A` returns `&A[0]`. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What name is given to the implicit conversion of an array to a pointer? -Back: Array decay. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -According to Gustedt, what C feature explains why are there no "array values"? -Back: Array-to-pointer decay. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Why can't arrays directly be made arguments to functions? -Back: Because array arguments decay to pointers. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Cloze -In a function declaration, any array parameter rewrites to {a pointer}. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -#### Fixed-Length - -A fixed-length array (FLA) has a predetermined size. Their stack allocations can be computed at compilation time. - -%%ANKI -Basic -What is FLA an acronym for? -Back: **F**ixed-**l**ength **a**rray. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -What two ways can the length of an FLA be specified? -Back: By an ICE or an initializer. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -For the following to be an FLA, what must `N` be? -```c -double a[N]; -``` -Back: An ICE. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -*Why* must FLAs be declared within a function? -Back: N/A. They can exist elsewhere. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. -Tags: x86-64 - -END%% - -#### Variable-Length - -A variable-length array (VLA) has its size determined at runtime. Their stack allocations must be determined with respect to other registers available to the frame. - -%%ANKI -Basic -What is VLA an acronym for? -Back: **V**ariable-**l**ength **a**rray. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -Where *must* a VLA be declared? -Back: Within a function. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -*Why* do variable-length arrays require use of a frame pointer? -Back: Offsets depend on how much space must be allocated on the stack. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. -Tags: x86-64 - -END%% - -%%ANKI -Basic -*Why* must VLAs be declared within a function? -Back: Their implementation relies on frame pointers. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. -Tags: x86-64 - -END%% - -### Structures - -A `struct` is a grouping of data together. It has the following general form: - -```c -struct optional_tag { - type_1 ident1; - ... - type_N identN; -} optional_var1 ... optional_varM; -``` - -%%ANKI -Cloze -{Structures} combine items that may have different {base types}. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What is the underlined portion of the following declaration called? -```c -struct ___ { ... }; -``` -Back: The tag. -Reference: Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994. - -END%% - -%%ANKI -Basic -What distinguishes the `X`'s from one another in the following? -```c -struct X { ... } X; -``` -Back: The first `X` is a tag whereas the second is a variable with type `struct X`. -Reference: Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994. - -END%% - -%%ANKI -Basic -What distinguishes the `X`'s from one another in the following? -```c -typedef struct X { ... } X; -``` -Back: The first `X` is a tag whereas the second is an alias for type `struct X`. -Reference: Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994. - -END%% - -%%ANKI -Basic -Define an object with `fieldA` set to `1` using designated initialization. -```c -struct example { int fieldA; }; -``` -Back: -```c -struct example test = { .fieldA = 1 }; -``` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Define an object with `fieldA` set to `1` *without* using designated initialization. -```c -struct example { int fieldA; }; -``` -Back: -```c -struct example test = { 1 }; -``` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What is the value of `test.fieldB` in the following? -```c -struct example { int fieldA; int fieldB; }; -struct example test = { .fieldA = 1 }; -``` -Back: `0` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Are `struct`s well-defined with respect to `=` (i.e. assignment)? -Back: Yes. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Are `struct`s well-defined with respect to `==` (i.e. equality)? -Back: No. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Are `struct`s well-defined with respect to `!=` (i.e. inequality)? -Back: No. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Generally speaking, what are valid member types of a `struct`? -Back: All object types except VLAs. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Generally speaking, what object type is not a valid `struct` member type? -Back: VLAs. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Declare a variable `var` of the nested `struct` with member `c` set to `1`. -```c -struct A { - struct B { int c; }; -}; -``` -Back: `struct B var = { 1 };` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What is the visibility of `struct B` with respect to `struct A`? -```c -struct A { - struct B { int c; }; -}; -``` -Back: They have the same visibility. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What does it mean for `struct A` and `struct B` to have the same visibility? -```c -struct A { - struct B { int c; }; -}; -``` -Back: Nesting `struct`s does not introduce any notion of scope. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -## Unions - -A `union` is a grouping of data together but with overlaid storage. It has the following general form: - -```c -union optional_tag { - type1 ident1; - ... - typeN identN; -} optional_var1 ... optional_varN; -``` - -%%ANKI -Basic -What is the underlined portion of the following declaration called? -```c -union ___ { ... }; -``` -Back: The tag. -Reference: Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994. - -END%% - -%%ANKI -Basic -What distinguishes the `X`'s from one another in the following? -```c -union X { ... } X; -``` -Back: The first `X` is a tag whereas the second is a variable with type `union X`. -Reference: Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994. - -END%% - -%%ANKI -Basic -What distinguishes the `X`'s from one another in the following? -```c -typedef union X { ... } X; -``` -Back: The first `X` is a tag whereas the second is an alias for type `union X`. -Reference: Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994. - -END%% - -## Pointers - -Pointers are another type of derived type. They are listed in more detail [[pointers|here]]. - -## Functions - -Functions are another type of derived type. They are listed in more detail [[c17/functions|here]]. - -## Bibliography - -* Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. -* “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). -* Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). -* Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994. diff --git a/notes/c17/types/index.md b/notes/c17/types/index.md index 58f4657..a314da1 100644 --- a/notes/c17/types/index.md +++ b/notes/c17/types/index.md @@ -8,7 +8,7 @@ tags: ## Overview -Types in C are categorized corresponding to a hierarchy with a foundation comprising of the [[simple|simple types]]. These types are then grouped and further categorized until we reach the top of the hierarchy consisting of all types. +Types in C are categorized corresponding to a hierarchy with a foundation comprising of the [[#Simple Types|simple types]]. These types are then grouped and further categorized until we reach the top of the hierarchy consisting of all types. %%ANKI Basic @@ -173,6 +173,1438 @@ Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open END%% +## Simple Types + +The bottom of the type hierarchy consists of **simple types**. This comprises the primitive types that all other types are either based off of or derived from. + +| Signed | Unsigned | 32-bit | 64-bit | +| ------------- | -------------------- | ------ | ------ | +| - | `bool` | 1 | 1 | +| `signed char` | `unsigned char` | 1 | 1 | +| `short` | `unsigned short` | 2 | 2 | +| `int` | `unsigned` | 4 | 4 | +| `long` | `unsigned long` | 4 | 8 | +| `long long` | `unsigned long long` | 8 | 8 | + +| Real | Complex | 32-bit | 64-bit | +| ------------- | --------------------- | ------ | ------ | +| `float` | - | 4 | 4 | +| `double` | - | 8 | 8 | +| `long double` | - | - | - | +| - | `float complex` | 4 | 4 | +| - | `double complex` | 8 | 8 | +| - | `long double complex` | - | - | + +%%ANKI +Cloze +The {``} header file contains {fixed width data integral types}. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +The integer types are grouped into what two classes? +Back: The signed and unsigned integer types. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +The floating point types are grouped into what two classes? +Back: The real and complex floating point types. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +Which header file contains `INT32_MAX`? +Back: `` +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +What two variants does a C integral type declaration have? +Back: Signed and unsigned. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +What does it mean for an integer to be "signed"? +Back: It can represent negative, zero, and positive values. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +What does it mean for an integer to be "unsigned"? +Back: It can only represent nonnegative values. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +How large is a word? +Back: This is a machine-dependent value. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +What word sizes are typical nowadays? +Back: 32- and 64-bit word sizes. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Cloze +`char` *typically* represents {1} byte(s) on a 64-bit platform. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Cloze +`short` *typically* represents {2} byte(s) on a 64-bit platform. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Cloze +`int` *typically* represents {4} bytes(s) on a 64-bit platform. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Cloze +`unsigned` *typically* represents {4} bytes(s) on a 64-bit platform. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Cloze +`long` *typically* represents {8} bytes(s) on a 64-bit platform. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Cloze +`long long` *typically* represents {8} bytes(s) on a 64-bit platform. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +What distinguishes `long` from `long long`? +Back: `long long`s are guaranteed to be at least 64-bit wide. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +*Why* is there both a `long` and `long long`? +Back: `long long`s are at least 64-bit wide, even on 32-bit platforms. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Cloze +`char *` *typically* represents {8} bytes(s) on a 64-bit platform. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Cloze +`float` *typically* represents {4} bytes(s) on a 64-bit platform. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Cloze +`double` *typically* represents {8} bytes(s) on a 64-bit platform. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +Is declaration `int` signed or unsigned? +Back: Signed. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +How is declaration `unsigned` written more precisely? +Back: `unsigned int`. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +Is declaration `long` signed or unsigned? +Back: Signed. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Cloze +{1:`float`} has {2:4} byte width whereas {2:`double`} has {1:8} byte width. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Cloze +The C standard defines {lower} bounds on numeric ranges of data types. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Cloze +The C standard does *not* define {1:upper} bounds on numeric ranges of data types (except for {1:fixed-size} types). +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +### Integers + +**Narrow types** cannot be used directly in arithmetic. Instead they are first promoted to a wider type. On almost every system, this promotion will be to a `signed int` of the same value, regardless of the signedness of the narrow type itself. + +%%ANKI +Basic +Why are narrow types named the way they are? +Back: They are considered too small to be used directly in arithmetic expressions. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Signed narrow types are usually promoted to what larger type? +Back: A `signed int`. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Unsigned narrow types found are usually promoted to what larger type? +Back: A `signed int`. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +#### Character Types + +The three types `char`, `signed char`, and `unsigned char` are collectively called the **character types**. The implementation defines `char` to have the same range, representation, and behavior as either `signed char` or `unsigned char`, but is considered incompatible with both. That is, it is a distinct type in the eyes of the type system. + +%%ANKI +Basic +Is declaration `char` signed or unsigned? +Back: This is implementation-dependent. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +What are the character types? +Back: `char`, `signed char`, and `unsigned char`. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +`char` has the same range, representation, and behavior as what other type? +Back: Either `signed char` or `unsigned char`. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +How do you check if `char` has the same representation as a `signed char` or `unsigned char`? +Back: Check if `CHAR_MIN` has value `SCHAR_MIN` or `0` respectively. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +Which header defines `CHAR_MIN`? +Back: `` +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +Which character type should be used for strings? +Back: `char` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What does it mean for `char` to be incompatible with other character types? +Back: The type system considers it to be a distinct type. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What does `sizeof(char)` evaluate to? +Back: `1` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +#### Unsigned Integers + +| Name | Narrow | Rank | Minimum Width | +| -------------------- | ------ | ---- | ------------- | +| `bool` | Yes | 0 | 1 | +| `char` (maybe) | Yes | 1 | - | +| `unsigned char` | Yes | 1 | 8 | +| `unsighed short` | Yes | 2 | 16 | +| `unsigned int` | No | 3 | 16 | +| `unsigned long` | No | 4 | 32 | +| `unsigned long long` | No | 5 | 64 | + +%%ANKI +Basic +Which basic unsigned type has the smallest rank? +Back: `bool` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Which unsigned type next succeeds `bool` in rank? +Back: `unsigned char` and (maybe) `char`. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Which unsigned type next succeeds `unsigned char` in rank? +Back: `unsigned short` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Which unsigned type next succeeds `unsigned short` in rank? +Back: `unsigned int` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Which unsigned type next succeeds `unsigned int` in rank? +Back: `unsigned long` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Which unsigned type next succeeds `unsigned long` in rank? +Back: `unsigned long long` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Which unsigned type next succeeds `unsigned long long` in rank? +Back: N/A. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Which unsigned narrow type has the highest rank? +Back: `unsigned short` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Which unsigned non-narrow type has the smallest rank? +Back: `unsigned int` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the minimum width of a `bool`? +Back: $1$ bit. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the minimum precision of a `bool`? +Back: $1$ bit. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the minimum width of an `unsigned char`? +Back: $8$ bits. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the minimum precision of an `unsigned char`? +Back: $8$ bits. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the maximum precision of an `unsigned char`? +Back: N/A. C does not define maximum precisions. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the minimum width of an `unsigned short`? +Back: $16$ bits. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the underlying binary representation used to encode an `unsigned short`? +Back: Unsigned encoding. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the minimum precision of an `unsigned short`? +Back: $16$ bits. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the minimum width of an `unsigned int`? +Back: $16$ bits. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the minimum precision of an `unsigned int`? +Back: $16$ bits. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the minimum width of an `unsigned long`? +Back: $32$ bits. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the underlying binary representation used to encode an `unsigned long`? +Back: Unsigned encoding. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the minimum precision of an `unsigned long`? +Back: $32$ bits. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the minimum width of an `unsigned long`? +Back: $32$ bits. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). +END%% + +%%ANKI +Basic +What is the minimum width of an `unsigned long long`? +Back: $64$ bits. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the minimum precision of an `unsigned long long`? +Back: $64$ bits. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +How do we portably compute the smallest possible `unsigned long long`? +Back: As `0`. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +How do we portably compute the largest possible `unsigned long long`? +Back: As `ULLONG_MAX`. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +#### Signed Integers + +| Name | Narrow | Rank | Minimum Width | +| ------------------ | ------ | ---- | ------------- | +| `char` (maybe) | Yes | 1 | - | +| `signed char` | Yes | 1 | 8 | +| `signed short` | Yes | 2 | 16 | +| `signed int` | No | 3 | 16 | +| `signed long` | No | 4 | 32 | +| `signed long long` | No | 5 | 64 | +| `float` | - | - | - | +| `double` | - | - | - | +| `long double` | - | - | - | + +%%ANKI +Basic +Which basic signed type has the smallest rank? +Back: `signed char` and (maybe) `char`. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Which signed type succeeds `signed char` in rank? +Back: `signed short` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Which signed type succeeds `signed short` in rank? +Back: `signed int` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Which signed type succeeds `signed int` in rank? +Back: `signed long` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Which signed type succeeds `signed long` in rank? +Back: `signed long long` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Which signed type succeeds `signed long long` in rank? +Back: N/A. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Which signed narrow type has the highest rank? +Back: `signed short` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Which signed non-narrow type has the smallest rank? +Back: `signed int` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the minimum width of a `signed char`? +Back: $8$ bits. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the underlying binary representation used to encode a `signed char`? +Back: N/A. This is implementation specific. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the minimum precision of a `signed char`? +Back: $7$ bits. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the minimum width of a `signed short`? +Back: $16$ bits. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the minimum precision of a `signed short`? +Back: $15$ bits. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the minimum width of a `signed int`? +Back: $16$ bits. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the underlying binary representation used to encode a `signed char`? +Back: N/A. This is implementation specific. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). +END%% + +%%ANKI +Basic +What is the minimum precision of a `signed int`? +Back: $15$ bits. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the minimum width of a `signed long`? +Back: $32$ bits. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the minimum precision of a `signed long`? +Back: $31$ bits. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the minimum width of a `signed long long`? +Back: $64$ bits. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the minimum precision of a `signed long long`? +Back: $63$ bits. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +How do we portably compute the smallest possible `signed int`? +Back: As `INT_MIN`. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +How do we portably compute the largest possible `signed int`? +Back: As `INT_MAX`. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +#### Enumerations + +An `enum` is a mapping of identifiers with integer values. They have general form: + +```c +enum optional_tag { + type1 ident1; + ... + typeN identN; +} optional_var1 ... optional_varN; +``` + +%%ANKI +Basic +What is the underlined portion of the following declaration called? +```c +enum ___ { ... }; +``` +Back: The tag. +Reference: Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994. + +END%% + +%%ANKI +Basic +What distinguishes the `X`'s from one another in the following? +```c +enum X { ... } X; +``` +Back: The first `X` is a tag whereas the second is a variable with type `enum X`. +Reference: Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994. + +END%% + +%%ANKI +Basic +What distinguishes the `X`'s from one another in the following? +```c +typedef enum X { ... } X; +``` +Back: The first `X` is a tag whereas the second is an alias for type `enum X`. +Reference: Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994. + +END%% + +%%ANKI +Basic +What is the value of `A` in the following? +```c +enum X { A, B, C }; +``` +Back: `0` +Reference: Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994. + +END%% + +%%ANKI +Basic +What is the value of `B` in the following? +```c +enum X { A, B, C }; +``` +Back: `1` +Reference: Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994. + +END%% + +%%ANKI +Basic +What is the value of `B` in the following? +```c +enum X { A=2, B, C }; +``` +Back: `3` +Reference: Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994. + +END%% + +%%ANKI +Basic +According to Linden, what is the "only advantage" of `enum`s over `#define`? +Back: `enum`s can usually be traced in a debugger. +Reference: Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994. + +END%% + +%%ANKI +Basic +Positional values of `enum`s start at what value? +Back: `0` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What name is given to members of an enumeration? +Back: Enumeration constants. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +What mechanism(s) are available for defining constants of type `signed int`? +Back: Macros and (possibly) `enum`s. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +What mechanism(s) are available for defining constants of type `unsigned int`? +Back: Macros and (possibly) `enum`s. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +#### Exact-Width + +The `stdint.h` library contains **exact-width integer types**. These are aliases to types that represent an exact width and sign representation: + +* If the type `uintN_t` is provided, it is an unsigned integer type with exactly $N$ bits of width and precision. +* If the type `intN_t` is provided, is is signed, with two's complement representation, has a width of exactly $N$ bits and a precision of $N - 1$. + +The C standard says these `typedef`s *must* be defined if they can be satisfied. Otherwise they may not exist. + +%%ANKI +Cloze +The {``} library contains {exact-width integer types}. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Suppose `uintN_t` exists. What is its width? +Back: `N` bits. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Suppose `uintN_t` exists. What is its precision? +Back: `N` bits. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Suppose `uintN_t` exists. It is encoded using what binary representation? +Back: Unsigned encoding. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Suppose `intN_t` exists. What is its width? +Back: `N` bits. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Suppose `intN_t` exists. What is its precision? +Back: `N - 1` bits. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Suppose `intN_t` exists. It is encoded using what binary representation? +Back: Two's-complement encoding. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Suppose a platform has a 32-bit signed integer type. Why might `int32_t` not be defined? +Back: The platform may not use two's-complement encoding. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Suppose a platform has a 32-bit signed two's-complement integer type. Why might `int32_t` not be defined? +Back: N/A. The C standard states `int32_t` *must* be defined. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +#### Literals + +Negative integer literals are typed in a counterintuitive way. When the compiler sees a number of form `-X`, the type of `X` is determined *before* being negated. Promotion follows the **first fit rule** described as follows: + +| Decimal | Oct/Hex | +| ----------- | -------------------- | +| `int` | `int` | +| `long` | `unsigned` | +| `long long` | `long` | +| `-` | `unsigned long` | +| `-` | `long long` | +| `-` | `unsigned long long` | + +%%ANKI +Basic +What is the first fit rule? +Back: A specification on what type is given to an integer literal. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the signedness of a decimal integer literal? +Back: `signed` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the signedness of an octal integer literal? +Back: `signed` or `unsigned`. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the signedness of a hexadecimal integer literal? +Back: `signed` or `unsigned`. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +The first fit rule lists what types (in order) for decimal integer literals? +Back: `int`, `long`, and `long long`. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +The first fit rule lists what types (in order) for octal integer literals? +Back: `int`, `unsigned`, `long`, `unsigned long`, `long long`, and `unsigned long long`. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +The first fit rule lists what types (in order) for hexadecimal integer literals? +Back: `int`, `unsigned`, `long`, `unsigned long`, `long long`, and `unsigned long long`. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +The first fit rule lists what types (in order) for hexadecimal integer literals? +Back: `int`, `unsigned`, `long`, `unsigned long`, `long long`, and `unsigned long long`. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). +END%% + +%%ANKI +Basic +How does the compiler process integer literal `-X`? +Back: By first determining the type of `X` and then negating the value. +Reference: Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +What integer literals are guaranteed `signed`? +Back: Decimal integer constants. +Reference: Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +How do we specify an octal integer literal? +Back: Prepend the literal with a `0`. +Reference: Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +Why avoid negative octal integer literals? +Back: Depending on value, the resulting type may be `unsigned`. +Reference: Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +How do we specify a hexadecimal integer literal? +Back: Prepend the literal with a `0x` or `0X`. +Reference: Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +Why avoid negative hexadecimal integer literals? +Back: Depending on value, the resulting type may be `unsigned`. +Reference: Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Cloze +Octal literals are to {`0`} whereas hexadecimal literals are to {`0x`/`0X`}. +Reference: Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +How might C dangerously interpret a negative hexadecimal integer literal? +Back: Depending on the value, the resulting type may be `unsigned`. +Reference: Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +Which header file contains `INT_MAX`? +Back: `` +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Cloze +{`INT_MAX`} is to `signed` whereas {`UINT_MAX`} is to `unsigned`. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +How does `` define `INT_MIN`? +Back: As `(-INT_MAX - 1)`. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +*Why* is `INT_MIN` defined as `(-INT_MAX - 1)` instead of directly as e.g. `-2147483648`? +Back: Because `2147483648` (without `-`) would be sized as a non-`int` before being negated. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Cloze +`INT_MAX` is to {``} whereas `INT32_MAX` is to {``}. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +In what order does C cast size and "signedness"? +Back: C casts size then signedness. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +In what order does C cast "signedness" and size? +Back: C casts size then signedness. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +Given `short sx`, cast `(unsigned) sx` is more explicitly written as what other sequence of casts? +Back: `(unsigned) (int) sx` +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +Given `short sx`, are the following two lines equivalent? +```c +(unsigned) sx +(unsigned) (int) sx +``` +Back: Yes. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +Given `short sx`, are the following two lines equivalent? +```c +(unsigned) sx +(unsigned) (unsigned short) sx +``` +Back: No. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +Given `short sx`, why is the following not an identity? +```c +(unsigned) sx = (unsigned) (unsigned short) sx +``` +Back: `(unsigned) sx` casts size before "signedness". +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +What does "signedness" of a variable refer to? +Back: Whether the variable was declared `signed` or `unsigned`. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +Integer constants can be made a certain signedness or type by using the following suffixes: + +| Suffix | Type | +| ------ | -------------------- | +| `U` | `unsigned` | +| `L` | `long` | +| `LL` | `long long` | +| `ULL` | `unsigned long long` | + +%%ANKI +Basic +Is an integer literal without a suffix (e.g. `U`) signed or unsigned? +Back: This depends on the first fit rule. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What type is given to an integer literal without a suffix (e.g. `U`)? +Back: This depends on the first fit rule. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What suffix can be used to denote an `unsigned` integer literal? +Back: Case-insensitive `U`. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +What suffix can be used to denote a `long` integer literal? +Back: Case-insensitive `L`. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +What suffix can be used to denote a `long long` integer literal? +Back: Case-insensitive `LL`. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +What suffix can be used to denote an `unsigned long long` integer literal? +Back: Case-insensitive `ULL`. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +What type is given to integer literal `-1`? +Back: `int` +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +What type is given to integer literal `-1U`? +Back: `unsigned int` +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +What type is given to integer literal `1`? +Back: `int` +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +What type is given to integer literal `0x0U`? +Back: `unsigned int` +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +#### Integer Constant Expressions + +An integer constant expression (ICE) is a compile-time integer value. Its value must be determinable at compile time (e.g. no function calls are permitted), and also no evaluation of an object must participate as an operand. + +%%ANKI +Basic +What is ICE an acronym for? +Back: **I**nteger **c**onstant **e**xpression. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +*Why* isn't `b42` in the following considered an ICE? +```c +enum { b42 = 42 }; +``` +Back: N/A. It is. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +*Why* isn't `b42` in the following considered an ICE? +```c +signed const a42 = 42; +enum { b42 = a42 }; +``` +Back: Because it depends on the evaluation of object `a42`. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +*Why* isn't `c52` in the following considered an ICE? +```c +enum { b42 = 42, c52 = b42 + 10 }; +``` +Back: N/A. It is. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +*Why* isn't `b42` in the following considered an ICE? +```c +signed const a42() { return 42; } +enum { b42 = a42() }; +``` +Back: Because it depends on a function call. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +### Floating Point + +#### Literals + +Floating-point constants can be made a certain type by using the following suffixes: + +| Suffix | Type | +| ------ | ------------- | +| `F` | `float` | +| `L` | `long double` | + +%%ANKI +Basic +What type is given to a floating-point literal without a suffix (e.g. `F`)? +Back: `double` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What suffix can be used to denote a `float` floating-point literal? +Back: Case-insensitive `F`. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +What suffix can be used to denote a `double` floating-point literal? +Back: N/A. Do not use a suffix. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +What suffix can be used to denote a `long double` floating-point literal? +Back: Case-insensitive `L`. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +What type is given to floating-point literal `-1.0`? +Back: `double` +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +What type is given to floating-point literal `-1.0F`? +Back: `float` +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +What type is given to floating-point literal `-1.0FL`? +Back: N/A. Invalid suffix. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +What type is given to floating-point literal `-1.0L`? +Back: `long double` +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +What type is given to floating-point literal `-1.0LL`? +Back: N/A. Invalid suffix. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Cloze +`INT_MAX` is to {``} whereas `DBL_MAX` is to {``}. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What does `DBL_MIN` refer to? +Back: The smallest `double` strictly greater than 0. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +How do we portably compute the smallest possible `double`? +Back: As `-DBL_MAX`. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +How do we portably compute the largest possible `double`? +Back: As `DBL_MAX`. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + ## Basic Types The **basic types** comprise of `char`, the signed/unsigned integer types, and the floating point types. All basic types are complete object types. @@ -437,310 +1869,971 @@ Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open END%% -## Conversions +## Derived Types -> Several operators convert operand values from one type to another automatically. - -The above quote refers to **implicit conversions**. Alternatively, we can use cast operations to perform **explicit conversions**. - -### Usual Arithmetic Conversions - -As a general rule, the result of an operation has the type of the operand with wider range. The usual arithmetic conversions behave according to the following pattern: - -1. Determine a **common real type** for the operands and result. -2. Convert each operand, without change of type domain, to a type with real type matching the common real type. -3. Unless explicitly stated otherwise, the common real type is the corresponding real type of the result, whose type domain is that of the operands if they are the same and complex otherwise. - -Common real types are prioritized in the following order: - -1. `long double` -2. `double` -3. `float` -4. If both operands have the same signedness, the higher ranked type. -5. If the unsigned operand has rank $\geq$ than that of the other, the unsigned type. -6. If the signed operand's includes that of the other, the signed type. -7. The unsigned integer type corresponding to that of the signed integer type. +A type is said to be **derived** if they are defined relative to other types. [[pointers|Pointers]] are also examples of derived types. %%ANKI Basic -What is the common real type of `a` and `b`? -```c -long double a; -long double complex b; -``` -Back: `long double` -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - +What is a derived type? +Back: A type defined relative to other types. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +### Aggregate Data Types + +The **aggregate data types** are so called because they combine multiple instances of one or several other data types. + +%%ANKI +Basic +What are the two aggregate data types? +Back: Arrays and structures. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + END%% %%ANKI Basic -What is the common real type of `a` and `b`? -```c -long double a; -long complex b; -``` -Back: `long double` -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - +Why are the aggregate data types named the way they are? +Back: They combine multiple instances of one or several other data types. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +#### Arrays + +An array is a contiguous sequence of objects. An array is either a **fixed-length array** or a **variable-length array**. + +%%ANKI +Cloze +{Arrays} combine items that all have the same {base type}. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + END%% %%ANKI Basic -What is the type domain of `a + b`? +What is the base type of the following array? ```c -long double a; -long complex b; -``` -Back: Complex. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -What is the common real type of `a` and `b`? -```c -double complex a; -long b; +double a[4]; ``` Back: `double` -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + END%% %%ANKI Basic -What is the type domain of `a + b`? +What is the base type of the following array? ```c -long double a; -long complex b; +double a[4][4]; ``` -Back: Complex. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). +Back: `double[4]` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + END%% %%ANKI Basic -What is the common real type of `a` and `b`? +How are parentheses introduced to the following declaration without affecting meaning? ```c -float a; -double b; +double a[4][4]; ``` -Back: `double` -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -What is the common real type of `a` and `b`? +Back: ```c -long long a; -float b; +double (a[4])[4]; ``` -Back: `float` -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + END%% %%ANKI Basic -What is the type domain of `a + b`? +Consider the following. How many objects make up `a[0]`? ```c -long long a; -float b; +double a[M][N]; ``` -Back: Real. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - +Back: `N` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + END%% %%ANKI Basic -*Why* is the common real type of `a` and `b` equal to `float`? +Consider the following. How many objects make up `a[M - 1]`? ```c -long long a; -float b; +double a[M][N]; ``` -Back: Because floating-point types have priority over integer types. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - +Back: `N` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + END%% %%ANKI Basic -What is the type domain of `a + b`? +Consider the following. How many objects make up `a[N - 1]`? ```c -long long a; -long long b; +double a[M][N]; ``` -Back: Real. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - +Back: Indeterminate. This may not be a valid array access. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + END%% %%ANKI Basic -What is the common real type of `a` and `b`? +Consider the following where `N < M`. How many objects make up `a[N - 1]`? ```c -long long a; -long long b; +double a[M][N]; ``` -Back: `long long` -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - +Back: `N` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + END%% %%ANKI Basic -What is the common real type of `a` and `b`? +Consider the following where `M < N`. How many objects make up `a[N - 1]`? ```c -unsigned int a; -signed short b; +double a[M][N]; ``` -Back: `unsigned int` -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - +Back: N/A. Invalid array access. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Cloze +Array `a[M][N]` is a sequence of {1:`M`} objects each containing {1:`N`} objects. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + END%% %%ANKI Basic -What is the common real type of `a` and `b`? +Arrays fall under what two categories? +Back: Fixed-length and variable-length. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Replace `EXPR` in the following with an expression to compute the length of `A`. ```c -unsigned a; -signed int b; +int A[N]; +size_t len = EXPR; ``` -Back: `unsigned` -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - +Back: `(sizeof A) / (sizeof A[0])` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + END%% %%ANKI Basic -*Why* is the common real type of `a` and `b` equal to `unsigned`? +What two syntactic forms does the `sizeof` operator come in? +Back: With and without parentheses. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +When can the `sizeof` operator elide parentheses? +Back: When operating on an object. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +When does the `sizeof` operator *require* parentheses? +Back: When operating on a type. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Apply the possible syntactic forms of the `sizeof` operator to object `a`. +Back: ```c -unsigned a; -signed int b; +sizeof a +sizeof(a) ``` -Back: Because `unsigned` has rank $\geq$ that of `signed int`. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + END%% %%ANKI Basic -What is the type domain of `a + b`? +Apply the possible syntactic forms of the `sizeof` operator to type `int`. +Back: ```c -unsigned a; -signed int b; +sizeof(int) ``` -Back: Real. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + END%% %%ANKI Basic -What is the type domain of `a + b`? +Suppose I pass the following array to a function. Which dimension is lost? ```c -unsigned short a; -signed int b; +double a[M][N][P]; ``` -Back: Real. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - +Back: `M` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + END%% %%ANKI Basic -What is the common real type of `a + b`? +Why shouldn't you use the `sizeof` operator on array parameters to functions? +Back: The innermost dimension of an array parameter to a function is lost. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +How is the following array parameter rewritten using pointer syntax? ```c -unsigned short a; -signed int b; +void foo(int a[const]) {} ``` -Back: Indeterminate. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -*Why* is the common real type of `a + b` indeterminate? +Back: ```c -unsigned short a; -int b; +void foo(int *const a) {} ``` -Back: Because the rank of `unsigned short` is $<$ that of `int`. Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - + END%% %%ANKI Basic -What *might* the common real type of `a + b` be? +How is the following array parameter rewritten using pointer syntax? ```c -unsigned short a; -signed int b; +void foo(const int a[]) {} ``` -Back: `signed int` or `unsigned int`. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -When is the common real type of `a + b` equal to `signed int`? +Back: ```c -unsigned short a; -signed int b; +void foo(const int *a) {} ``` -Back: When `signed int` can represent all the values of `unsigned short`. Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - + END%% %%ANKI Basic -When is the common real type of `a + b` equal to `unsigned int`? +How is the following pointer parameter rewritten using array syntax? ```c -unsigned short a; -signed int b; +void foo(int *restrict a) {} ``` -Back: When `signed int` cannot represent all the values of `unsigned short`. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -When is the common real type of `a + b` equal to `unsigned short`? +Back: ```c -unsigned short a; -signed int b; +void foo(int a[restrict]) {} ``` -Back: N/A. It never is. Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - + END%% %%ANKI Basic -Suppose `a` and `b` has signed and unsigned types respectively. When is `a + b` signed? -Back: When `b`'s type has lower rank and the range of `b` is included in the range of `a`. +How is the following pointer parameter rewritten using array syntax? +```c +void foo(volatile int *a) {} +``` +Back: +```c +void foo(volatile int a[]) {} +``` Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - + END%% %%ANKI Basic -Suppose `a` and `b` have signed and unsigned types respectively. When is `a + b` unsigned? -Back: When `b`'s type has higher rank or the range of `a` cannot fit the range of `b`. +What does the `3` keyword guarantee to the compiler? +```c +void bar(int a[3]); +``` +Back: N/A. It is ignored. Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - + +END%% + +%%ANKI +Basic +*Why* might you see e.g. `3` like in the following prototype? +```c +void bar(int a[3]); +``` +Back: It serves as documentation. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +In the following, what does the `static` keyword guarantee to the compiler? +```c +void bar(int a[static 3]); +``` +Back: The minimum number of elements for the compiler to assume `a` contains. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +*Why* does the following produce a compilation error? +```c +void foo(int a[3]); + +int main() { + int a[] = { 1, 2 }; + foo(a); +} +``` +Back: N/A. It doesn't. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +*Why* does the following produce a compilation error? +```c +void foo(int a[static 3]); + +int main() { + int a[] = { 1, 2 }; + foo(a); +} +``` +Back: Because the argument to `foo` does not have at least `3` elements. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +*Why* does the following produce a compilation error? +```c +void foo(int a[static 3]); + +int main() { + int a[] = { 1, 2, 3, 4 }; + foo(a); +} +``` +Back: N/A. It doesn't. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +*Why* does the following produce a compilation error? +```c +void foo(int a[3]); + +int main() { + foo(0); +} +``` +Back: N/A. It doesn't. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +*Why* does the following produce a compilation error? +```c +void foo(int a[static 1]); + +int main() { + foo(0); +} +``` +Back: Because `static` indicates a valid pointer must be passed to `foo`. +Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). + +END%% + +%%ANKI +Basic +Add initializer with first element set to `1` using designated initialization. +```c +int example[3]; +``` +Back: +```c +int example[3] = { [0] = 1 }; +``` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Add initializer with first element set to `1` *without* using designated initialization. +```c +int example[3]; +``` +Back: +```c +int example[3] = { 1 }; +``` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the value of `example[1]` in the following? +```c +int example[2] = { [0] = 1 }; +``` +Back: `0` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Are arrays passed by reference or value? +Back: Reference. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Is the following an FLA or VLA? +```c +double a[4]; +``` +Back: FLA. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Is the following an FLA or VLA? +```c +double a[]; +``` +Back: N/A. This is invalid syntax. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Is the following an FLA or VLA? +```c +double a[] = { 0 }; +``` +Back: FLA. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Is the following an FLA or VLA? +```c +int n = 2; +double a[n]; +``` +Back: VLA. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Is the following an FLA or VLA? +```c +#define n 2 +double a[n]; +``` +Back: FLA. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Is the following an FLA or VLA? +```c +enum { n = 2 }; +double a[n]; +``` +Back: FLA. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Is the following an FLA or VLA? +```c +int n = 2; +double a[n] = {0}; +``` +Back: N/A. Variable-sized objects may not be initialized. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +Evaluation of an array `A` returns `&A[0]`, i.e. a [[#Pointers|pointer]] to the first array element. This is called **array decay**. + +%%ANKI +Basic +What is the effect of array decay? +Back: Evaluation of an array `A` returns `&A[0]`. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What name is given to the implicit conversion of an array to a pointer? +Back: Array decay. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +According to Gustedt, what C feature explains why are there no "array values"? +Back: Array-to-pointer decay. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Why can't arrays directly be made arguments to functions? +Back: Because array arguments decay to pointers. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Cloze +In a function declaration, any array parameter rewrites to {a pointer}. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +##### Fixed-Length + +A fixed-length array (FLA) has a predetermined size. Their stack allocations can be computed at compilation time. + +%%ANKI +Basic +What is FLA an acronym for? +Back: **F**ixed-**l**ength **a**rray. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +What two ways can the length of an FLA be specified? +Back: By an ICE or an initializer. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +For the following to be an FLA, what must `N` be? +```c +double a[N]; +``` +Back: An ICE. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +*Why* must FLAs be declared within a function? +Back: N/A. They can exist elsewhere. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. +Tags: x86-64 + +END%% + +##### Variable-Length + +A variable-length array (VLA) has its size determined at runtime. Their stack allocations must be determined with respect to other registers available to the frame. + +%%ANKI +Basic +What is VLA an acronym for? +Back: **V**ariable-**l**ength **a**rray. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +Where *must* a VLA be declared? +Back: Within a function. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +*Why* do variable-length arrays require use of a frame pointer? +Back: Offsets depend on how much space must be allocated on the stack. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. +Tags: x86-64 + +END%% + +%%ANKI +Basic +*Why* must VLAs be declared within a function? +Back: Their implementation relies on frame pointers. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. +Tags: x86-64 + +END%% + +#### Structures + +A `struct` is a grouping of data together. It has the following general form: + +```c +struct optional_tag { + type_1 ident1; + ... + type_N identN; +} optional_var1 ... optional_varM; +``` + +%%ANKI +Cloze +{Structures} combine items that may have different {base types}. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the underlined portion of the following declaration called? +```c +struct ___ { ... }; +``` +Back: The tag. +Reference: Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994. + +END%% + +%%ANKI +Basic +What distinguishes the `X`'s from one another in the following? +```c +struct X { ... } X; +``` +Back: The first `X` is a tag whereas the second is a variable with type `struct X`. +Reference: Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994. + +END%% + +%%ANKI +Basic +What distinguishes the `X`'s from one another in the following? +```c +typedef struct X { ... } X; +``` +Back: The first `X` is a tag whereas the second is an alias for type `struct X`. +Reference: Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994. + +END%% + +%%ANKI +Basic +Define an object with `fieldA` set to `1` using designated initialization. +```c +struct example { int fieldA; }; +``` +Back: +```c +struct example test = { .fieldA = 1 }; +``` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Define an object with `fieldA` set to `1` *without* using designated initialization. +```c +struct example { int fieldA; }; +``` +Back: +```c +struct example test = { 1 }; +``` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the value of `test.fieldB` in the following? +```c +struct example { int fieldA; int fieldB; }; +struct example test = { .fieldA = 1 }; +``` +Back: `0` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Are `struct`s well-defined with respect to `=` (i.e. assignment)? +Back: Yes. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Are `struct`s well-defined with respect to `==` (i.e. equality)? +Back: No. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Are `struct`s well-defined with respect to `!=` (i.e. inequality)? +Back: No. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Generally speaking, what are valid member types of a `struct`? +Back: All object types except VLAs. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Generally speaking, what object type is not a valid `struct` member type? +Back: VLAs. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Declare a variable `var` of the nested `struct` with member `c` set to `1`. +```c +struct A { + struct B { int c; }; +}; +``` +Back: `struct B var = { 1 };` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the visibility of `struct B` with respect to `struct A`? +```c +struct A { + struct B { int c; }; +}; +``` +Back: They have the same visibility. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What does it mean for `struct A` and `struct B` to have the same visibility? +```c +struct A { + struct B { int c; }; +}; +``` +Back: Nesting `struct`s does not introduce any notion of scope. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +### Unions + +A `union` is a grouping of data together but with overlaid storage. It has the following general form: + +```c +union optional_tag { + type1 ident1; + ... + typeN identN; +} optional_var1 ... optional_varN; +``` + +%%ANKI +Basic +What is the underlined portion of the following declaration called? +```c +union ___ { ... }; +``` +Back: The tag. +Reference: Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994. + +END%% + +%%ANKI +Basic +What distinguishes the `X`'s from one another in the following? +```c +union X { ... } X; +``` +Back: The first `X` is a tag whereas the second is a variable with type `union X`. +Reference: Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994. + +END%% + +%%ANKI +Basic +What distinguishes the `X`'s from one another in the following? +```c +typedef union X { ... } X; +``` +Back: The first `X` is a tag whereas the second is an alias for type `union X`. +Reference: Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994. + +END%% + +### Functions + +A function `f` without a following opening `(` is converted to a pointer to its start. This is called **function decay**. + +%%ANKI +Basic +What is the effect of function decay? +Back: Evaluation of a function `f` without a following opening `(` is converted to a pointer to its start. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What name is given to the implicit conversion of a function to a pointer? +Back: Function decay. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +According to Gustedt, what C feature explains why are there no "function values"? +Back: Function-to-pointer decay. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Why can't functions directly be made arguments to functions? +Back: Because function arguments decay to pointers. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Cloze +{1:Function pointers} are to {2:`(...)`} whereas {2:pointers} are to {1:`[...]`}. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +In what order are decays, dereferences, address ofs, and calls performed in the following? +```c +f(3); +``` +Back: Decay, call. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +In what order are decays, dereferences, address ofs, and calls performed in the following? +```c +(&f)(3); +``` +Back: Address of, call. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +In what order are decays, dereferences, address ofs, and calls performed in the following? +```c +(*f)(3); +``` +Back: Decay, dereference, decay, call. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +In what order are decays, dereferences, address ofs, and calls performed in the following? +```c +(*&f)(3); +``` +Back: Address of, dereference, decay, call. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +In what order are decays, dereferences, address ofs, and calls performed in the following? +```c +(&*f)(3); +``` +Back: Decay, dereference, address of, call. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Cloze +{1:Pointers} refer to {2:arrays} whereas {2:function pointers} refer to {1:functions}. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + END%% ## Bibliography * “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). -* Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). \ No newline at end of file +* Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). +* Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. [http://www.catb.org/esr/structure-packing/](http://www.catb.org/esr/structure-packing/). +* Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994. \ No newline at end of file diff --git a/notes/c17/types/simple.md b/notes/c17/types/simple.md deleted file mode 100644 index b31d1d7..0000000 --- a/notes/c17/types/simple.md +++ /dev/null @@ -1,1486 +0,0 @@ ---- -title: Simple Types -TARGET DECK: Obsidian::STEM -FILE TAGS: c17::type -tags: - - c17 ---- - -## Overview - -The bottom of the type hierarchy consists of **simple types**. This comprises the primitive types that all other types are either based off of or derived from. - -| Signed | Unsigned | 32-bit | 64-bit | -| ------------- | -------------------- | ------ | ------ | -| - | `bool` | 1 | 1 | -| `signed char` | `unsigned char` | 1 | 1 | -| `short` | `unsigned short` | 2 | 2 | -| `int` | `unsigned` | 4 | 4 | -| `long` | `unsigned long` | 4 | 8 | -| `long long` | `unsigned long long` | 8 | 8 | - -| Real | Complex | 32-bit | 64-bit | -| ------------- | --------------------- | ------ | ------ | -| `float` | - | 4 | 4 | -| `double` | - | 8 | 8 | -| `long double` | - | - | - | -| - | `float complex` | 4 | 4 | -| - | `double complex` | 8 | 8 | -| - | `long double complex` | - | - | - -%%ANKI -Cloze -The {``} header file contains {fixed width data integral types}. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -The integer types are grouped into what two classes? -Back: The signed and unsigned integer types. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -The floating point types are grouped into what two classes? -Back: The real and complex floating point types. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -Which header file contains `INT32_MAX`? -Back: `` -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -What two variants does a C integral type declaration have? -Back: Signed and unsigned. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -What does it mean for an integer to be "signed"? -Back: It can represent negative, zero, and positive values. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -What does it mean for an integer to be "unsigned"? -Back: It can only represent nonnegative values. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -How large is a word? -Back: This is a machine-dependent value. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -What word sizes are typical nowadays? -Back: 32- and 64-bit word sizes. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Cloze -`char` *typically* represents {1} byte(s) on a 64-bit platform. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Cloze -`short` *typically* represents {2} byte(s) on a 64-bit platform. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Cloze -`int` *typically* represents {4} bytes(s) on a 64-bit platform. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Cloze -`unsigned` *typically* represents {4} bytes(s) on a 64-bit platform. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Cloze -`long` *typically* represents {8} bytes(s) on a 64-bit platform. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Cloze -`long long` *typically* represents {8} bytes(s) on a 64-bit platform. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -What distinguishes `long` from `long long`? -Back: `long long`s are guaranteed to be at least 64-bit wide. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -*Why* is there both a `long` and `long long`? -Back: `long long`s are at least 64-bit wide, even on 32-bit platforms. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Cloze -`char *` *typically* represents {8} bytes(s) on a 64-bit platform. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Cloze -`float` *typically* represents {4} bytes(s) on a 64-bit platform. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Cloze -`double` *typically* represents {8} bytes(s) on a 64-bit platform. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -Is declaration `int` signed or unsigned? -Back: Signed. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -How is declaration `unsigned` written more precisely? -Back: `unsigned int`. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -Is declaration `long` signed or unsigned? -Back: Signed. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Cloze -{1:`float`} has {2:4} byte width whereas {2:`double`} has {1:8} byte width. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Cloze -The C standard defines {lower} bounds on numeric ranges of data types. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Cloze -The C standard does *not* define {1:upper} bounds on numeric ranges of data types (except for {1:fixed-size} types). -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -## Integers - -**Narrow types** cannot be used directly in arithmetic. Instead they are first promoted to a wider type. On almost every system, this promotion will be to a `signed int` of the same value, regardless of the signedness of the narrow type itself. - -%%ANKI -Basic -Why are narrow types named the way they are? -Back: They are considered too small to be used directly in arithmetic expressions. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Signed narrow types are usually promoted to what larger type? -Back: A `signed int`. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Unsigned narrow types found are usually promoted to what larger type? -Back: A `signed int`. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -### Character Types - -The three types `char`, `signed char`, and `unsigned char` are collectively called the **character types**. The implementation defines `char` to have the same range, representation, and behavior as either `signed char` or `unsigned char`, but is considered incompatible with both. That is, it is a distinct type in the eyes of the type system. - -%%ANKI -Basic -Is declaration `char` signed or unsigned? -Back: This is implementation-dependent. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -What are the character types? -Back: `char`, `signed char`, and `unsigned char`. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -`char` has the same range, representation, and behavior as what other type? -Back: Either `signed char` or `unsigned char`. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -How do you check if `char` has the same representation as a `signed char` or `unsigned char`? -Back: Check if `CHAR_MIN` has value `SCHAR_MIN` or `0` respectively. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -Which header defines `CHAR_MIN`? -Back: `` -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -Which character type should be used for strings? -Back: `char` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What does it mean for `char` to be incompatible with other character types? -Back: The type system considers it to be a distinct type. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What does `sizeof(char)` evaluate to? -Back: `1` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -### Unsigned Integers - -| Name | Narrow | Rank | Minimum Width | -| -------------------- | ------ | ---- | ------------- | -| `bool` | Yes | 0 | 1 | -| `char` (maybe) | Yes | 1 | - | -| `unsigned char` | Yes | 1 | 8 | -| `unsighed short` | Yes | 2 | 16 | -| `unsigned int` | No | 3 | 16 | -| `unsigned long` | No | 4 | 32 | -| `unsigned long long` | No | 5 | 64 | - -%%ANKI -Basic -Which basic unsigned type has the smallest rank? -Back: `bool` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Which unsigned type next succeeds `bool` in rank? -Back: `unsigned char` and (maybe) `char`. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Which unsigned type next succeeds `unsigned char` in rank? -Back: `unsigned short` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Which unsigned type next succeeds `unsigned short` in rank? -Back: `unsigned int` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Which unsigned type next succeeds `unsigned int` in rank? -Back: `unsigned long` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Which unsigned type next succeeds `unsigned long` in rank? -Back: `unsigned long long` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Which unsigned type next succeeds `unsigned long long` in rank? -Back: N/A. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Which unsigned narrow type has the highest rank? -Back: `unsigned short` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Which unsigned non-narrow type has the smallest rank? -Back: `unsigned int` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What is the minimum width of a `bool`? -Back: $1$ bit. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What is the minimum precision of a `bool`? -Back: $1$ bit. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What is the minimum width of an `unsigned char`? -Back: $8$ bits. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What is the minimum precision of an `unsigned char`? -Back: $8$ bits. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What is the maximum precision of an `unsigned char`? -Back: N/A. C does not define maximum precisions. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What is the minimum width of an `unsigned short`? -Back: $16$ bits. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What is the underlying binary representation used to encode an `unsigned short`? -Back: Unsigned encoding. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What is the minimum precision of an `unsigned short`? -Back: $16$ bits. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What is the minimum width of an `unsigned int`? -Back: $16$ bits. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What is the minimum precision of an `unsigned int`? -Back: $16$ bits. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What is the minimum width of an `unsigned long`? -Back: $32$ bits. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What is the underlying binary representation used to encode an `unsigned long`? -Back: Unsigned encoding. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What is the minimum precision of an `unsigned long`? -Back: $32$ bits. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What is the minimum width of an `unsigned long`? -Back: $32$ bits. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). -END%% - -%%ANKI -Basic -What is the minimum width of an `unsigned long long`? -Back: $64$ bits. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What is the minimum precision of an `unsigned long long`? -Back: $64$ bits. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -How do we portably compute the smallest possible `unsigned long long`? -Back: As `0`. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -How do we portably compute the largest possible `unsigned long long`? -Back: As `ULLONG_MAX`. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -### Signed Integers - -| Name | Narrow | Rank | Minimum Width | -| ------------------ | ------ | ---- | ------------- | -| `char` (maybe) | Yes | 1 | - | -| `signed char` | Yes | 1 | 8 | -| `signed short` | Yes | 2 | 16 | -| `signed int` | No | 3 | 16 | -| `signed long` | No | 4 | 32 | -| `signed long long` | No | 5 | 64 | -| `float` | - | - | - | -| `double` | - | - | - | -| `long double` | - | - | - | - -%%ANKI -Basic -Which basic signed type has the smallest rank? -Back: `signed char` and (maybe) `char`. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Which signed type succeeds `signed char` in rank? -Back: `signed short` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Which signed type succeeds `signed short` in rank? -Back: `signed int` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Which signed type succeeds `signed int` in rank? -Back: `signed long` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Which signed type succeeds `signed long` in rank? -Back: `signed long long` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Which signed type succeeds `signed long long` in rank? -Back: N/A. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Which signed narrow type has the highest rank? -Back: `signed short` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Which signed non-narrow type has the smallest rank? -Back: `signed int` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What is the minimum width of a `signed char`? -Back: $8$ bits. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What is the underlying binary representation used to encode a `signed char`? -Back: N/A. This is implementation specific. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What is the minimum precision of a `signed char`? -Back: $7$ bits. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What is the minimum width of a `signed short`? -Back: $16$ bits. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What is the minimum precision of a `signed short`? -Back: $15$ bits. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What is the minimum width of a `signed int`? -Back: $16$ bits. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What is the underlying binary representation used to encode a `signed char`? -Back: N/A. This is implementation specific. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). -END%% - -%%ANKI -Basic -What is the minimum precision of a `signed int`? -Back: $15$ bits. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What is the minimum width of a `signed long`? -Back: $32$ bits. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What is the minimum precision of a `signed long`? -Back: $31$ bits. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What is the minimum width of a `signed long long`? -Back: $64$ bits. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What is the minimum precision of a `signed long long`? -Back: $63$ bits. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -How do we portably compute the smallest possible `signed int`? -Back: As `INT_MIN`. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -How do we portably compute the largest possible `signed int`? -Back: As `INT_MAX`. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -### Literals - -Negative integer literals are typed in a counterintuitive way. When the compiler sees a number of form `-X`, the type of `X` is determined *before* being negated. Promotion follows the **first fit rule** described as follows: - -| Decimal | Oct/Hex | -| ----------- | -------------------- | -| `int` | `int` | -| `long` | `unsigned` | -| `long long` | `long` | -| `-` | `unsigned long` | -| `-` | `long long` | -| `-` | `unsigned long long` | - -%%ANKI -Basic -What is the first fit rule? -Back: A specification on what type is given to an integer literal. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What is the signedness of a decimal integer literal? -Back: `signed` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What is the signedness of an octal integer literal? -Back: `signed` or `unsigned`. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What is the signedness of a hexadecimal integer literal? -Back: `signed` or `unsigned`. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -The first fit rule lists what types (in order) for decimal integer literals? -Back: `int`, `long`, and `long long`. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -The first fit rule lists what types (in order) for octal integer literals? -Back: `int`, `unsigned`, `long`, `unsigned long`, `long long`, and `unsigned long long`. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -The first fit rule lists what types (in order) for hexadecimal integer literals? -Back: `int`, `unsigned`, `long`, `unsigned long`, `long long`, and `unsigned long long`. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -The first fit rule lists what types (in order) for hexadecimal integer literals? -Back: `int`, `unsigned`, `long`, `unsigned long`, `long long`, and `unsigned long long`. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). -END%% - -%%ANKI -Basic -How does the compiler process integer literal `-X`? -Back: By first determining the type of `X` and then negating the value. -Reference: Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -What integer literals are guaranteed `signed`? -Back: Decimal integer constants. -Reference: Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -How do we specify an octal integer literal? -Back: Prepend the literal with a `0`. -Reference: Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -Why avoid negative octal integer literals? -Back: Depending on value, the resulting type may be `unsigned`. -Reference: Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -How do we specify a hexadecimal integer literal? -Back: Prepend the literal with a `0x` or `0X`. -Reference: Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -Why avoid negative hexadecimal integer literals? -Back: Depending on value, the resulting type may be `unsigned`. -Reference: Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Cloze -Octal literals are to {`0`} whereas hexadecimal literals are to {`0x`/`0X`}. -Reference: Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -How might C dangerously interpret a negative hexadecimal integer literal? -Back: Depending on the value, the resulting type may be `unsigned`. -Reference: Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -Which header file contains `INT_MAX`? -Back: `` -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Cloze -{`INT_MAX`} is to `signed` whereas {`UINT_MAX`} is to `unsigned`. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -How does `` define `INT_MIN`? -Back: As `(-INT_MAX - 1)`. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -*Why* is `INT_MIN` defined as `(-INT_MAX - 1)` instead of directly as e.g. `-2147483648`? -Back: Because `2147483648` (without `-`) would be sized as a non-`int` before being negated. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Cloze -`INT_MAX` is to {``} whereas `INT32_MAX` is to {``}. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -In what order does C cast size and "signedness"? -Back: C casts size then signedness. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -In what order does C cast "signedness" and size? -Back: C casts size then signedness. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -Given `short sx`, cast `(unsigned) sx` is more explicitly written as what other sequence of casts? -Back: `(unsigned) (int) sx` -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -Given `short sx`, are the following two lines equivalent? -```c -(unsigned) sx -(unsigned) (int) sx -``` -Back: Yes. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -Given `short sx`, are the following two lines equivalent? -```c -(unsigned) sx -(unsigned) (unsigned short) sx -``` -Back: No. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -Given `short sx`, why is the following not an identity? -```c -(unsigned) sx = (unsigned) (unsigned short) sx -``` -Back: `(unsigned) sx` casts size before "signedness". -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -What does "signedness" of a variable refer to? -Back: Whether the variable was declared `signed` or `unsigned`. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -Integer constants can be made a certain signedness or type by using the following suffixes: - -| Suffix | Type | -| ------ | -------------------- | -| `U` | `unsigned` | -| `L` | `long` | -| `LL` | `long long` | -| `ULL` | `unsigned long long` | - -%%ANKI -Basic -Is an integer literal without a suffix (e.g. `U`) signed or unsigned? -Back: This depends on the first fit rule. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What type is given to an integer literal without a suffix (e.g. `U`)? -Back: This depends on the first fit rule. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What suffix can be used to denote an `unsigned` integer literal? -Back: Case-insensitive `U`. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -What suffix can be used to denote a `long` integer literal? -Back: Case-insensitive `L`. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -What suffix can be used to denote a `long long` integer literal? -Back: Case-insensitive `LL`. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -What suffix can be used to denote an `unsigned long long` integer literal? -Back: Case-insensitive `ULL`. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -What type is given to integer literal `-1`? -Back: `int` -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -What type is given to integer literal `-1U`? -Back: `unsigned int` -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -What type is given to integer literal `1`? -Back: `int` -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -What type is given to integer literal `0x0U`? -Back: `unsigned int` -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -### Integer Constant Expressions - -An integer constant expression (ICE) is a compile-time integer value. Its value must be determinable at compile time (e.g. no function calls are permitted), and also no evaluation of an object must participate as an operand. - -%%ANKI -Basic -What is ICE an acronym for? -Back: **I**nteger **c**onstant **e**xpression. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -*Why* isn't `b42` in the following considered an ICE? -```c -enum { b42 = 42 }; -``` -Back: N/A. It is. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -*Why* isn't `b42` in the following considered an ICE? -```c -signed const a42 = 42; -enum { b42 = a42 }; -``` -Back: Because it depends on the evaluation of object `a42`. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -*Why* isn't `c52` in the following considered an ICE? -```c -enum { b42 = 42, c52 = b42 + 10 }; -``` -Back: N/A. It is. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -*Why* isn't `b42` in the following considered an ICE? -```c -signed const a42() { return 42; } -enum { b42 = a42() }; -``` -Back: Because it depends on a function call. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -## Floating Point - -### Literals - -Floating-point constants can be made a certain type by using the following suffixes: - -| Suffix | Type | -| ------ | ------------- | -| `F` | `float` | -| `L` | `long double` | - -%%ANKI -Basic -What type is given to a floating-point literal without a suffix (e.g. `F`)? -Back: `double` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What suffix can be used to denote a `float` floating-point literal? -Back: Case-insensitive `F`. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -What suffix can be used to denote a `double` floating-point literal? -Back: N/A. Do not use a suffix. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -What suffix can be used to denote a `long double` floating-point literal? -Back: Case-insensitive `L`. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -What type is given to floating-point literal `-1.0`? -Back: `double` -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -What type is given to floating-point literal `-1.0F`? -Back: `float` -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -What type is given to floating-point literal `-1.0FL`? -Back: N/A. Invalid suffix. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -What type is given to floating-point literal `-1.0L`? -Back: `long double` -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -What type is given to floating-point literal `-1.0LL`? -Back: N/A. Invalid suffix. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Cloze -`INT_MAX` is to {``} whereas `DBL_MAX` is to {``}. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What does `DBL_MIN` refer to? -Back: The smallest `double` strictly greater than 0. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -How do we portably compute the smallest possible `double`? -Back: As `-DBL_MAX`. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -How do we portably compute the largest possible `double`? -Back: As `DBL_MAX`. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -## Exact-Width Integer Types - -The `stdint.h` library contains **exact-width integer types**. These are aliases to types that represent an exact width and sign representation: - -* If the type `uintN_t` is provided, it is an unsigned integer type with exactly $N$ bits of width and precision. -* If the type `intN_t` is provided, is is signed, with two's complement representation, has a width of exactly $N$ bits and a precision of $N - 1$. - -The C standard says these `typedef`s *must* be defined if they can be satisfied. Otherwise they may not exist. - -%%ANKI -Cloze -The {``} library contains {exact-width integer types}. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Suppose `uintN_t` exists. What is its width? -Back: `N` bits. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Suppose `uintN_t` exists. What is its precision? -Back: `N` bits. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Suppose `uintN_t` exists. It is encoded using what binary representation? -Back: Unsigned encoding. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Suppose `intN_t` exists. What is its width? -Back: `N` bits. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Suppose `intN_t` exists. What is its precision? -Back: `N - 1` bits. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Suppose `intN_t` exists. It is encoded using what binary representation? -Back: Two's-complement encoding. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Suppose a platform has a 32-bit signed integer type. Why might `int32_t` not be defined? -Back: The platform may not use two's-complement encoding. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Suppose a platform has a 32-bit signed two's-complement integer type. Why might `int32_t` not be defined? -Back: N/A. The C standard states `int32_t` *must* be defined. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -## Enumerated Types - -An `enum` is a mapping of identifiers with integer values. They have general form: - -```c -enum optional_tag { - type1 ident1; - ... - typeN identN; -} optional_var1 ... optional_varN; -``` - -%%ANKI -Basic -What is the underlined portion of the following declaration called? -```c -enum ___ { ... }; -``` -Back: The tag. -Reference: Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994. - -END%% - -%%ANKI -Basic -What distinguishes the `X`'s from one another in the following? -```c -enum X { ... } X; -``` -Back: The first `X` is a tag whereas the second is a variable with type `enum X`. -Reference: Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994. - -END%% - -%%ANKI -Basic -What distinguishes the `X`'s from one another in the following? -```c -typedef enum X { ... } X; -``` -Back: The first `X` is a tag whereas the second is an alias for type `enum X`. -Reference: Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994. - -END%% - -%%ANKI -Basic -What is the value of `A` in the following? -```c -enum X { A, B, C }; -``` -Back: `0` -Reference: Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994. - -END%% - -%%ANKI -Basic -What is the value of `B` in the following? -```c -enum X { A, B, C }; -``` -Back: `1` -Reference: Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994. - -END%% - -%%ANKI -Basic -What is the value of `B` in the following? -```c -enum X { A=2, B, C }; -``` -Back: `3` -Reference: Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994. - -END%% - -%%ANKI -Basic -According to Linden, what is the "only advantage" of `enum`s over `#define`? -Back: `enum`s can usually be traced in a debugger. -Reference: Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994. - -END%% - -%%ANKI -Basic -Positional values of `enum`s start at what value? -Back: `0` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What name is given to members of an enumeration? -Back: Enumeration constants. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -An enumerated type must be compatible with what three possibilities? -Back: A `char`, a signed integer type, or an unsigned integer type. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -What is the type given to an enumerated type? -Back: N/A. This is implementation-defined. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -What size restriction is enforced on the implementation-defined enumerated type? -Back: It must be capable of representing the values of all the members of the `enum`. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -What kind of integer expressions are enumeration constants limited to? -Back: Integer constant expressions with value representable as an `int`. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -Besides being an ICE, what other condition is expected on enumeration constants? -Back: The ICE evaluates to an integer that fits within an `int`. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -What mechanism(s) are available for defining constants of type `signed int`? -Back: Macros and (possibly) `enum`s. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -%%ANKI -Basic -What mechanism(s) are available for defining constants of type `unsigned int`? -Back: Macros and (possibly) `enum`s. -Reference: “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). - -END%% - -## Bibliography - -* “ISO: Programming Languages - C17,” April 2017, [https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). -* Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). -* Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. [http://www.catb.org/esr/structure-packing/](http://www.catb.org/esr/structure-packing/). -* Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994. diff --git a/notes/linkers/relocatable.md b/notes/linkers/relocatable.md index fe1cb08..5e25f74 100644 --- a/notes/linkers/relocatable.md +++ b/notes/linkers/relocatable.md @@ -856,7 +856,7 @@ END%% %%ANKI Cloze -{1:`R_X86_64_32`} is to {2:absolute} whereas {2:`R_X86_64_PC32`} is to {1:relative}. +{1:`R_X86_64_32`} is to {2:absolute} whereas {2:`R_X86_64_PC32`} is to {1:PC-relative}. Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. Tags: linker::elf x86-64 @@ -882,7 +882,7 @@ END%% %%ANKI Basic -What is the significance of `PC` in type `R_X86_64_PC32`? +What is `PC` an acronym of in type `R_X86_64_PC32`? Back: It is short for **p**rogram **c**ounter. Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. Tags: c17 linker::elf x86-64 diff --git a/notes/set/cardinality.md b/notes/set/cardinality.md index 2271adf..b80f13d 100644 --- a/notes/set/cardinality.md +++ b/notes/set/cardinality.md @@ -1512,6 +1512,46 @@ Reference: Herbert B. Enderton, *Elements of Set Theory* (New York: Academic Pre END%% +%%ANKI +Basic +What does $\mathop{\text{card}}\mathbb{R}$ evaluate to? +Back: $2^{\aleph_0}$ +Reference: Herbert B. Enderton, *Elements of Set Theory* (New York: Academic Press, 1977). + +END%% + +%%ANKI +Basic +$\mathbb{R}$ is equinumerous to the power set of what? +Back: $\omega$ +Reference: Herbert B. Enderton, *Elements of Set Theory* (New York: Academic Press, 1977). + +END%% + +%%ANKI +Basic +$\mathscr{P}(\omega)$ is equinumerous to what other well-known set? +Back: $\mathbb{R}$ +Reference: Herbert B. Enderton, *Elements of Set Theory* (New York: Academic Press, 1977). + +END%% + +%%ANKI +Basic +What does $\mathop{\text{card}}(\mathbb{R} \times \mathbb{R})$ evaluate to? +Back: $2^{\aleph_0}$ +Reference: Herbert B. Enderton, *Elements of Set Theory* (New York: Academic Press, 1977). + +END%% + +%%ANKI +Basic +Expression $2^{\aleph_0} \cdot 2^{\aleph_0}$ corresponds to the cardinality of what set? +Back: $\mathbb{R} \times \mathbb{R}$ +Reference: Herbert B. Enderton, *Elements of Set Theory* (New York: Academic Press, 1977). + +END%% + ### Schröder-Bernstein Theorem For any sets $A$ and $B$, if $A \preceq B$ and $B \preceq A$, then $A \approx B$. @@ -1601,6 +1641,38 @@ Reference: Herbert B. Enderton, *Elements of Set Theory* (New York: Academic Pre END%% +%%ANKI +Basic +Enderton proves $\mathbb{R} \approx \mathscr{P}(\omega)$ by applying the Schröder-Bernstein theorem on what two sets? +Back: $\mathbb{R}$ and $^\omega 2$. +Reference: Herbert B. Enderton, *Elements of Set Theory* (New York: Academic Press, 1977). + +END%% + +%%ANKI +Basic +Suppose $A \subseteq B \subseteq C$ and $A \approx C$. What theorem is used to prove $A \approx B$? +Back: The Schröder-Bernstein theorem. +Reference: Herbert B. Enderton, *Elements of Set Theory* (New York: Academic Press, 1977). + +END%% + +%%ANKI +Basic +Suppose $A \subseteq B \subseteq C$ and $A \approx B$. What theorem is used to prove $B \approx C$? +Back: N/A. This is not necessarily true. +Reference: Herbert B. Enderton, *Elements of Set Theory* (New York: Academic Press, 1977). + +END%% + +%%ANKI +Basic +Suppose $A \subseteq B \subseteq C$ and $C \approx A$. What theorem is used to prove $B \approx C$? +Back: The Schröder-Bernstein theorem. +Reference: Herbert B. Enderton, *Elements of Set Theory* (New York: Academic Press, 1977). + +END%% + ## Hilbert's Hotel Consider a hypothetical hotel with rooms numbered $1$, $2$, $3$, and so on with no upper limit. That is, there is a countably infinite number of rooms in this hotel. Furthermore, it's assumed every room is occupied. diff --git a/notes/trigonometry/index.md b/notes/trigonometry/index.md index 7f936d5..c75fa87 100644 --- a/notes/trigonometry/index.md +++ b/notes/trigonometry/index.md @@ -10,8 +10,6 @@ tags: Trigonometry was originally derived from a Greek word meaning "triangle measuring". It has since been generalized to refer to the study of periodicity. -If the real number $t$ is the directed length of an arc (either positive or negative) measured on the [[unit-circle|unit circle]] $x^2 + y^2 = 1$ (with counterclockwise as the positive direction) with initial point $\langle 1, 0 \rangle$ and terminal point $\langle x, y \rangle$, then the **cosine** of $t$, denoted $\cos(t)$, and **sine** of $t$, denoted $\sin(t)$, are defined to be $$\cos(t) = x \quad\text{and}\quad \sin(t) = y.$$ - %%ANKI Basic Trigonometry was originally the study of what geometric shape? @@ -20,6 +18,8 @@ Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. END%% +## Functions + %%ANKI Basic What are the two most fundamental trigonometric functions? @@ -28,36 +28,6 @@ Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. END%% -%%ANKI -Cloze -The {sine} of $t$ is denoted as {$\sin(t)$}. -Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. - -END%% - -%%ANKI -Cloze -The {cosine} of $t$ is denoted as {$\cos(t)$}. -Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. - -END%% - -%%ANKI -Basic -Map $[0, t]$ to the unit circle. Geometrically, what does $\cos(t)$ correspond to? -Back: The $x$-coordinate of the arc's terminal point. -Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. - -END%% - -%%ANKI -Basic -Map $[0, t]$ to the unit circle. Geometrically, what does $\sin(t)$ correspond to? -Back: The $y$-coordinate of the arc's terminal point. -Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. - -END%% - %%ANKI Cloze The {1:$x$}-coordinate is to {2:$\cos$} whereas the {2:$y$}-coordinate is to {1:$\sin$}. @@ -75,18 +45,38 @@ END%% %%ANKI Basic -What geometric aspect of the unit circle corresponds to the input of the cosine function? -Back: Arc length. +Consider the following arc with length $t$ on the unit circle. With maximum specificity, what is its terminal point? +![[example-arc.png]] +Back: $\langle \cos(t), \sin(t) \rangle$ Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. - + END%% %%ANKI Basic -What geometric aspect of the unit circle corresponds to the output of the cosine function? -Back: The $x$-coordinate of an arc's terminal point. +Why are $\sin$ and $\cos$ called circular functions? +Back: Their values are determined by coordinates of points on the unit circle. Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. - + +END%% + +### Sine + +If the real number $t$ is the directed length of an arc (either positive or negative) measured on the [[unit-circle|unit circle]] $x^2 + y^2 = 1$ (with counterclockwise as the positive direction) with initial point $\langle 1, 0 \rangle$ and terminal point $\langle x, y \rangle$, then the **sine** of $t$, denoted $\sin(t)$ is defined to be $$\sin(t) = y.$$ + +%%ANKI +Cloze +The {sine} of $t$ is denoted as {$\sin(t)$}. +Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. + +END%% + +%%ANKI +Basic +Map $[0, t]$ to the unit circle. Geometrically, what does $\sin(t)$ correspond to? +Back: The $y$-coordinate of the arc's terminal point. +Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. + END%% %%ANKI @@ -105,15 +95,6 @@ Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. END%% -%%ANKI -Basic -Consider the following arc with length $t$ on the unit circle. What is the terminal point's $x$-coordinate? -![[example-arc.png]] -Back: $\cos(t)$ -Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. - -END%% - %%ANKI Basic Consider the following arc with length $t$ on the unit circle. What is the terminal point's $y$-coordinate? @@ -123,47 +104,6 @@ Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. END%% -%%ANKI -Basic -Consider the following arc with length $t$ on the unit circle. With maximum specificity, what is its terminal point? -![[example-arc.png]] -Back: $\langle \cos(t), \sin(t) \rangle$ -Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. - -END%% - -%%ANKI -Basic -What does $\cos(0)$ evaluate to? -Back: $1$ -Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. - -END%% - -%%ANKI -Basic -What does $\cos\left(\frac{\pi}{2}\right)$ evaluate to? -Back: $0$ -Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. - -END%% - -%%ANKI -Basic -What does $\cos\left(-\frac{\pi}{2}\right)$ evaluate to? -Back: $0$ -Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. - -END%% - -%%ANKI -Basic -What does $\cos\left(\pi\right)$ evaluate to? -Back: $-1$ -Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. - -END%% - %%ANKI Basic What does $\sin(2\pi)$ evaluate to? @@ -198,10 +138,165 @@ END%% %%ANKI Basic -Why are $\sin$ and $\cos$ called circular functions? -Back: Their values are determined by coordinates of points on the unit circle. +What is the domain of $\sin$? +Back: $\mathbb{R}$ Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. - + +END%% + +%%ANKI +Basic +What is the range of $\sin$? +Back: $[-1, 1]$ +Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. + +END%% + +%%ANKI +Basic +Let $t$ be the length of an arc with initial point $\langle 1, 0 \rangle$ and terminal point in quadrant I. What is the sign of $\sin(t)$? +Back: $\sin(t) > 0$ +Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. + +END%% + +%%ANKI +Basic +Let $t$ be the length of an arc with initial point $\langle 1, 0 \rangle$ and terminal point in quadrant II. What is the sign of $\sin(t)$? +Back: $\sin(t) > 0$ +Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. + +END%% + +%%ANKI +Basic +Let $t$ be the length of an arc with initial point $\langle 1, 0 \rangle$ and terminal point in quadrant III. What is the sign of $\sin(t)$? +Back: $\sin(t) < 0$ +Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. + +END%% + +%%ANKI +Basic +Let $t$ be the length of an arc with initial point $\langle 1, 0 \rangle$ and terminal point in quadrant IV. What is the sign of $\sin(t)$? +Back: $\sin(t) < 0$ +Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. + +END%% + +%%ANKI +Basic +Let $t$ be the length of an arc with initial point $\langle 1, 0 \rangle$. When is $\sin(t) > 0$? +Back: When the terminal point of the arc is in quadrant I or II. +Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. + +END%% + +%%ANKI +Basic +Let $t$ be the length of an arc with initial point $\langle 1, 0 \rangle$. When is $\sin(t) < 0$? +Back: When the terminal point of the arc is in quadrant III or IV. +Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. + +END%% + +%%ANKI +Basic +Let $\frac{\pi}{2} < t < \pi$. What is the sign of $\sin(t)$? +Back: Positive. +Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. + +END%% + +%%ANKI +Basic +Let $\frac{3\pi}{2} < t < 2\pi$. What is the sign of $\sin(t)$? +Back: Negative. +Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. + +END%% + +%%ANKI +Cloze +For any real number $t$, {$\sin^2(t)$} is alternatively denoted as {$(\sin(t))^2$}. +Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. + +END%% + +### Cosine + +If the real number $t$ is the directed length of an arc (either positive or negative) measured on the [[unit-circle|unit circle]] $x^2 + y^2 = 1$ (with counterclockwise as the positive direction) with initial point $\langle 1, 0 \rangle$ and terminal point $\langle x, y \rangle$, then the **cosine** of $t$, denoted $\cos(t)$, is defined to be $$\cos(t) = x.$$ + +%%ANKI +Cloze +The {cosine} of $t$ is denoted as {$\cos(t)$}. +Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. + +END%% + +%%ANKI +Basic +Map $[0, t]$ to the unit circle. Geometrically, what does $\cos(t)$ correspond to? +Back: The $x$-coordinate of the arc's terminal point. +Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. + +END%% + +%%ANKI +Basic +What geometric aspect of the unit circle corresponds to the input of the cosine function? +Back: Arc length. +Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. + +END%% + +%%ANKI +Basic +What geometric aspect of the unit circle corresponds to the output of the cosine function? +Back: The $x$-coordinate of an arc's terminal point. +Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. + +END%% + +%%ANKI +Basic +Consider the following arc with length $t$ on the unit circle. What is the terminal point's $x$-coordinate? +![[example-arc.png]] +Back: $\cos(t)$ +Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. + +END%% + +%%ANKI +Basic +What does $\cos(0)$ evaluate to? +Back: $1$ +Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. + +END%% + +%%ANKI +Basic +What does $\cos\left(\frac{\pi}{2}\right)$ evaluate to? +Back: $0$ +Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. + +END%% + +%%ANKI +Basic +What does $\cos\left(-\frac{\pi}{2}\right)$ evaluate to? +Back: $0$ +Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. + +END%% + +%%ANKI +Basic +What does $\cos\left(\pi\right)$ evaluate to? +Back: $-1$ +Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. + END%% %%ANKI @@ -222,18 +317,111 @@ END%% %%ANKI Basic -What is the domain of $\sin$? -Back: $\mathbb{R}$ +Let $t$ be the length of an arc with initial point $\langle 1, 0 \rangle$ and terminal point in quadrant I. What is the sign of $\cos(t)$? +Back: $\cos(t) > 0$ Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. - + END%% %%ANKI Basic -What is the range of $\sin$? -Back: $[-1, 1]$ +Let $t$ be the length of an arc with initial point $\langle 1, 0 \rangle$ and terminal point in quadrant II. What is the sign of $\cos(t)$? +Back: $\cos(t) < 0$ Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. - + +END%% + +%%ANKI +Basic +Let $t$ be the length of an arc with initial point $\langle 1, 0 \rangle$ and terminal point in quadrant III. What is the sign of $\cos(t)$? +Back: $\cos(t) < 0$ +Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. + +END%% + +%%ANKI +Basic +Let $t$ be the length of an arc with initial point $\langle 1, 0 \rangle$ and terminal point in quadrant IV. What is the sign of $\cos(t)$? +Back: $\cos(t) < 0$ +Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. + +END%% + +%%ANKI +Basic +Let $t$ be the length of an arc with initial point $\langle 1, 0 \rangle$. When is $\cos(t) > 0$? +Back: When the terminal point of the arc is in quadrant I or IV. +Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. + +END%% + +%%ANKI +Basic +Let $t$ be the length of an arc with initial point $\langle 1, 0 \rangle$. When is $\cos(t) < 0$? +Back: When the terminal point of the arc is in quadrant II or III. +Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. + +END%% + +%%ANKI +Basic +Let $\frac{\pi}{2} < t < \pi$. What is the sign of $\cos(t)$? +Back: Negative. +Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. + +END%% + +%%ANKI +Basic +Let $\frac{3\pi}{2} < t < 2\pi$. What is the sign of $\cos(t)$? +Back: Positive. +Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. + +END%% + +%%ANKI +Cloze +For any real number $t$, {$\cos^2(t)$} is alternatively denoted as {$(\cos(t))^2$}. +Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. + +END%% + +## Identities + +### Pythagorean Identity + +For any real number $t$, $$\cos^2(t) + \sin^2(t) = 1.$$ + +%%ANKI +Basic +Which trigonometric identity is often considered the most important? +Back: The Pythagorean Identity. +Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. + +END%% + +%%ANKI +Basic +What does the Pytagorean Identity state? +Back: For any real number $t$, $\cos^2(t) + \sin^2(t) = 1$. +Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. + +END%% + +%%ANKI +Basic +What shape is used to derive the Pythagorean Identity? +Back: The unit circle. +Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. + +END%% + +%%ANKI +Basic +How is the Pythagorean Identity derived? +Back: By applying the Pythagorean Theorem on point $\langle \cos(t), \sin(t) \rangle$ on the unit circle. +Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. + END%% ## Bibliography diff --git a/notes/trigonometry/unit-circle.md b/notes/trigonometry/unit-circle.md index 5717f23..2a23e8e 100644 --- a/notes/trigonometry/unit-circle.md +++ b/notes/trigonometry/unit-circle.md @@ -203,7 +203,7 @@ END%% %%ANKI Basic -What arc describes the portion of a unit circle found in the first quadrant? +What interval maps to the portion of a unit circle found in the first quadrant? Back: $\left(0, \frac{\pi}{2}\right)$ Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. @@ -211,7 +211,7 @@ END%% %%ANKI Basic -What arc describes the portion of a unit circle found in the third quadrant? +What interval maps to the portion of a unit circle found in the third quadrant? Back: $\left(\pi, \frac{3\pi}{2}\right)$ Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. @@ -219,7 +219,7 @@ END%% %%ANKI Basic -What arc describes the portion of a unit circle found in the fourth quadrant? +What interval maps to the portion of a unit circle found in the fourth quadrant? Back: $\left(\frac{3\pi}{2}, 2\pi\right)$ Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. @@ -227,7 +227,7 @@ END%% %%ANKI Basic -What arc describes the portion of a unit circle found in the second quadrant? +What interval maps to the portion of a unit circle found in the second quadrant? Back: $\left(\frac{\pi}{2}, \pi\right)$ Reference: Ted Sundstrom and Steven Schlicker, _Trigonometry_, 2024. diff --git a/notes/x86-64/procedures.md b/notes/x86-64/procedures.md index 3265e0b..a94b78b 100644 --- a/notes/x86-64/procedures.md +++ b/notes/x86-64/procedures.md @@ -562,7 +562,7 @@ END%% %%ANKI Basic How many registers are designated as callee-saved? -Back: 6. +Back: Six. Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. END%%