diff --git a/notes/.obsidian/plugins/obsidian-to-anki-plugin/data.json b/notes/.obsidian/plugins/obsidian-to-anki-plugin/data.json index 7cd0e1c..72b2964 100644 --- a/notes/.obsidian/plugins/obsidian-to-anki-plugin/data.json +++ b/notes/.obsidian/plugins/obsidian-to-anki-plugin/data.json @@ -985,7 +985,7 @@ "c17/linkage.md": "1e4f60f07619b9cbf2f71f11aae9e5f4", "_journal/2024-11-30.md": "c6040ba2caf1c9ff55faed25d7879e9a", "_journal/2024-11/2024-11-29.md": "4896d849b6345010669047323521e21a", - "_journal/2024-11/2024-11-28.md": "e9d252bc4bc3d6f70db56c36e1090e86", + "_journal/2024-11/2024-11-28.md": "dd843bc11263a1dca3c9e27ab4fd8c68", "_journal/2024-11/2024-11-27.md": "7f44f4c91fea0c5c05d542aeeb9e723d", "_journal/2024-12-01.md": "d34aff07b7fa8a9a05f84841496d13b5", "_journal/2024-11/2024-11-30.md": "c6040ba2caf1c9ff55faed25d7879e9a", @@ -1005,7 +1005,7 @@ "_journal/2024-12-08.md": "5662897539b222db1af45dcd217f0796", "_journal/2024-12/2024-12-07.md": "bfb6c4db0acbacba19f03a04ec29fa5c", "linkers/static.md": "cc56ddfc33f605d26b954ec242abc4cf", - "linkers/index.md": "73b07789a48be8a611941d50ae4f3831", + "linkers/index.md": "18d4a03aa1a94418d450a5107e7c883d", "_journal/2024-12-09.md": "8988f0e8f0060f4b86d17e0bc4e7ff7e", "_journal/2024-12/2024-12-08.md": "5662897539b222db1af45dcd217f0796", "_journal/2024-12-10.md": "c12d380d24d7d1dc2e74a57a1b79399e", @@ -1024,7 +1024,10 @@ "_journal/2024-12/2024-12-12.md": "59e71caa4e9ebdb11a7c7549c33bed20", "linkers/object-files.md": "77767f310330b8650a5023dd0522226c", "_journal/2024-12-16.md": "77c27920d5b44b6315c2ea22b990eefc", - "_journal/2024-12/2024-12-15.md": "be66c8808d8bb66d4e7b91db7c93c94a" + "_journal/2024-12/2024-12-15.md": "be66c8808d8bb66d4e7b91db7c93c94a", + "linkers/elf.md": "83a27a7d71ca26f8f034770a31d58fa0", + "c17/strings/printf.md": "8b67cfbccaf35dd9488b73e7e5555405", + "c17/strings/index.md": "3fa6f42967f3cc786740bb8537c62682" }, "fields_dict": { "Basic": [ diff --git a/notes/_journal/2024-11/2024-11-28.md b/notes/_journal/2024-11/2024-11-28.md index bc63444..c574067 100644 --- a/notes/_journal/2024-11/2024-11-28.md +++ b/notes/_journal/2024-11/2024-11-28.md @@ -8,4 +8,4 @@ title: "2024-11-28" - [ ] Sheet Music (10 min.) - [ ] Korean (Read 1 Story) -* Notes on [[strings#Copying Functions|memcpy and memmove]]. \ No newline at end of file +* Notes on [[c17/strings/index#Copying Functions|memcpy and memmove]]. \ No newline at end of file diff --git a/notes/_journal/2024-12-16.md b/notes/_journal/2024-12-16.md index 15c3faa..898a5e7 100644 --- a/notes/_journal/2024-12-16.md +++ b/notes/_journal/2024-12-16.md @@ -6,4 +6,6 @@ title: "2024-12-16" - [x] KoL - [ ] OGS - [ ] Sheet Music (10 min.) -- [ ] Korean (Read 1 Story) \ No newline at end of file +- [ ] Korean (Read 1 Story) + +* Notes on sections found within an [[elf|ELF]] file. \ No newline at end of file diff --git a/notes/c17/strings/index.md b/notes/c17/strings/index.md new file mode 100644 index 0000000..a8598bb --- /dev/null +++ b/notes/c17/strings/index.md @@ -0,0 +1,357 @@ +--- +title: Strings +TARGET DECK: Obsidian::STEM +FILE TAGS: c17 +tags: + - c17 +--- + +## Overview + +A contiguous sequence of characters terminated by the `NUL` character (refer to [[ascii|ASCII]]). Text data is said to be more platform-independent than [[endianness|binary]] data since it is unaffected by word size or byte ordering. + +%%ANKI +Basic +What is a C-style string? +Back: A character array terminated with a `NUL` character. +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 character terminates all C-style strings? +Back: `NUL` +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 is the decimal value of `NUL` in ASCII encoding? +Back: `0` +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 +Text is more platform-independent than e.g. integer encodings because it is unaffected by what two properties? +Back: Word size and byte ordering. +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 kind of array can be expressed as a literal? +Back: Strings. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Cloze +A {string} is a {`NUL`}-terminated array of {`char`}. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Cloze +`` uses prefix {`mem`} to refer to {array} arguments. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Cloze +`` uses prefix {`str`} to refer to {string} arguments. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +## Escape Sequences + +C has a standard for processing different escape sequences. Many languages built with C in mind parse these escape sequences in a similar way. + +* `\ooo`: Consists of one to three octal digits. + * [[bash/index|Bash]] supports this sequence as `$'\ooo'`. + +%%ANKI +Basic +How are C escape sequences for octal digits denoted? +Back: As `\ooo`. +Reference: Brian W. Kernighan and Dennis M. Ritchie, *The C Programming Language*, 2nd ed (Englewood Cliffs, N.J: Prentice Hall, 1988). + +END%% + +%%ANKI +Basic +In C, `\ooo` allows specifying how many octal digits? +Back: One to three. +Reference: Brian W. Kernighan and Dennis M. Ritchie, *The C Programming Language*, 2nd ed (Englewood Cliffs, N.J: Prentice Hall, 1988). + +END%% + +%%ANKI +Basic +How are C escape sequences exposed in bash? +Back: Using ANSI-C quoting, i.e. `$$'string'`. +Reference: Mendel Cooper, “Advanced Bash-Scripting Guide,” n.d., 916. +Tags: bash + +END%% + +* `\xhh`: Consists of one or more [[radices#Hexadecimal|hexadecimal]] digits. The `x` prefix is required to distinguish from octal escape sequences. + * [[bash/index|Bash]] supports this sequence as `$'\xhh'`. One or two digits is supported. + +%%ANKI +Basic +How are C escape sequences for hexadecimal digits denoted? +Back: As `\xhh`. +Reference: Brian W. Kernighan and Dennis M. Ritchie, *The C Programming Language*, 2nd ed (Englewood Cliffs, N.J: Prentice Hall, 1988). + +END%% + +%%ANKI +Basic +In C, `\x` allows specifying how many hexadecimal digits? +Back: One or more. +Reference: Brian W. Kernighan and Dennis M. Ritchie, *The C Programming Language*, 2nd ed (Englewood Cliffs, N.J: Prentice Hall, 1988). + +END%% + +%%ANKI +Basic +What footgun does C's `\x` sequence expose? +Back: Using more than two hexadecimal digits can produce undefined results. +Reference: Arnold D. Robbins, “GAWK: Effective AWK Programming,” October 2023, [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf). + +END%% + +* `\uhhhh`: Introduced in C11 to represent Unicode code points. *Must* have exactly four hexadecimal characters specified with `0` leading padding if necessary. + * [[bash/index|Bash]] supports this sequence as `$'uhhhh'`. One to four hex digits is supported. + +%%ANKI +Basic +What two ways are C escape sequences for unicode denoted? +Back: As `\uhhhh` or `\Uhhhhhhhh`. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). +Tags: encoding::unicode + +END%% + +%%ANKI +Basic +In C, `\u` allows specifying how many hexadecimal digits? +Back: Exactly four. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). +Tags: encoding::unicode + +END%% + +%%ANKI +Basic +In what standard were C's `\u` and `\U` escape sequences introduced? +Back: C11. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). +Tags: encoding::unicode + +END%% + +* `\Uhhhhhhhh`: Introduced in C11 to represent larger unicode code points. *Must* have exactly eight hexadecimal characters specified with `0` leading padding if necessary. + +%%ANKI +Basic +In C, `\U` allows specifying how many hexadecimal digits? +Back: Exactly eight. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). +Tags: encoding::unicode + +END%% + +%%ANKI +Basic +Why does C have both `\u` and `\U`? +Back: `\U` accommodates for larger code point values. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). +Tags: encoding::unicode + +END%% + +## Copying Functions + +The two primary functions used for copying memory are `memcpy` and `memmove`: + +```c +void* memcpy(void* restrict s1, const void* restrict s2, size_t n); +void* memmove(void* s1, const void* s2, size_t n); +``` + +%%ANKI +Basic +What are the two primary functions provided by the standard library for copying memory? +Back: `memcpy` and `memmove`. +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 function prototype of `memcpy`? +Back: +```c +void* memcpy(void* restrict s1, const void* restrict s2, size_t n); +``` +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 `memcpy` function do? +Back: Copies a specified number of bytes from one object to another. +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 +Disregarding out of bounds errors, when does `memcpy` invoke undefined behavior? +Back: When copying occurs between objects that overlap in memory. +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 first argument of `memcpy` refer to? +Back: The destination address bytes are copied to. +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 second argument of `memcpy` refer to? +Back: The source address bytes are copied from. +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 third argument of `memcpy` refer to? +Back: The number of bytes to copy from the source address. +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 `memcpy` or `memmove` is faster? +Back: `memcpy` +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 `memmove` slower than `memcpy`? +Back: It's implemented as if copying first into a temporary array that does not overlap the arguments in memory. +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 should you prefer using `memcpy` instead of `memmove`? +Back: When the copying occurs between objects that do not overlap in memory. +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 standard header library defines `memcpy`? +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 is the function prototype of `memmove`? +Back: +```c +void* memmove(void* s1, const void* s2, size_t n); +``` +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 first argument of `memmove` refer to? +Back: The destination address bytes are copied to. +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 second argument of `memmove` refer to? +Back: The source address bytes are copied from. +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 third argument of `memmove` refer to? +Back: The number of bytes to copy from the source address. +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 +Disregarding out of bounds errors, when does `memmove` invoke undefined behavior? +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 +When should you prefer using `memmove` instead of `memcopy`? +Back: When the copying occurs between objects that overlap in memory. +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 standard header library defines `memmove`? +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 does the `memmove` function do? +Back: Copies a specified number of bytes from one object to another. +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 + +* Arnold D. Robbins, “GAWK: Effective AWK Programming,” October 2023, [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf). +* Brian W. Kernighan and Dennis M. Ritchie, *The C Programming Language*, 2nd ed (Englewood Cliffs, N.J: Prentice Hall, 1988). +* 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). +* Mendel Cooper, “Advanced Bash-Scripting Guide,” n.d., 916. diff --git a/notes/c17/strings.md b/notes/c17/strings/printf.md similarity index 70% rename from notes/c17/strings.md rename to notes/c17/strings/printf.md index dd771c8..76d0e8c 100644 --- a/notes/c17/strings.md +++ b/notes/c17/strings/printf.md @@ -1,78 +1,14 @@ --- -title: Strings +title: printf TARGET DECK: Obsidian::STEM -FILE TAGS: c17 +FILE TAGS: c17::printf tags: - c17 + - printf --- ## Overview -A contiguous sequence of characters terminated by the `NUL` character (refer to [[ascii|ASCII]]). Text data is said to be more platform-independent than [[endianness|binary]] data since it is unaffected by word size or byte ordering. - -%%ANKI -Basic -What is a C-style string? -Back: A character array terminated with a `NUL` character. -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 character terminates all C-style strings? -Back: `NUL` -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 is the decimal value of `NUL` in ASCII encoding? -Back: `0` -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 -Text is more platform-independent than e.g. integer encodings because it is unaffected by what two properties? -Back: Word size and byte ordering. -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 kind of array can be expressed as a literal? -Back: Strings. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Cloze -A {string} is a {`NUL`}-terminated array of {`char`}. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Cloze -`` uses prefix {`mem`} to refer to {array} arguments. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Cloze -`` uses prefix {`str`} to refer to {string} arguments. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -## `printf` - The syntax for the format placeholder is `%[flags][width][.precision][length]specifier`. %%ANKI @@ -80,7 +16,6 @@ Basic What four optional parts make up a `printf` argument? Back: Flags, width, precision, and length. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -89,7 +24,6 @@ Basic What is the purpose of the width field in a `printf` argument? Back: It specifies a minimum number of characters to ouput. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -98,7 +32,6 @@ Basic What is the purpose of the precision field in a `printf` argument? Back: It specifies the maximum limit on the output, depending on the argument type. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -107,7 +40,6 @@ Basic What is the purpose of the length field in a `printf` argument? Back: It specifies the size of the `printf` argument before default promotion. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -116,7 +48,6 @@ Basic Which header file contains basic `printf` functionality? Back: `` Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -124,7 +55,6 @@ END%% Cloze {1:`width`} specifies a {2:minimum} while {2:`precision`} specifies a {1:maximum}. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -132,11 +62,10 @@ END%% Cloze The {1:`width`} and {1:`precision`} fields are output related whereas the {2:`length`} field is input related. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% -### Flags +## Flags | Flag | Description | | ---- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | @@ -150,7 +79,6 @@ END%% Cloze The {`-`} flag {left-aligns} the output. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -159,7 +87,6 @@ Basic What is the output of `printf("%-2d abc", 100)`? Back: `100␣abc` Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -168,7 +95,6 @@ Basic What is the output of `printf("%-4d abc", 100)`? Back: `100␣␣abc` Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -177,7 +103,6 @@ Basic What is the output of `printf("%.4d abc", 100)`? Back: `0100␣abc` Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -186,7 +111,6 @@ Basic What is the output of `printf("%04d abc", 100)`? Back: `0100␣abc` Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -195,7 +119,6 @@ Basic What is the output of `printf("%.4s abc", "efg")`? Back: `efg␣abc` Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -204,7 +127,6 @@ Basic What is the output of `printf("%.2s abc", "efg")`? Back: `ef␣abc` Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -213,7 +135,6 @@ Basic What is the output of `printf("%.2f abc", 0.01234)`? Back: `0.01␣abc` Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -222,7 +143,6 @@ Basic How does the precision field affect `"%s"` parameters? Back: Strings are truncated if longer than the specified precision. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -231,7 +151,6 @@ Basic How does the precision field affect `"%f"` parameters? Back: Decimal values are truncated if longer than the specified precision. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -240,7 +159,6 @@ Basic What does a negative width field value indicate? Back: This is actually a positive width with a `-` (left-align) flag. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -249,7 +167,6 @@ Basic The `-` `printf` flag overrides what default behavior? Back: Output is right-aligned by default. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -257,7 +174,6 @@ END%% Cloze The {`+`} `printf` flag {prepends a plus for positive signed-numeric types}. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -266,7 +182,6 @@ Basic The `+` `printf` flag overrides what default behavior? Back: Nothing is prepended to positive signed-numeric types by default. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -274,7 +189,6 @@ END%% Cloze The {`␣`} `printf` flag {prepends a space for positive signed-numeric types}. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -283,7 +197,6 @@ Basic The `␣` `printf` flag overrides what default behavior? Back: Nothing is prepended to positive signed-numeric types by default. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -292,7 +205,6 @@ Basic How do the `+` and `␣` `printf` flags relate to one another? Back: Both prepend a character to positively signed-numeric types. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -301,7 +213,6 @@ Basic What happens if both the `+` and `␣` `printf` flags are specified? Back: The `+` flag takes precedence. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -309,7 +220,6 @@ END%% Cloze The {`0`} `printf` flag {prepends zeros for numeric types}. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -318,7 +228,6 @@ Basic What option must be specified for the `0` `printf` flag to take effect? Back: The "width" option. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -327,7 +236,6 @@ Basic The `0` `printf` flag overrides what default behavior? Back: Spaces are used to match the "width" option by default. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -336,7 +244,6 @@ Basic How is `%#g` different from `%g`? Back: The former always includes a decimal point and may include trailing `0`s. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -345,7 +252,6 @@ Basic How is `%#f` different from `%f`? Back: The former always includes a decimal point. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -354,7 +260,6 @@ Basic How is `%#e` different from `%e`? Back: The former always includes a decimal point. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -363,7 +268,6 @@ Basic Which `printf` flag can be used to ensure decimal points in the output of floating-point types? Back: `#` Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -372,7 +276,6 @@ Basic How is `%#o` different from `%o`? Back: The former prepends a `0` to the output. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -381,7 +284,6 @@ Basic How is `%#x` different from `%x`? Back: The former prepends a `0x` to the output. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -390,7 +292,6 @@ Basic How is `%#X` different from `%X`? Back: The former prepends a `0X` to the output. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -398,11 +299,10 @@ END%% Cloze `%#o` is to {`0`} as `%#x` is to {`0x`}. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% -### Length +## Length Length | Description --------- | ----------- @@ -417,7 +317,6 @@ Basic *Why* do length modifiers for e.g. `char` exist? Back: For maximum portability in the face of default argument promotions. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -425,7 +324,6 @@ END%% Cloze The {`hh`} length corresponds to the {`char`} type declaration. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -433,7 +331,6 @@ END%% Cloze The {`h`} length corresponds to the {`short`} type declaration. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -441,7 +338,6 @@ END%% Cloze The {`l`} length corresponds to the {`long`} type declaration. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -449,7 +345,6 @@ END%% Cloze The {`ll`} length corresponds to the {`long long`} type declaration. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -457,11 +352,10 @@ END%% Cloze The {`z`} length corresponds to the {`size_t`} type declaration. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% -### Specifiers +## Specifiers Specifier | Description --------- | ----------- @@ -481,7 +375,6 @@ Basic What character do `printf` format specifiers start with? Back: `%` Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -490,7 +383,6 @@ Basic Why is `printf` named the way it is? Back: It stands for **print f**ormatted. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -498,7 +390,6 @@ END%% Cloze The {`%d` and `%i`} format specifers work on a {decimal `signed int`}. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -506,7 +397,6 @@ END%% Cloze {`%d` and `%i`} are to `signed` as {`%u`} is to `unsigned`. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -514,7 +404,6 @@ END%% Cloze The {`%u`} format specifier outputs a {decimal `unsigned int`}. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -523,7 +412,6 @@ Basic What distinguishes format specifiers `%d` and `%i`? Back: Nothing. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -532,7 +420,6 @@ Basic Which format specifiers were probably used to yield `printf` output `-12`? Back: `%d` or `%i` Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -541,7 +428,6 @@ Basic What distinguishes format specifiers `%d` and `%u`? Back: The former is for signed integers, the latter unsigned. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -549,7 +435,6 @@ END%% Cloze The {`%x`} format specifier outputs a {lowercase hexadecimal `unsigned int`}. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -558,7 +443,6 @@ Basic Which format specifier was probably used to yield `printf` output `7af`? Back: `%x` Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -566,7 +450,6 @@ END%% Cloze The {`%X`} format specifier outputs an {uppercase hexadecimal `unsigned int`}. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -575,7 +458,6 @@ Basic Which format specifier was probably used to yield `printf` output `7AF`? Back: `%X` Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -584,7 +466,6 @@ Basic What distinguishes format specifiers `%x` and `%X`? Back: The former outputs lowercase hex digits, the latter uppercase. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -592,7 +473,6 @@ END%% Cloze The {`%f`} format specifier outputs a {lowercase fixed-point `double`}. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -600,7 +480,6 @@ END%% Cloze The {`%F`} format specifier outputs an {uppercase fixed-point `double`}. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -609,7 +488,6 @@ Basic What distinguishes format specifiers `%f` and `%F`? Back: The former outputs lowercase identifiers, the latter uppercase. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -618,7 +496,6 @@ Basic What three special identifiers might specifier `%f` output? Back: `inf`, `infinity`, and `nan` Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -627,7 +504,6 @@ Basic Assuming round-to-even, what is the output of `printf("%.0f", 3.5)`? Back: `4` Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. -Tags: printf END%% @@ -636,7 +512,6 @@ Basic Assuming round-to-even, what is the output of `printf("%.0f", 2.5)`? Back: `2` Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. -Tags: printf END%% @@ -645,7 +520,6 @@ Basic How does the C standard define the rounding mode of floating-point specifiers? Back: This is implementation specific. Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. -Tags: printf END%% @@ -654,7 +528,6 @@ Basic What does the rounding mode of floating-point specifiers refer to? Back: How numbers with greater than the specified precision are output. Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. -Tags: printf END%% @@ -663,7 +536,6 @@ Basic What three special identifiers might specifier `%F` output? Back: `INF`, `INFINITY`, and `NAN` Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -672,7 +544,6 @@ Basic Which format specifier was probably used to yield `printf` output `inf`? Back: `%f` Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -680,7 +551,6 @@ END%% Cloze {1:Fixed-point} notation is to {2:`%f`} whereas {2:standard/exponential} notation is to {1:`%e`}. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -689,7 +559,6 @@ Basic Which format specifier was probably used to yield `printf` output `172.345000`? Back: `%f` Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -698,7 +567,6 @@ Basic What term describes the kind of output notation corresponding to `%f`? Back: Fixed-point notation. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -707,7 +575,6 @@ Basic What distinguishes format specifiers `%e` and `%E`? Back: The former outputs an `e`, the latter an `E`. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -716,7 +583,6 @@ Basic How many digits follow `e` in the output of `printf` specifier `%e`? Back: At least `2`. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -725,7 +591,6 @@ Basic Which format specifiers correspond to scientific notation? Back: `%e` and `%E` Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -734,7 +599,6 @@ Basic Which format specifier was probably used to yield `printf` output `1.723450e+02`? Back: `%e` Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -743,7 +607,6 @@ Basic What is the default precision of `%f`? Back: `6` Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -752,7 +615,6 @@ Basic What is the output of `printf("%e", 3.14)`? Back: `3.140000e00` Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -761,7 +623,6 @@ Basic What is the output of `printf("%e", 314)`? Back: `3.140000e02` Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -770,7 +631,6 @@ Basic What term describes the kind of output notation corresponding to `%e`? Back: Standard or exponential notation. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -779,7 +639,6 @@ Basic What is the default precision of `%e`? Back: `6` Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -787,7 +646,6 @@ END%% Cloze The {`%g`} format specifier outputs a {lowercase `double` in fixed-point or standard notation}. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -795,7 +653,6 @@ END%% Cloze The {`%G`} format specifier outputs an {uppercase `double` in fixed-point or standard notation}. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -804,7 +661,6 @@ Basic The `%g` format specifier subsumes functionality of what other format specifiers? Back: `%f` and `%e` Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -813,7 +669,6 @@ Basic The `%G` format specifier subsumes functionality of what other format specifiers? Back: `%F` and `%E` Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -822,7 +677,6 @@ Basic How does `%g` handle integral values differently from `%f`? Back: It excludes a trailing `.` and insignificant `0`s. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -831,7 +685,6 @@ Basic How does `%g` handle non-integral values differently from `%f`? Back: It excludes insignifant `0`s after the decimal point. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -840,7 +693,6 @@ Basic What distinguishes `%g` from `%G`? Back: The former uses lowercase letters. The latter uses uppercase letters. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -849,7 +701,6 @@ Basic What is the output of `printf("%.1f", 2.0)`? Back: `2.0` Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -858,7 +709,6 @@ Basic Assuming fixed-point notation, what is the output of `printf("%.1g", 2.0)`? Back: `2` Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -867,7 +717,6 @@ Basic Assuming fixed-point notation, what is the output of `printf("%g", 3.14)`? Back: `3.14` Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -876,7 +725,6 @@ Basic Assuming fixed-point notation, what is the output of `printf("%g", 3)`? Back: `3` Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -885,7 +733,6 @@ Basic What is the output of `printf("%f", 3)`? Back: `3.000000` Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -893,7 +740,6 @@ END%% Cloze The {`%o`} format specifier outputs an {octal `unsigned int`}. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -902,7 +748,6 @@ Basic Why doesn't the `%o` format specifier have a corresponding `%O` specifier? Back: There is no distinction between lower and uppercase octal digits. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -910,7 +755,6 @@ END%% Cloze The {`%s`} format specifiers outputs a {`NUL`-terminated string}. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -919,7 +763,6 @@ Basic Which format specifier was probably used to yield `printf` output `abc`? Back: `%s` Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -927,7 +770,6 @@ END%% Cloze The {`%c`} format specifier outputs a {`char` character}. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -936,7 +778,6 @@ Basic Which format specifier was probably used to yield `printf` output `a`? Back: `%c` Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -944,7 +785,6 @@ END%% Cloze The {`%p`} format specifier outputs a {`void*` address}. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -953,7 +793,6 @@ Basic Which format specifier was probably used to yield `printf` output `0b80000000`? Back: `%p` Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -962,7 +801,6 @@ Basic How is the address outputted by the `%p` format specifier written? Back: In an implementation-specific way. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -971,7 +809,6 @@ Basic What should be done to a pointer outputted by the `%p` format specifier? Back: It should be cast to a `void*` pointer. Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). -Tags: printf END%% @@ -980,7 +817,6 @@ Basic Given `int64_t x`, why is `printf("%d", x)` a problem? Back: `%d` matches an `int` which is not necessarily 64-bits. Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. -Tags: printf END%% @@ -989,7 +825,6 @@ Basic What must you use when invoking `printf` with a fixed-width integer type? Back: `printf`-specific macros. Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. -Tags: printf END%% @@ -998,7 +833,6 @@ Basic What is `PRId32` an example macro for? Back: A macro that expands to the correct specifier for a 32-bit signed integral type. Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. -Tags: printf END%% @@ -1006,7 +840,6 @@ END%% Cloze {`PRId32`} is to signed whereas {`PRIu32`} is to unsigned. Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -1015,7 +848,6 @@ Basic Which C header specifies `printf` macros for fixed-width integral types? Back: `` Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). -Tags: printf END%% @@ -1024,7 +856,6 @@ Basic Given `int32_t x`, how might we invoke `printf` on it? Back: `printf("%" PRId32, x)` Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. -Tags: printf END%% @@ -1033,292 +864,11 @@ Basic What prefix do `printf` macros from `` share? Back: `PRI` Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. -Tags: printf END%% -## Escape Sequences - -C has a standard for processing different escape sequences. Many languages built with C in mind parse these escape sequences in a similar way. - -* `\ooo`: Consists of one to three octal digits. - * [[bash/index|Bash]] supports this sequence as `$'\ooo'`. - -%%ANKI -Basic -How are C escape sequences for octal digits denoted? -Back: As `\ooo`. -Reference: Brian W. Kernighan and Dennis M. Ritchie, *The C Programming Language*, 2nd ed (Englewood Cliffs, N.J: Prentice Hall, 1988). - -END%% - -%%ANKI -Basic -In C, `\ooo` allows specifying how many octal digits? -Back: One to three. -Reference: Brian W. Kernighan and Dennis M. Ritchie, *The C Programming Language*, 2nd ed (Englewood Cliffs, N.J: Prentice Hall, 1988). - -END%% - -%%ANKI -Basic -How are C escape sequences exposed in bash? -Back: Using ANSI-C quoting, i.e. `$$'string'`. -Reference: Mendel Cooper, “Advanced Bash-Scripting Guide,” n.d., 916. -Tags: bash - -END%% - -* `\xhh`: Consists of one or more [[radices#Hexadecimal|hexadecimal]] digits. The `x` prefix is required to distinguish from octal escape sequences. - * [[bash/index|Bash]] supports this sequence as `$'\xhh'`. One or two digits is supported. - -%%ANKI -Basic -How are C escape sequences for hexadecimal digits denoted? -Back: As `\xhh`. -Reference: Brian W. Kernighan and Dennis M. Ritchie, *The C Programming Language*, 2nd ed (Englewood Cliffs, N.J: Prentice Hall, 1988). - -END%% - -%%ANKI -Basic -In C, `\x` allows specifying how many hexadecimal digits? -Back: One or more. -Reference: Brian W. Kernighan and Dennis M. Ritchie, *The C Programming Language*, 2nd ed (Englewood Cliffs, N.J: Prentice Hall, 1988). - -END%% - -%%ANKI -Basic -What footgun does C's `\x` sequence expose? -Back: Using more than two hexadecimal digits can produce undefined results. -Reference: Arnold D. Robbins, “GAWK: Effective AWK Programming,” October 2023, [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf). - -END%% - -* `\uhhhh`: Introduced in C11 to represent Unicode code points. *Must* have exactly four hexadecimal characters specified with `0` leading padding if necessary. - * [[bash/index|Bash]] supports this sequence as `$'uhhhh'`. One to four hex digits is supported. - -%%ANKI -Basic -What two ways are C escape sequences for unicode denoted? -Back: As `\uhhhh` or `\Uhhhhhhhh`. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). -Tags: encoding::unicode - -END%% - -%%ANKI -Basic -In C, `\u` allows specifying how many hexadecimal digits? -Back: Exactly four. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). -Tags: encoding::unicode - -END%% - -%%ANKI -Basic -In what standard were C's `\u` and `\U` escape sequences introduced? -Back: C11. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). -Tags: encoding::unicode - -END%% - -* `\Uhhhhhhhh`: Introduced in C11 to represent larger unicode code points. *Must* have exactly eight hexadecimal characters specified with `0` leading padding if necessary. - -%%ANKI -Basic -In C, `\U` allows specifying how many hexadecimal digits? -Back: Exactly eight. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). -Tags: encoding::unicode - -END%% - -%%ANKI -Basic -Why does C have both `\u` and `\U`? -Back: `\U` accommodates for larger code point values. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). -Tags: encoding::unicode - -END%% - -## Copying Functions - -The two primary functions used for copying memory are `memcpy` and `memmove`: - -```c -void* memcpy(void* restrict s1, const void* restrict s2, size_t n); -void* memmove(void* s1, const void* s2, size_t n); -``` - -%%ANKI -Basic -What are the two primary functions provided by the standard library for copying memory? -Back: `memcpy` and `memmove`. -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 function prototype of `memcpy`? -Back: -```c -void* memcpy(void* restrict s1, const void* restrict s2, size_t n); -``` -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 `memcpy` function do? -Back: Copies a specified number of bytes from one object to another. -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 -Disregarding out of bounds errors, when does `memcpy` invoke undefined behavior? -Back: When copying occurs between objects that overlap in memory. -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 first argument of `memcpy` refer to? -Back: The destination address bytes are copied to. -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 second argument of `memcpy` refer to? -Back: The source address bytes are copied from. -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 third argument of `memcpy` refer to? -Back: The number of bytes to copy from the source address. -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 `memcpy` or `memmove` is faster? -Back: `memcpy` -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 `memmove` slower than `memcpy`? -Back: It's implemented as if copying first into a temporary array that does not overlap the arguments in memory. -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 should you prefer using `memcpy` instead of `memmove`? -Back: When the copying occurs between objects that do not overlap in memory. -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 standard header library defines `memcpy`? -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 is the function prototype of `memmove`? -Back: -```c -void* memmove(void* s1, const void* s2, size_t n); -``` -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 first argument of `memmove` refer to? -Back: The destination address bytes are copied to. -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 second argument of `memmove` refer to? -Back: The source address bytes are copied from. -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 third argument of `memmove` refer to? -Back: The number of bytes to copy from the source address. -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 -Disregarding out of bounds errors, when does `memmove` invoke undefined behavior? -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 -When should you prefer using `memmove` instead of `memcopy`? -Back: When the copying occurs between objects that overlap in memory. -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 standard header library defines `memmove`? -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 does the `memmove` function do? -Back: Copies a specified number of bytes from one object to another. -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 -* Arnold D. Robbins, “GAWK: Effective AWK Programming,” October 2023, [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf). -* Brian W. Kernighan and Dennis M. Ritchie, *The C Programming Language*, 2nd ed (Englewood Cliffs, N.J: Prentice Hall, 1988). * 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). -* Mendel Cooper, “Advanced Bash-Scripting Guide,” n.d., 916. -* “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). +* “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962). \ No newline at end of file diff --git a/notes/linkers/elf.md b/notes/linkers/elf.md new file mode 100644 index 0000000..399e911 --- /dev/null +++ b/notes/linkers/elf.md @@ -0,0 +1,331 @@ +--- +title: ELF +TARGET DECK: Obsidian::STEM +FILE TAGS: linker::elf x86-64 +tags: + - elf + - linker + - x86-64 +--- + +## Overview + +Modern [[x86-64/index|x86-64]] Linux systems use the ELF (Executable and Linkable Format) object file format. + +![[elf.png]] + +%%ANKI +Basic +What object file format do modern x86-64 Linux machines typically use? +Back: ELF. +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 is the ELF object file format an acronym for? +Back: **E**xecutable and **L**inkable **F**ormat. +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 is ELF an example of? +Back: An object file format. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +### Relocatable Object Files + +A typical ELF relocatable object file contains the following sections: + +#### `.text` + +The machine code of the compiled program. + +%%ANKI +Basic +What does the `.text` section of an ELF file contain? +Back: The machine code of the compiled program. +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 ELF section typically contains the machine code of the compiled program? +Back: `.text` +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 {`.text`} section contains the {machine code} of the compiled program. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +#### `.rodata` + +Read-only data such as the format strings in [[c17/strings/printf|printf]] statements and [[conditions#JMP|jump tables]] for switch statements. + +%%ANKI +Basic +What does the `.rodata` section of an ELF file contain? +Back: Read-only data. +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 ELF section typically contains read-only data? +Back: `.rodata` +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 the `.rodata` section named the way it is? +Back: It stands for **r**ead-**o**nly **data**. +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 section of an ELF file would a `printf` format string be found in? +Back: `.rodata` +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. +Tags: c17::printf + +END%% + +%%ANKI +Basic +In what section of an ELF file would a jump table be found in? +Back: `.rodata` +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +#### `.data` + +Global and static C variables initialized to a non-zero value. + +%%ANKI +Basic +What kind of global/static C variables does the `.data` section of an ELF file contain? +Back: Those initialized to a non-zero 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 +Which ELF section contains global C variables initialized to a non-zero value? +Back: `.data` +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 ELF section contains static C variables initialized to a non-zero value? +Back: `.data` +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 ELF section contains local C variables initialized to a non-zero value? +Back: N/A. +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 +Consider the following translation unit. Which ELF section will `foo` end up in? +```c +int foo = 1; +``` +Back: `.data` +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 +Consider the following translation unit. Which ELF section will `bar` end up in? +```c +int foo() { + int bar = 0; +} +``` +Back: N/A. +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 +Consider the following translation unit. Which ELF section will `bar` end up in? +```c +int foo() { + static int bar = 1; +} +``` +Back: `.data`. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +#### `.bss` + +Uninitialized global and static C variables, along with any global or static variables initialized to zero. + +%%ANKI +Basic +What kind of global/static C variables does the `.bss` section of an ELF file contain? +Back: Unitialized variables or those initialized to zero. +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 ELF section contains uninitialized global C variables? +Back: `.bss` +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 ELF section contains global C variables initialized to a zero value? +Back: `.bss` +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 two ELF sections contain global/static C variables? +Back: `.data` and `.bss` +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 +Consider the following translation unit. Which ELF section will `foo` end up in? +```c +int foo = 0; +``` +Back: `.bss` +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 +Consider the following translation unit. Which ELF section will `foo` end up in? +```c +int foo; +``` +Back: `.bss` +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 +Both `.bss` and `.data` hold what kind of C variables? +Back: Global or static C variables. +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 of `.data` and/or `.bss` is considered a placeholder? +Back: `.bss` +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 Bryant et al. mean by saying `.bss` is "merely a placeholder"? +Back: The `.bss` section occupies no actual space in the object file. +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 the size of the `.bss` section compare to that of the `.data` section? +Back: Less than or equal since the `.bss` section is always empty. +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 mneumonic does Bryant et al. suggest to remember the `.bss` and `.data` distinction? +Back: `.bss` is a backronym for "**B**etter **S**ave **S**pace!" +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 +Consider the following translation unit. Which ELF section will `bar` end up in? +```c +int foo() { + static int bar = 0; +} +``` +Back: `.bss`. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +#### `.symtab` + +A symbol table with information about functions and global variables defined and referenced in the program. + +%%ANKI +Basic +What does the `.symtab` section of an ELF file contain? +Back: A symbol table for functions and global variables. +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 ELF section contains a symbol table for functions and global variables? +Back: `.symtab` +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 the `.symtab` ELF section named the way it is? +Back: It's short for **sym**bol **tab**le. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +## Bibliography + +* Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. \ No newline at end of file diff --git a/notes/linkers/images/elf.png b/notes/linkers/images/elf.png new file mode 100644 index 0000000..3139ea4 Binary files /dev/null and b/notes/linkers/images/elf.png differ diff --git a/notes/linkers/index.md b/notes/linkers/index.md index c7566df..d9120ab 100644 --- a/notes/linkers/index.md +++ b/notes/linkers/index.md @@ -99,6 +99,99 @@ Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Program END%% +## Object Files + +Object files come in three forms: + +1. **Relocatable object files**. Contains binary code and data in a form that can be combined with other relocatable object files at compile time. +2. **Executable object files**. Contains binary code and data in a form that can be copied directly into memory and executed. +3. **Shared object files**. A special type of relocatable object file that can be loaded into memory and linked dynamically, at either load time or run time. + +An **object module** is a sequence of bytes whereas an **object file** is an object module stored on disk. + +%%ANKI +Basic +What are the three types of object files? +Back: Relocatable, executable, and shared. +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 +Relocatable object files are outputs of which compiler driver component? +Back: The assembler. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. +Tags: c17 + +END%% + +%%ANKI +Basic +Executable object files are outputs of which compiler driver component? +Back: The linker. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. +Tags: c17 + +END%% + +%%ANKI +Basic +Relocatable object files are inputs into which compiler driver component? +Back: The linker. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. +Tags: c17 + +END%% + +%%ANKI +Basic +Executable object files are inputs into which compiler driver component? +Back: N/A. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. +Tags: c17 + +END%% + +%%ANKI +Cloze +A {shared} object file is a special case of a {relocatable} object file. +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 is an object module? +Back: A sequence of bytes. +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 terms of object modules, what is an object file? +Back: An object module stored on disk. +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 an object module from an object file? +Back: An object file is an object module stored on disk. +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 way is the term "object file" misused by Bryant et al.? +Back: Technically this term only refers to object modules on disk. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + ## Bibliography * Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. \ No newline at end of file diff --git a/notes/linkers/object-files.md b/notes/linkers/object-files.md deleted file mode 100644 index 5e54205..0000000 --- a/notes/linkers/object-files.md +++ /dev/null @@ -1,138 +0,0 @@ ---- -title: Object Files -TARGET DECK: Obsidian::STEM -FILE TAGS: linker::object-file -tags: - - linker - - object-file ---- - -## Overview - -Object files come in three forms: - -1. **Relocatable object files**. Contains binary code and data in a form that can be combined with other relocatable object files at compile time. -2. **Executable object files**. Contains binary code and data in a form that can be copied directly into memory and executed. -3. **Shared object files**. A special type of relocatable object file that can be loaded into memory and linked dynamically, at either load time or run time. - -%%ANKI -Basic -What are the two types of object files? -Back: Relocatable and executable. -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 -Relocatable object files are outputs of which compiler driver component? -Back: The assembler. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. -Tags: c17 - -END%% - -%%ANKI -Basic -Executable object files are outputs of which compiler driver component? -Back: The linker. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. -Tags: c17 - -END%% - -%%ANKI -Basic -Relocatable object files are inputs into which compiler driver component? -Back: The linker. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. -Tags: c17 - -END%% - -%%ANKI -Basic -Executable object files are inputs into which compiler driver component? -Back: N/A. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. -Tags: c17 - -END%% - -%%ANKI -Cloze -A {shared} object file is a special case of a {relocatable} object file. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -An **object module** is a sequence of bytes whereas an **object file** is an object module stored on disk. - -%%ANKI -Basic -What is an object module? -Back: A sequence of bytes. -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 terms of object modules, what is an object file? -Back: An object module stored on disk. -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 an object module from an object file? -Back: An object file is an object module stored on disk. -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 way is the term "object file" misused by Bryant et al.? -Back: Technically this term only refers to object modules on disk. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -Object files are organized to specific **object file formats**: - -* Windows uses the PE (Portable Executable) format. -* Mac OS-X uses the Mach-O format. -* Modern x86-64 Linux systems use the ELF (Executable and Linkable Format) format. - -%%ANKI -Basic -What object file format do x86-64 Linux machines typically use? -Back: ELF. -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 -What is the ELF object file format an acronym for? -Back: **E**xecutable and **L**inkable **F**ormat. -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 -What is ELF an example of? -Back: An object file format. -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%% - -## Bibliography - -* Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. \ No newline at end of file