Reorganize C flashcards slightly.
parent
968cbc4f30
commit
4194fc6c47
|
@ -334,7 +334,7 @@
|
|||
"_journal/2024-02-23.md": "219ce9ad15a8733edd476c97628b71fd",
|
||||
"_journal/2024-02/2024-02-22.md": "312e55d57868026f6e80f7989a889c2b",
|
||||
"c17/strings.md": "02f329f678efe939dbb5501399130296",
|
||||
"c17/index.md": "611dd21439d56575172ef004c7635382",
|
||||
"c17/index.md": "14c1dff9cb7e0272128ac1b89b3494b9",
|
||||
"c17/escape-sequences.md": "a8b99070336878b4e8c11e9e4525a500",
|
||||
"c17/declarations.md": "c853b9584365ba69ac5845082061a8dd",
|
||||
"algorithms/sorting/merge-sort.md": "6506483f7df6507cee0407bd205dbedd",
|
||||
|
@ -798,9 +798,9 @@
|
|||
"c17/enums.md": "9414fb67aa256a0a11b7240534c67bf6",
|
||||
"c17/derived-types.md": "6fb8f23a2423f05d5bdccb6672a32e38",
|
||||
"c17/basic-types.md": "7c6653bf6dc24c2f2aa72fc95c4f7875",
|
||||
"c17/types/simple.md": "44d56a2998f3bba578f14672c8b0ee3c",
|
||||
"c17/types/simple.md": "07bc1e1d1e6764353e99ea018a37f781",
|
||||
"c17/types/enumerated.md": "e1f70a30677c776b7b44ac3e0ff4e76d",
|
||||
"c17/types/derived.md": "9d3a800fb7c944a9391f495c6978c99e",
|
||||
"c17/types/derived.md": "74c608d72b25794bc3887f58f001bab2",
|
||||
"c17/types/basic.md": "5064e21e683c0218890058882e06b6f3",
|
||||
"c17/types/index.md": "14b651bcfc8b2d62ffd200a74a9a2a6b",
|
||||
"_journal/2024-08-25.md": "e73a8edbd027d0f1a39289540eb512f2",
|
||||
|
@ -919,7 +919,7 @@
|
|||
"_journal/2024-10/2024-10-31.md": "8c5e70f566953974f252da9472e527f0",
|
||||
"_journal/2024-10/2024-10-30.md": "054bdbf52843fa2445f6b9f91d5ca46e",
|
||||
"_journal/2024-10/2024-10-29.md": "432b3b073dafd54421ff6f7182ab9a58",
|
||||
"c17/alignment.md": "20c03c33df5feaa16c8cf6e7c67603df",
|
||||
"c17/alignment.md": "7f10089d8e71e0c61c20f84ff95d78a8",
|
||||
"_journal/2024-11-05.md": "6a599e6bc9dcd12a0940956285ae4d00",
|
||||
"_journal/2024-11-06.md": "c91de8a099cfee2514e1054400996e76",
|
||||
"_journal/2024-11/2024-11-05.md": "79a1304037e18fefa135d576db040784",
|
||||
|
@ -957,7 +957,9 @@
|
|||
"_journal/2024-11-24.md": "225661114ad1b605132873e83acdf777",
|
||||
"_journal/2024-11/2024-11-23.md": "1649e2f050cfb45a0c8cf52be29f625f",
|
||||
"_journal/2024-11-25.md": "1ec17a8473fa9c4779090ecbd22d70ef",
|
||||
"_journal/2024-11/2024-11-24.md": "894b021e6335d4e6947448c8d362c083"
|
||||
"_journal/2024-11/2024-11-24.md": "894b021e6335d4e6947448c8d362c083",
|
||||
"_journal/2024-11-26.md": "29bc0b54d23034b9108e567a1d5fa8ac",
|
||||
"_journal/2024-11/2024-11-25.md": "1ec17a8473fa9c4779090ecbd22d70ef"
|
||||
},
|
||||
"fields_dict": {
|
||||
"Basic": [
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
---
|
||||
title: "2024-11-26"
|
||||
---
|
||||
|
||||
- [ ] Anki Flashcards
|
||||
- [x] KoL
|
||||
- [x] OGS
|
||||
- [ ] Sheet Music (10 min.)
|
||||
- [ ] Korean (Read 1 Story)
|
|
@ -304,7 +304,7 @@ struct foo {
|
|||
char c;
|
||||
};
|
||||
```
|
||||
Back: $16$
|
||||
Back: `16`
|
||||
Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. [http://www.catb.org/esr/structure-packing/](http://www.catb.org/esr/structure-packing/).
|
||||
<!--ID: 1730902219825-->
|
||||
END%%
|
||||
|
@ -318,7 +318,7 @@ struct foo {
|
|||
char c;
|
||||
};
|
||||
```
|
||||
Back: $4$
|
||||
Back: `4`
|
||||
Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. [http://www.catb.org/esr/structure-packing/](http://www.catb.org/esr/structure-packing/).
|
||||
<!--ID: 1730902219830-->
|
||||
END%%
|
||||
|
|
|
@ -344,7 +344,7 @@ Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co
|
|||
<!--ID: 1723856661364-->
|
||||
END%%
|
||||
|
||||
## Representation
|
||||
## Representations
|
||||
|
||||
The **binary representation** of a type is the model used to represent values of said type on a given platform. The **object representation** of a type determines how values are stored in memory, disk, etc.
|
||||
|
||||
|
@ -394,6 +394,38 @@ Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co
|
|||
<!--ID: 1727432711873-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
The `sizeof` operator returns values in units of what?
|
||||
Back: Bytes.
|
||||
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
|
||||
<!--ID: 1732622657260-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
Consider object `A`. What is "the type" of its object representation?
|
||||
Back: `unsigned char[sizeof A]`
|
||||
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
|
||||
<!--ID: 1732622657263-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
Every object can be viewed as an array of what type?
|
||||
Back: `unsigned char`
|
||||
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
|
||||
<!--ID: 1732622657266-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
Which type is considered the "atom" of all object types?
|
||||
Back: `unsigned char`
|
||||
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
|
||||
<!--ID: 1732622657269-->
|
||||
END%%
|
||||
|
||||
## Bibliography
|
||||
|
||||
* “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
|
|
|
@ -1,314 +0,0 @@
|
|||
---
|
||||
title: Conversions
|
||||
TARGET DECK: Obsidian::STEM
|
||||
FILE TAGS: c17::type
|
||||
tags:
|
||||
- c17
|
||||
---
|
||||
|
||||
## Overview
|
||||
|
||||
> Several operators convert operand values from one type to another automatically.
|
||||
|
||||
The above quote refers to **implicit conversions**. Alternatively, we can use cast operations to perform **explicit conversions**.
|
||||
|
||||
## Usual Arithmetic Conversions
|
||||
|
||||
As a general rule, the result of an operation has the type of the operand with wider range. The usual arithmetic conversions behave according to the following pattern:
|
||||
|
||||
1. Determine a **common real type** for the operands and result.
|
||||
2. Convert each operand, without change of type domain, to a type with real type matching the common real type.
|
||||
3. Unless explicitly stated otherwise, the common real type is the corresponding real type of the result, whose type domain is that of the operands if they are the same and complex otherwise.
|
||||
|
||||
Common real types are prioritized in the following order:
|
||||
|
||||
1. `long double`
|
||||
2. `double`
|
||||
3. `float`
|
||||
4. If both operands have the same signedness, the higher ranked type.
|
||||
5. If the unsigned operand has rank $\geq$ than that of the other, the unsigned type.
|
||||
6. If the signed operand's includes that of the other, the signed type.
|
||||
7. The unsigned integer type corresponding to that of the signed integer type.
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What is the common real type of `a` and `b`?
|
||||
```c
|
||||
long double a;
|
||||
long double complex b;
|
||||
```
|
||||
Back: `long double`
|
||||
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
<!--ID: 1724762203391-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What is the common real type of `a` and `b`?
|
||||
```c
|
||||
long double a;
|
||||
long complex b;
|
||||
```
|
||||
Back: `long double`
|
||||
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
<!--ID: 1724762203395-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What is the type domain of `a + b`?
|
||||
```c
|
||||
long double a;
|
||||
long complex b;
|
||||
```
|
||||
Back: Complex.
|
||||
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
<!--ID: 1724762203398-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What is the common real type of `a` and `b`?
|
||||
```c
|
||||
double complex a;
|
||||
long b;
|
||||
```
|
||||
Back: `double`
|
||||
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
<!--ID: 1724762203402-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What is the type domain of `a + b`?
|
||||
```c
|
||||
long double a;
|
||||
long complex b;
|
||||
```
|
||||
Back: Complex.
|
||||
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What is the common real type of `a` and `b`?
|
||||
```c
|
||||
float a;
|
||||
double b;
|
||||
```
|
||||
Back: `double`
|
||||
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
<!--ID: 1724762203405-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What is the common real type of `a` and `b`?
|
||||
```c
|
||||
long long a;
|
||||
float b;
|
||||
```
|
||||
Back: `float`
|
||||
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
<!--ID: 1724762203408-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What is the type domain of `a + b`?
|
||||
```c
|
||||
long long a;
|
||||
float b;
|
||||
```
|
||||
Back: Real.
|
||||
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
<!--ID: 1724762203419-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
*Why* is the common real type of `a` and `b` equal to `float`?
|
||||
```c
|
||||
long long a;
|
||||
float b;
|
||||
```
|
||||
Back: Because floating-point types have priority over integer types.
|
||||
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
<!--ID: 1724762203427-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What is the type domain of `a + b`?
|
||||
```c
|
||||
long long a;
|
||||
long long b;
|
||||
```
|
||||
Back: Real.
|
||||
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
<!--ID: 1724762203431-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What is the common real type of `a` and `b`?
|
||||
```c
|
||||
long long a;
|
||||
long long b;
|
||||
```
|
||||
Back: `long long`
|
||||
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
<!--ID: 1724762203436-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What is the common real type of `a` and `b`?
|
||||
```c
|
||||
unsigned int a;
|
||||
signed short b;
|
||||
```
|
||||
Back: `unsigned int`
|
||||
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
<!--ID: 1724762203440-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What is the common real type of `a` and `b`?
|
||||
```c
|
||||
unsigned a;
|
||||
signed int b;
|
||||
```
|
||||
Back: `unsigned`
|
||||
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
<!--ID: 1724762203445-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
*Why* is the common real type of `a` and `b` equal to `unsigned`?
|
||||
```c
|
||||
unsigned a;
|
||||
signed int b;
|
||||
```
|
||||
Back: Because `unsigned` has rank $\geq$ that of `signed int`.
|
||||
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
<!--ID: 1724762203450-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What is the type domain of `a + b`?
|
||||
```c
|
||||
unsigned a;
|
||||
signed int b;
|
||||
```
|
||||
Back: Real.
|
||||
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
<!--ID: 1724762203454-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What is the type domain of `a + b`?
|
||||
```c
|
||||
unsigned short a;
|
||||
signed int b;
|
||||
```
|
||||
Back: Real.
|
||||
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
<!--ID: 1724762203469-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What is the common real type of `a + b`?
|
||||
```c
|
||||
unsigned short a;
|
||||
signed int b;
|
||||
```
|
||||
Back: Indeterminate.
|
||||
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
<!--ID: 1726840632804-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
*Why* is the common real type of `a + b` indeterminate?
|
||||
```c
|
||||
unsigned short a;
|
||||
int b;
|
||||
```
|
||||
Back: Because the rank of `unsigned short` is $<$ that of `int`.
|
||||
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
<!--ID: 1724762203475-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What *might* the common real type of `a + b` be?
|
||||
```c
|
||||
unsigned short a;
|
||||
signed int b;
|
||||
```
|
||||
Back: `signed int` or `unsigned int`.
|
||||
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
<!--ID: 1724762203479-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
When is the common real type of `a + b` equal to `signed int`?
|
||||
```c
|
||||
unsigned short a;
|
||||
signed int b;
|
||||
```
|
||||
Back: When `signed int` can represent all the values of `unsigned short`.
|
||||
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
<!--ID: 1724762203484-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
When is the common real type of `a + b` equal to `unsigned int`?
|
||||
```c
|
||||
unsigned short a;
|
||||
signed int b;
|
||||
```
|
||||
Back: When `signed int` cannot represent all the values of `unsigned short`.
|
||||
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
<!--ID: 1724762203488-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
When is the common real type of `a + b` equal to `unsigned short`?
|
||||
```c
|
||||
unsigned short a;
|
||||
signed int b;
|
||||
```
|
||||
Back: N/A. It never is.
|
||||
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
<!--ID: 1724762203492-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
Suppose `a` and `b` has signed and unsigned types respectively. When is `a + b` signed?
|
||||
Back: When `b`'s type has lower rank and the range of `b` is included in the range of `a`.
|
||||
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
<!--ID: 1724762203460-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
Suppose `a` and `b` have signed and unsigned types respectively. When is `a + b` unsigned?
|
||||
Back: When `b`'s type has higher rank or the range of `a` cannot fit the range of `b`.
|
||||
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
<!--ID: 1724762203465-->
|
||||
END%%
|
||||
|
||||
## Bibliography
|
||||
|
||||
* “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
|
@ -1417,6 +1417,40 @@ Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co
|
|||
<!--ID: 1732456644482-->
|
||||
END%%
|
||||
|
||||
### Aliasing
|
||||
|
||||
Accessing the same object through different pointers is called **aliasing**. With the exclusion of [[simple#Character Types|character types]], only pointers of the same base type may alias.
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What does aliasing refer to?
|
||||
Back: Accessing the same object through different pointers.
|
||||
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
|
||||
<!--ID: 1732623646937-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
Is aliasing possible in the following function?
|
||||
```c
|
||||
void foo(double const* a, double* b);
|
||||
```
|
||||
Back: Yes.
|
||||
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
|
||||
<!--ID: 1732623646945-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
Is aliasing possible in the following function?
|
||||
```c
|
||||
void foo(double const* a, float* b);
|
||||
```
|
||||
Back: No.
|
||||
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
|
||||
<!--ID: 1732623646948-->
|
||||
END%%
|
||||
|
||||
## Functions
|
||||
|
||||
A function `f` without a following opening `(` is converted to a pointer to its start. This is called **function decay**.
|
||||
|
|
|
@ -437,6 +437,309 @@ Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70
|
|||
<!--ID: 1724546734502-->
|
||||
END%%
|
||||
|
||||
## Conversions
|
||||
|
||||
> Several operators convert operand values from one type to another automatically.
|
||||
|
||||
The above quote refers to **implicit conversions**. Alternatively, we can use cast operations to perform **explicit conversions**.
|
||||
|
||||
### Usual Arithmetic Conversions
|
||||
|
||||
As a general rule, the result of an operation has the type of the operand with wider range. The usual arithmetic conversions behave according to the following pattern:
|
||||
|
||||
1. Determine a **common real type** for the operands and result.
|
||||
2. Convert each operand, without change of type domain, to a type with real type matching the common real type.
|
||||
3. Unless explicitly stated otherwise, the common real type is the corresponding real type of the result, whose type domain is that of the operands if they are the same and complex otherwise.
|
||||
|
||||
Common real types are prioritized in the following order:
|
||||
|
||||
1. `long double`
|
||||
2. `double`
|
||||
3. `float`
|
||||
4. If both operands have the same signedness, the higher ranked type.
|
||||
5. If the unsigned operand has rank $\geq$ than that of the other, the unsigned type.
|
||||
6. If the signed operand's includes that of the other, the signed type.
|
||||
7. The unsigned integer type corresponding to that of the signed integer type.
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What is the common real type of `a` and `b`?
|
||||
```c
|
||||
long double a;
|
||||
long double complex b;
|
||||
```
|
||||
Back: `long double`
|
||||
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
<!--ID: 1724762203391-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What is the common real type of `a` and `b`?
|
||||
```c
|
||||
long double a;
|
||||
long complex b;
|
||||
```
|
||||
Back: `long double`
|
||||
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
<!--ID: 1724762203395-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What is the type domain of `a + b`?
|
||||
```c
|
||||
long double a;
|
||||
long complex b;
|
||||
```
|
||||
Back: Complex.
|
||||
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
<!--ID: 1724762203398-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What is the common real type of `a` and `b`?
|
||||
```c
|
||||
double complex a;
|
||||
long b;
|
||||
```
|
||||
Back: `double`
|
||||
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
<!--ID: 1724762203402-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What is the type domain of `a + b`?
|
||||
```c
|
||||
long double a;
|
||||
long complex b;
|
||||
```
|
||||
Back: Complex.
|
||||
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What is the common real type of `a` and `b`?
|
||||
```c
|
||||
float a;
|
||||
double b;
|
||||
```
|
||||
Back: `double`
|
||||
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
<!--ID: 1724762203405-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What is the common real type of `a` and `b`?
|
||||
```c
|
||||
long long a;
|
||||
float b;
|
||||
```
|
||||
Back: `float`
|
||||
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
<!--ID: 1724762203408-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What is the type domain of `a + b`?
|
||||
```c
|
||||
long long a;
|
||||
float b;
|
||||
```
|
||||
Back: Real.
|
||||
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
<!--ID: 1724762203419-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
*Why* is the common real type of `a` and `b` equal to `float`?
|
||||
```c
|
||||
long long a;
|
||||
float b;
|
||||
```
|
||||
Back: Because floating-point types have priority over integer types.
|
||||
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
<!--ID: 1724762203427-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What is the type domain of `a + b`?
|
||||
```c
|
||||
long long a;
|
||||
long long b;
|
||||
```
|
||||
Back: Real.
|
||||
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
<!--ID: 1724762203431-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What is the common real type of `a` and `b`?
|
||||
```c
|
||||
long long a;
|
||||
long long b;
|
||||
```
|
||||
Back: `long long`
|
||||
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
<!--ID: 1724762203436-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What is the common real type of `a` and `b`?
|
||||
```c
|
||||
unsigned int a;
|
||||
signed short b;
|
||||
```
|
||||
Back: `unsigned int`
|
||||
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
<!--ID: 1724762203440-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What is the common real type of `a` and `b`?
|
||||
```c
|
||||
unsigned a;
|
||||
signed int b;
|
||||
```
|
||||
Back: `unsigned`
|
||||
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
<!--ID: 1724762203445-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
*Why* is the common real type of `a` and `b` equal to `unsigned`?
|
||||
```c
|
||||
unsigned a;
|
||||
signed int b;
|
||||
```
|
||||
Back: Because `unsigned` has rank $\geq$ that of `signed int`.
|
||||
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
<!--ID: 1724762203450-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What is the type domain of `a + b`?
|
||||
```c
|
||||
unsigned a;
|
||||
signed int b;
|
||||
```
|
||||
Back: Real.
|
||||
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
<!--ID: 1724762203454-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What is the type domain of `a + b`?
|
||||
```c
|
||||
unsigned short a;
|
||||
signed int b;
|
||||
```
|
||||
Back: Real.
|
||||
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
<!--ID: 1724762203469-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What is the common real type of `a + b`?
|
||||
```c
|
||||
unsigned short a;
|
||||
signed int b;
|
||||
```
|
||||
Back: Indeterminate.
|
||||
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
<!--ID: 1726840632804-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
*Why* is the common real type of `a + b` indeterminate?
|
||||
```c
|
||||
unsigned short a;
|
||||
int b;
|
||||
```
|
||||
Back: Because the rank of `unsigned short` is $<$ that of `int`.
|
||||
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
<!--ID: 1724762203475-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What *might* the common real type of `a + b` be?
|
||||
```c
|
||||
unsigned short a;
|
||||
signed int b;
|
||||
```
|
||||
Back: `signed int` or `unsigned int`.
|
||||
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
<!--ID: 1724762203479-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
When is the common real type of `a + b` equal to `signed int`?
|
||||
```c
|
||||
unsigned short a;
|
||||
signed int b;
|
||||
```
|
||||
Back: When `signed int` can represent all the values of `unsigned short`.
|
||||
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
<!--ID: 1724762203484-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
When is the common real type of `a + b` equal to `unsigned int`?
|
||||
```c
|
||||
unsigned short a;
|
||||
signed int b;
|
||||
```
|
||||
Back: When `signed int` cannot represent all the values of `unsigned short`.
|
||||
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
<!--ID: 1724762203488-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
When is the common real type of `a + b` equal to `unsigned short`?
|
||||
```c
|
||||
unsigned short a;
|
||||
signed int b;
|
||||
```
|
||||
Back: N/A. It never is.
|
||||
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
<!--ID: 1724762203492-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
Suppose `a` and `b` has signed and unsigned types respectively. When is `a + b` signed?
|
||||
Back: When `b`'s type has lower rank and the range of `b` is included in the range of `a`.
|
||||
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
<!--ID: 1724762203460-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
Suppose `a` and `b` have signed and unsigned types respectively. When is `a + b` unsigned?
|
||||
Back: When `b`'s type has higher rank or the range of `a` cannot fit the range of `b`.
|
||||
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
<!--ID: 1724762203465-->
|
||||
END%%
|
||||
|
||||
## Bibliography
|
||||
|
||||
* “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
|
|
|
@ -223,18 +223,6 @@ Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Program
|
|||
<!--ID: 1718281813458-->
|
||||
END%%
|
||||
|
||||
## Characters
|
||||
|
||||
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.
|
||||
<!--ID: 1707493017239-->
|
||||
END%%
|
||||
|
||||
## Integers
|
||||
|
||||
**Narrow types** cannot be used directly in arithmetic. Instead they are first promoted to a wider type. On almost every system, this promotion will be to a `signed int` of the same value, regardless of the signedness of the narrow type itself.
|
||||
|
@ -263,6 +251,74 @@ Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co
|
|||
<!--ID: 1723859121972-->
|
||||
END%%
|
||||
|
||||
### Character Types
|
||||
|
||||
The three types `char`, `signed char`, and `unsigned char` are collectively called the **character types**. The implementation defines `char` to have the same range, representation, and behavior as either `signed char` or `unsigned char`, but is considered incompatible with both. That is, it is a distinct type in the eyes of the type system.
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
Is declaration `char` signed or unsigned?
|
||||
Back: This is implementation-dependent.
|
||||
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||
<!--ID: 1707493017239-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What are the character types?
|
||||
Back: `char`, `signed char`, and `unsigned char`.
|
||||
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
<!--ID: 1732622657234-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
`char` has the same range, representation, and behavior as what other type?
|
||||
Back: Either `signed char` or `unsigned char`.
|
||||
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
<!--ID: 1732622657240-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
How do you check if `char` has the same representation as a `signed char` or `unsigned char`?
|
||||
Back: Check if `CHAR_MIN` has value `SCHAR_MIN` or `0` respectively.
|
||||
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
<!--ID: 1732622657243-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
Which header defines `CHAR_MIN`?
|
||||
Back: `limits.h`
|
||||
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
|
||||
<!--ID: 1732622657247-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
Which character type should be used for strings?
|
||||
Back: `char`
|
||||
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
|
||||
<!--ID: 1732622657250-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What does it mean for `char` to be incompatible with other character types?
|
||||
Back: The type system considers it to be a distinct type.
|
||||
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
|
||||
<!--ID: 1732622657253-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What does `sizeof(char)` evaluate to?
|
||||
Back: `1`
|
||||
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
|
||||
<!--ID: 1732622657256-->
|
||||
END%%
|
||||
|
||||
### Unsigned Integers
|
||||
|
||||
| Name | Narrow | Rank | Minimum Width |
|
||||
|
|
Loading…
Reference in New Issue