notebook/notes/c17/typedefs.md

160 lines
4.8 KiB
Markdown
Raw Permalink Normal View History

2024-11-05 18:32:35 +00:00
---
title: Typedefs
TARGET DECK: Obsidian::STEM
FILE TAGS: c17::type
tags:
- c17
---
## Overview
The `<stddef.h>` header defines a few standard `typedef`s:
* `ptrdiff_t`: the signed integer type of the result of subtracting two pointers.
* `size_t`: the unsigned integer type of the result of the `sizeof` operator.
The standard often uses `typedef`s ending with `_t`.
%%ANKI
Basic
What is the type of `x` in the following?
```c
#define int_ptr int *
int_ptr x, y;
```
Back: `int *`
Reference: Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994.
<!--ID: 1722786892109-->
END%%
%%ANKI
Basic
What is the type of `y` in the following?
```c
#define int_ptr int *
int_ptr x, y;
```
Back: `int`
Reference: Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994.
<!--ID: 1722786892110-->
END%%
%%ANKI
Basic
What is the type of `x` in the following?
```c
typedef int_ptr int *
int_ptr x, y;
```
Back: `int *`
Reference: Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994.
<!--ID: 1722786892111-->
END%%
%%ANKI
Basic
What is the type of `y` in the following?
```c
typedef int_ptr int *
int_ptr x, y;
```
Back: `int *`
Reference: Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994.
<!--ID: 1722786892112-->
END%%
%%ANKI
Basic
What header defines `size_t`?
Back: `<stddef.h>`
2024-12-02 12:31:29 +00:00
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).
2024-11-05 18:32:35 +00:00
<!--ID: 1730740461658-->
END%%
%%ANKI
Basic
What header defines `ptrdiff_t`?
Back: `<stddef.h>`
2024-12-02 12:31:29 +00:00
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).
2024-11-05 18:32:35 +00:00
<!--ID: 1730740461659-->
END%%
%%ANKI
Basic
`ptrdiff_t` is used as the type of what result?
Back: Subtracting two pointers.
2024-12-02 12:31:29 +00:00
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).
2024-11-05 18:32:35 +00:00
<!--ID: 1730740461660-->
END%%
%%ANKI
Basic
`size_t` is used as the type of what result?
Back: The `sizeof` operation.
2024-12-02 12:31:29 +00:00
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).
2024-11-05 18:32:35 +00:00
<!--ID: 1730740461661-->
END%%
%%ANKI
Basic
Is `ptrdiff_t` signed or unsigned?
Back: Signed.
2024-12-02 12:31:29 +00:00
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).
2024-11-05 18:32:35 +00:00
<!--ID: 1730740461662-->
END%%
2024-11-23 22:21:18 +00:00
%%ANKI
Basic
When can two pointers be subtracted?
Back: Only if both refer to elements of the same array object.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732397726963-->
END%%
%%ANKI
Basic
What is the result of `p - q` in the following?
```c
double A[4] = { 0.0, 1.0, 2.0, -3.0 };
double* p = &A[1];
double* q = &A[3];
```
Back: `-2`
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732397726964-->
END%%
%%ANKI
Basic
What is the result of `p - q` in the following?
```c
double A[4] = { 0.0, 1.0, 2.0, -3.0 };
double* p = &A[3];
double* q = &A[1];
```
Back: `2`
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732397726965-->
END%%
2024-11-05 18:32:35 +00:00
%%ANKI
Basic
Is `size_t` signed or unsigned?
Back: Unsigned.
2024-12-02 12:31:29 +00:00
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).
2024-11-05 18:32:35 +00:00
<!--ID: 1730740461663-->
END%%
%%ANKI
Basic
The C standard typically suffixes `typedef`s with what?
Back: `_t`
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1730758755507-->
END%%
## Bibliography
2024-12-02 12:31:29 +00:00
* “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).
2024-11-05 18:32:35 +00:00
* Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
* Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994.