2024-02-23 14:40:31 +00:00
---
title: C17
2024-08-13 01:05:18 +00:00
TARGET DECK: Obsidian::STEM
FILE TAGS: c17
2024-02-23 14:40:31 +00:00
tags:
- c17
---
2024-08-13 01:05:18 +00:00
## Overview
2024-08-17 01:46:05 +00:00
> A C program can be seen as a sort of machine that manipulates values: the particular values that variables of the program have at a given time, and also intermediate values that are the result of computed expressions.
2024-08-13 01:05:18 +00:00
2024-08-17 01:46:05 +00:00
This quote describes C's **abstract state machine** . Whatever instructions a C program compiles down to is "unimportant" provided that all **observable states** are correctly reproduced. This is the essence of optimization.
2024-08-13 01:05:18 +00:00
%%ANKI
Basic
2024-08-17 01:46:05 +00:00
What feature of C's abstract state machine makes C performant?
2024-08-24 08:07:51 +00:00
Back: The ability to optimize.
2024-08-17 01:46:05 +00:00
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!-- ID: 1723856661330 -->
2024-08-13 01:05:18 +00:00
END%%
%%ANKI
Basic
2024-08-17 01:46:05 +00:00
C can compile into any sequence of instructions provided what holds?
Back: All observable states are correctly reproduced.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!-- ID: 1723856661337 -->
2024-08-13 01:05:18 +00:00
END%%
%%ANKI
2024-08-17 01:46:05 +00:00
Basic
Why is C's abstract state machine called the way it is?
Back: Compilers are free to realize the state machine however they see fit.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!-- ID: 1723856661340 -->
2024-08-13 01:05:18 +00:00
END%%
%%ANKI
Basic
2024-08-17 01:46:05 +00:00
What three components make up C's abstract state machine?
Back: Values, types, and binary representations.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!-- ID: 1723856661343 -->
2024-08-13 01:05:18 +00:00
END%%
%%ANKI
2024-08-17 01:46:05 +00:00
Cloze
In C's abstract state machine, {binary representations} describe {types} which describe {values}.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!-- ID: 1723856668033 -->
2024-08-13 01:05:18 +00:00
END%%
2024-08-17 01:46:05 +00:00
## Values
An **object** is a region of data storage in the execution environment, the contents of which can represent **values** . An **lvalue** is an expression (with non-`void` object type) that potentially designates an object. An **rvalue** is the "value of the expression."
The notion of a value in C is an abstract entity. It exists beyond the program or the representation of the value in the program. For example, the value `0` (no matter how its represented) added to variable `x` should always yield result `x` regardless of platform.
2024-08-13 01:05:18 +00:00
%%ANKI
Basic
2024-08-17 01:46:05 +00:00
What does an object refer to?
Back: A region of data storage in the execution environment.
2024-08-13 01:05:18 +00:00
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf ](https://port70.net/~nsz/c/c11/n1570.pdf ).
2024-08-17 01:46:05 +00:00
<!-- ID: 1723510994830 -->
2024-08-13 01:05:18 +00:00
END%%
%%ANKI
Basic
2024-08-17 01:46:05 +00:00
What does a value refer to?
Back: The contents of an object when interpreted as having a specific type.
2024-08-13 01:05:18 +00:00
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf ](https://port70.net/~nsz/c/c11/n1570.pdf ).
2024-08-17 01:46:05 +00:00
<!-- ID: 1723510994835 -->
2024-08-13 01:05:18 +00:00
END%%
%%ANKI
Basic
2024-08-17 01:46:05 +00:00
Why does Gustedt refer to values as abstract entities?
Back: A value exists beyond a program or any particular representation.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!-- ID: 1723856661349 -->
2024-08-13 01:05:18 +00:00
END%%
%%ANKI
Basic
2024-08-17 01:46:05 +00:00
How does Gustedt distinguish the data of a program execution from values?
Back: The data is the set of values of all objects at a given moment.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!-- ID: 1723856661358 -->
END%%
%%ANKI
Cloze
A {value} refers to the contents of an {object} when interpreted as having a specific type.
2024-08-13 01:05:18 +00:00
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf ](https://port70.net/~nsz/c/c11/n1570.pdf ).
2024-08-17 01:46:05 +00:00
<!-- ID: 1723510994839 -->
2024-08-13 01:05:18 +00:00
END%%
%%ANKI
Basic
What is an lvalue?
Back: An expression (with non-`void` object type) that potentially designates an object.
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: 1723510994874 -->
END%%
%%ANKI
Basic
Why are lvalues named the way they are?
2024-09-06 01:27:23 +00:00
Back: The name is an acronym for **l**ocator **value** (or **l**eft **value** ).
2024-08-13 01:05:18 +00:00
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: 1723510994878 -->
END%%
%%ANKI
Basic
What is an rvalue?
Back: The value of an expression.
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: 1723510994881 -->
END%%
%%ANKI
Basic
Why are rvalues named the way they are?
Back: The name is an acronym for **r**ight **value** .
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: 1723510994886 -->
END%%
%%ANKI
Basic
Can an lvalue designate an object?
Back: Yes.
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: 1723510994900 -->
END%%
%%ANKI
Basic
Can an lvalue designate a function?
Back: No.
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: 1723510994907 -->
END%%
%%ANKI
Basic
Is `x` an lvalue or rvalue in the following snippet?
```c
int x = 10;
```
Back: An lvalue.
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: 1723510994914 -->
END%%
%%ANKI
Basic
Is `int` an lvalue or rvalue in the following snippet?
```c
int x = 10;
```
Back: Neither.
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: 1723510994920 -->
END%%
%%ANKI
Basic
Is `10` an lvalue or rvalue in the following snippet?
```c
int x = 10;
```
Back: An rvalue.
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: 1723510994925 -->
END%%
%%ANKI
Basic
*Why* is `x` an lvalue in the following?
```c
int x = 10;
```
Back: Because `x` refers to a memory location.
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: 1723510994932 -->
END%%
%%ANKI
Basic
Is `x` an lvalue or rvalue in the following snippet?
```c
void x;
```
Back: Neither.
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: 1723510994939 -->
END%%
%%ANKI
Basic
Is `x` an lvalue or rvalue in the following snippet?
```c
void *x;
```
Back: An lvalue.
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: 1723510994945 -->
END%%
%%ANKI
Basic
Is `y` an lvalue or rvalue in the following snippet?
```c
int y = x + 10;
```
Back: An lvalue.
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: 1723510994952 -->
END%%
%%ANKI
Basic
Is `x` an lvalue or rvalue in the following snippet?
```c
int y = x + 10;
```
Back: An rvalue.
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: 1723510994958 -->
END%%
%%ANKI
Basic
In the second line, is `ptr` an lvalue or rvalue?
```c
int *ptr = &x;
*ptr = 10;
```
Back: An lvalue.
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: 1723510994964 -->
END%%
%%ANKI
Basic
In the second line, is `*ptr` an lvalue or rvalue?
```c
int *ptr = &x;
*ptr = 10;
```
Back: An lvalue.
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: 1723510994970 -->
END%%
%%ANKI
Basic
Is `getValue` an lvalue or rvalue in the following snippet?
```c
int getValue () {
return 42;
}
```
Back: Neither.
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: 1723510994975 -->
END%%
%%ANKI
Basic
Is `getValue()` an lvalue or rvalue in the following snippet?
```c
int d = getValue();
```
Back: An rvalue.
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: 1723510994980 -->
END%%
%%ANKI
Basic
*Why* is `getValue` in the following snippet neither an lvalue nor an rvalue?
```c
int getValue() {
return 42;
}
```
Back: The function name is just syntax. That is, it isn't an expression.
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: 1723510994984 -->
END%%
%%ANKI
Basic
Are variables typically lvalues or rvalues?
Back: lvalues.
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: 1723510994988 -->
END%%
%%ANKI
Basic
Are constants typically lvalues or rvalues?
Back: rvalues.
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: 1723510994992 -->
END%%
%%ANKI
Basic
2024-10-24 23:14:43 +00:00
Are array accesses typically lvalues or rvalues?
2024-08-13 01:05:18 +00:00
Back: lvalues.
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: 1723510994997 -->
END%%
%%ANKI
Basic
Are dereferenced pointers typically lvalues or rvalues?
Back: lvalues.
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: 1723510995001 -->
END%%
%%ANKI
Basic
Are temporary values typically lvalues or rvalues?
Back: rvalues.
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: 1723510995006 -->
END%%
2024-08-17 01:46:05 +00:00
## Types
Types are additional properties that C associates with values. All values have a type that is statically determined and all possible operations on a value are determined by its type.
%%ANKI
Cloze
Possible operations on a {value} are determined by its {type}.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!-- ID: 1723856661364 -->
END%%
## Representation
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.
%%ANKI
Basic
What is the binary representation of a type?
Back: The model used to represent values of the type on a given platform.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!-- ID: 1723856661371 -->
END%%
%%ANKI
Basic
What is the object representation of a type?
Back: How a value of a given type is actually stored in memory, disk, etc.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!-- ID: 1723856661386 -->
END%%
%%ANKI
Cloze
A {type}'s {binary representation} determines the results of all operations.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!-- ID: 1723856661393 -->
END%%
%%ANKI
Basic
Why does Gustedt refer to binary representations as abstract entities?
Back: Binary representations don't completely determine how values are stored in memory.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!-- ID: 1723856661399 -->
END%%
%%ANKI
Cloze
A {binary} representation is abstract whereas an {object} representation is concrete.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!-- ID: 1723856661405 -->
END%%
2024-09-28 03:02:33 +00:00
%%ANKI
Basic
Why might the same value have different binary representations?
Back: Because the binary representation corresponds to the type of the value.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!-- ID: 1727432711873 -->
END%%
2024-08-13 01:05:18 +00:00
## Bibliography
* “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf ](https://port70.net/~nsz/c/c11/n1570.pdf ).
* Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).