--- title: Types TARGET DECK: Obsidian::STEM FILE TAGS: c17 tags: - c17 --- ## Overview C has a series of basic types and means of constructing derived types from them. ## Integers Type `char` is special since it can be signed or unsigned depending on platform. Keep in mind regardless of its signedness, it is still considered a distinct type from both the `unsigned char` and `signed char` type. %%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%% **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 to 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%% ### Unsigned These correspond to nonnegative integer values. | Name | Narrow | Rank | | -------------------- | ------ | ---- | | `bool` | Yes | 0 | | `char` (maybe) | Yes | 1 | | `unsigned char` | Yes | 1 | | `unsighed short` | Yes | 2 | | `unsigned int` | No | 3 | | `unsigned long` | No | 4 | | `unsigned long long` | No | 5 | %%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%% ### Signed These correspond to possibly negative integer values. | Name | Narrow | Rank | | -------------------- | ------ | ---- | | `char` (maybe) | Yes | 1 | | `signed char` | Yes | 1 | | `signed short` | Yes | 2 | | `signed int` | No | 3 | | `signed long` | No | 4 | | `signed long long` | No | 5 | | `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%% ### 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 rules are as follows: | Decimal | Oct/Hex | | ----------- | -------------------- | | `int` | `int` | | `long` | `unsigned` | | `long long` | `long` | | `-` | `unsigned long` | | `-` | `long long` | | `-` | `unsigned long long` | %%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 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 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 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%% ## Bibliography * Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. * Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).