| Contents |

41 Function Specifiers, Alignment Specifiers/Operators

These don’t see a heck of a lot of use in my experience, but we’ll cover them here for the sake of completeness.

41.1 Function Specifiers

When you declare a function, you can give the compiler a couple tips about how the functions could or will be used. This enables or encourages the compiler to make certain optimizations.

41.1.1 inline for Speed—Maybe

You can declare a function to be inline like this:

static inline int add(int x, int y) {
    return x + y;
}

This is meant to encourage the compiler to make this function call as fast as possible. And, historically, one way to do this was inlining, which means that the body of the function would be embedded in its entirety where the call was made. This would avoid all the overhead of setting up the function call and tearing it down at the expense of larger code size as the function was copied all over the place instead of being reused.

The quick-and-dirty things to remember are:

  1. You probably don’t need to use inline for speed. Modern compilers know what’s best.

  2. If you do use it for speed, use it with file scope, i.e. static inline. This avoids the messy rules of external linkage and inline functions.

Stop reading this section now.

Glutton for punishment, eh?

Let’s try leaving the static off.

#include <stdio.h>

inline int add(int x, int y)
{
    return x + y;
}

int main(void)
{
    printf("%d\n", add(1, 2));
}

gcc gives a linker error on add()231. The spec requires that if you have a non-extern inline function you must also provide a version with external linkage.

So you’d have to have an extern version somewhere else for this to work. If the compiler has both an inline function in the current file and an external version of the same function elsewhere, it gets to choose which one to call. So I highly recommend they be the same.

Another thing you can do is to declare the function as extern inline. This will attempt to inline in the same file (for speed), but will also create a version with external linkage.

41.1.2 noreturn and _Noreturn

This indicates to the compiler that a particular function will not ever return to its caller, i.e. the program will exit by some mechanism before the function returns.

It allows the compiler to perhaps perform some optimizations around the function call.

It also allows you to indicate to other devs that some program logic depends on a function not returning.

You’ll likely never need to use this, but you’ll see it on some library calls like exit()232 and abort()233.

The built-in keyword is _Noreturn, but if it doesn’t break your existing code, everyone would recommend including <stdnoreturn.h> and using the easier-to-read noreturn instead.

It’s undefined behavior if a function specified as noreturn actually does return. It’s computationally dishonest, see.

Here’s an example of using noreturn correctly:

#include <stdio.h>
#include <stdlib.h>
#include <stdnoreturn.h>

noreturn void foo(void) // This function should never return!
{
    printf("Happy days\n");

    exit(1);            // And it doesn't return--it exits here!
}

int main(void)
{
    foo();
}

If the compiler detects that a noreturn function could return, it might warn you, helpfully.

Replacing the foo() function with this:

noreturn void foo(void)
{
    printf("Breakin' the law\n");
}

gets me a warning:

foo.c:7:1: warning: function declared 'noreturn' should not return

41.2 Alignment Specifiers and Operators

Alignment234 is all about multiples of addresses on which objects can be stored. Can you store this at any address? Or must it be a starting address that’s divisible by 2? Or 8? Or 16?

If you’re coding up something low-level like a memory allocator that interfaces with your OS, you might need to consider this. Most devs go their careers without using this functionality in C.

41.2.1 alignas and _Alignas

This isn’t a function. Rather, it’s an alignment specifier that you can use with a variable declaration.

The built-in specifier is _Alignas, but the header <stdalign.h> defines it as alignas for something better looking.

If you need your char to be aligned like an int, you can force it like this when you declare it:

char alignas(int) c;

You can also pass a constant value or expression in for the alignment. This has to be something supported by the system, but the spec stops short of dictating what values you can put in there. Small powers of 2 (1, 2, 4, 8, and 16) are generally safe bets.

char alignas(8) c;   // align on 8-byte boundaries

If you want to align at the maximum used alignment by your system, include <stddef.h> and use the type max_align_t, like so:

char alignas(max_align_t) c;

You could potentially over-align by specifying an alignment more than that of max_align_t, but whether or not such things are allowed is system dependent.

41.2.2 alignof and _Alignof

This operator will return the address multiple a particular type uses for alignment on this system. For example, maybe chars are aligned every 1 address, and ints are aligned every 4 addresses.

The built-in operator is _Alignof, but the header <stdalign.h> defines it as alignof if you want to look cooler.

Here’s a program that will print out the alignments of a variety of different types. Again, these will vary from system to system. Note that the type max_align_t will give you the maximum alignment used by the system.

#include <stdalign.h>
#include <stdio.h>     // for printf()
#include <stddef.h>    // for max_align_t

struct t {
    int a;
    char b;
    float c;
};

int main(void)
{
    printf("char       : %zu\n", alignof(char));
    printf("short      : %zu\n", alignof(short));
    printf("int        : %zu\n", alignof(int));
    printf("long       : %zu\n", alignof(long));
    printf("long long  : %zu\n", alignof(long long));
    printf("double     : %zu\n", alignof(double));
    printf("long double: %zu\n", alignof(long double));
    printf("struct t   : %zu\n", alignof(struct t));
    printf("max_align_t: %zu\n", alignof(max_align_t));
}

Output on my system:

char       : 1
short      : 2
int        : 4
long       : 8
long long  : 8
double     : 8
long double: 16
struct t   : 16
max_align_t: 16

41.3 memalignment() Function

New in C23!

(Caveat: none of my compilers support this function yet, so the code is largely untested.)

alignof is great if you know the type of your data. But what if you’re woefully ignorant of the type, and only have a pointer to the data?

How could that even happen?

Well, with our good friend the void*, of course. We can’t pass that to alignof, but what if we need to know the alignment of the thing it points to?

We might want to know this if we’re about to use the memory for something that has significant alignment needs. For example, atomic and floating types often behave badly if misaligned.

So with this function we can check the alignment of some data as long as we have a pointer to that data, even if it’s a void*.

Let’s do a quick test to see if a void pointer is well-aligned for use as an atomic type, and, if so, let’s get a variable to use it as that type:

void foo(void *p)
{
    if (memalignment(p) >= alignof(atomic int)) {
        atomic int *i = p;
        do_things(i);
    } else
        puts("This pointer is no good as an atomic int\n");

...

I suspect you will rarely (to the point of never, likely) need to use this function unless you’re doing some low-level stuff.

And there you have it. Alignment!


  1. https://www.ioccc.org/↩︎

  2. https://en.wikipedia.org/wiki/Python_(programming_language)↩︎

  3. https://en.wikipedia.org/wiki/JavaScript↩︎

  4. https://en.wikipedia.org/wiki/Java_(programming_language)↩︎

  5. https://en.wikipedia.org/wiki/Rust_(programming_language)↩︎

  6. https://en.wikipedia.org/wiki/Go_(programming_language)↩︎

  7. https://en.wikipedia.org/wiki/Swift_(programming_language)↩︎

  8. https://en.wikipedia.org/wiki/Objective-C↩︎

  9. https://beej.us/guide/bgclr/↩︎

  10. https://en.wikipedia.org/wiki/ANSI_C↩︎

  11. https://en.wikipedia.org/wiki/POSIX↩︎

  12. https://visualstudio.microsoft.com/vs/community/↩︎

  13. https://docs.microsoft.com/en-us/windows/wsl/install-win10↩︎

  14. https://developer.apple.com/xcode/↩︎

  15. https://beej.us/guide/bgc/↩︎

  16. https://en.cppreference.com/↩︎

  17. https://groups.google.com/g/comp.lang.c↩︎

  18. https://www.reddit.com/r/C_Programming/↩︎

  19. https://en.wikipedia.org/wiki/Assembly_language↩︎

  20. https://en.wikipedia.org/wiki/Bare_machine↩︎

  21. https://en.wikipedia.org/wiki/Operating_system↩︎

  22. https://en.wikipedia.org/wiki/Embedded_system↩︎

  23. https://en.wikipedia.org/wiki/Rust_(programming_language)↩︎

  24. https://en.wikipedia.org/wiki/Grok↩︎

  25. I know someone will fight me on that, but it’s gotta be at least in the top three, right?↩︎

  26. Well, technically there are more than two, but hey, let’s pretend there are two—ignorance is bliss, right?↩︎

  27. https://en.wikipedia.org/wiki/Assembly_language↩︎

  28. https://en.wikipedia.org/wiki/Machine_code↩︎

  29. Technically, it contains preprocessor directives and function prototypes (more on that later) for common input and output needs.↩︎

  30. https://en.wikipedia.org/wiki/Unix↩︎

  31. If you don’t give it an output filename, it will export to a file called a.out by default—this filename has its roots deep in Unix history.↩︎

  32. https://formulae.brew.sh/formula/gcc↩︎

  33. A “byte” is typically an 8-bit binary number. Think of it as an integer that can only hold the values from 0 to 255, inclusive. Technically, C allows bytes to be any number of bits and if you want to unambiguously refer to an 8-bit number, you should use the term octet. But programmers are going assume you mean 8-bits when you say “byte” unless you specify otherwise.↩︎

  34. I’m seriously oversimplifying how modern memory works, here. But the mental model works, so please forgive me.↩︎

  35. I’m lying here a little. Technically 3.14159 is of type double, but we’re not there yet and I want you to associate float with “Floating Point”, and C will happily coerce that type into a float. In short, don’t worry about it until later.↩︎

  36. Read this as “pointer to a char” or “char pointer”. “Char” for character. Though I can’t find a study, it seems anecdotally most people pronounce this as “char”, a minority say “car”, and a handful say “care”. We’ll talk more about pointers later.↩︎

  37. Colloquially, we say they have “random” values, but they aren’t truly—or even pseudo-truly—random numbers.↩︎

  38. This isn’t strictly 100% true. When we get to learning about static storage duration, you’ll find the some variables are initialized to zero automatically. But the safe thing to do is always initialize them.↩︎

  39. The _t is short for type.↩︎

  40. Except for with variable length arrays—but that’s a story for another time.↩︎

  41. https://beej.us/guide/bgclr/html/split/stdlib.html#man-srand↩︎

  42. This was considered such a hazard that the designers of the Go Programming Language made break the default; you have to explicitly use Go’s fallthrough statement if you want to fall into the next case.↩︎

  43. Never say “never”.↩︎

  44. Typically. I’m sure there are exceptions out there in the dark corridors of computing history.↩︎

  45. A byte is a number made up of no more than 8 binary digits, or bits for short. This means in decimal digits just like grandma used to use, it can hold an unsigned number between 0 and 255, inclusive.↩︎

  46. The order that bytes come in is referred to as the endianness of the number. The usual suspects are big-endian (with the most significant byte first) and little-endian (with the most-significant byte last), or, uncommonly now, mixed-endian (with the most-significant bytes somewhere else).↩︎

  47. That is, base 16 with digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F.↩︎

  48. https://en.wikipedia.org/wiki/Virtual_memory↩︎

  49. That’s not all! It’s used in /*comments*/ and multiplication and in function prototypes with variable length arrays! It’s all the same *, but the context gives it different meaning.↩︎

  50. https://en.wikipedia.org/wiki/Null_pointer#History↩︎

  51. https://en.wikipedia.org/wiki/Sentinel_value↩︎

  52. The pointer type variables are a, d, f, and i, because those are the ones with * in front of them.↩︎

  53. These days, anyway.↩︎

  54. Again, not really, but variable-length arrays—of which I’m not really a fan—are a story for another time.↩︎

  55. Since arrays are just pointers to the first element of the array under the hood, there’s no additional information recording the length.↩︎

  56. Because when you pass an array to a function, you’re actually just passing a pointer to the first element of that array, not the “entire” array.↩︎

  57. In the good old MS-DOS days before memory protection was a thing, I was writing some particularly abusive C code that deliberately engaged in all kinds of undefined behavior. But I knew what I was doing, and things were working pretty well. Until I made a misstep that caused a lockup and, as I found upon reboot, nuked all my BIOS settings. That was fun. (Shout-out to @man for those fun times.)↩︎

  58. There are a lot of things that cause undefined behavior, not just out-of-bounds array accesses. This is what makes the C language so exciting.↩︎

  59. https://en.wikipedia.org/wiki/Row-_and_column-major_order↩︎

  60. This is technically incorrect, as a pointer to an array and a pointer to the first element of an array have different types. But we can burn that bridge when we get to it.↩︎

  61. C11 §6.7.6.2¶1 requires it be greater than zero. But you might see code out there with arrays declared of zero length at the end of structs and GCC is particularly lenient about it unless you compile with -pedantic. This zero-length array was a hackish mechanism for making variable-length structures. Unfortunately, it’s technically undefined behavior to access such an array even though it basically worked everywhere. C99 codified a well-defined replacement for it called flexible array members, which we’ll chat about later.↩︎

  62. This is also equivalent: void print_2D_array(int (*a)[3]), but that’s more than I want to get into right now.↩︎

  63. Though it is true that C doesn’t track the length of strings.↩︎

  64. If you’re using the basic character set or an 8-bit character set, you’re used to one character being one byte. This isn’t true in all character encodings, though.↩︎

  65. This is different than the NULL pointer, and I’ll abbreviate it NUL when talking about the character versus NULL for the pointer.↩︎

  66. Later we’ll learn a neater way to do it with pointer arithmetic.↩︎

  67. There’s a safer function called strncpy() that you should probably use instead, but we’ll get to that later.↩︎

  68. Although in C individual items in memory like ints are referred to as “objects”, they’re not objects in an object-oriented programming sense.↩︎

  69. The Saturn was a popular brand of economy car in the United States until it was put out of business by the 2008 crash, sadly so to us fans.↩︎

  70. A pointer is likely 8 bytes on a 64-bit system.↩︎

  71. A deep copy follows pointer in the struct and copies the data they point to, as well. A shallow copy just copies the pointers, but not the things they point to. C doesn’t come with any built-in deep copy functionality.↩︎

  72. https://beej.us/guide/bgclr/html/split/stringref.html#man-strcmp↩︎

  73. https://beej.us/guide/bgclr/html/split/stringref.html#man-memset↩︎

  74. https://stackoverflow.com/questions/141720/how-do-you-compare-structs-for-equality-in-c↩︎

  75. We used to have three different newlines in broad effect: Carriage Return (CR, used on old Macs), Linefeed (LF, used on Unix systems), and Carriage Return/Linefeed (CRLF, used on Windows systems). Thankfully the introduction of OS X, being Unix-based, reduced this number to two.↩︎

  76. If the buffer’s not big enough to read in an entire line, it’ll just stop reading mid-line, and the next call to fgets() will continue reading the rest of the line.↩︎

  77. Normally the second program would read all the bytes at once, and then print them out in a loop. That would be more efficient. But we’re going for demo value, here.↩︎

  78. https://en.wikipedia.org/wiki/Hex_dump↩︎

  79. https://en.wikipedia.org/wiki/Endianess↩︎

  80. And this is why I used individual bytes in my fwrite() and fread() examples, above, shrewdly.↩︎

  81. https://en.wikipedia.org/wiki/Protocol_buffers↩︎

  82. We’ll talk more about these later.↩︎

  83. Recall that the sizeof operator tells you the size in bytes of an object in memory.↩︎

  84. Or string, which is really an array of chars. Somewhat peculiarly, you can also have a pointer that references one past the end of the array without a problem and still do math on it. You just can’t dereference it when it’s out there.↩︎

  85. https://beej.us/guide/bgclr/html/split/stdlib.html#man-qsort↩︎

  86. https://beej.us/guide/bgclr/html/split/stdlib.html#man-bsearch↩︎

  87. Because remember that array notation is just a dereference and some pointer math, and you can’t dereference a void*!↩︎

  88. You can also cast the void* to another type, but we haven’t gotten to casts yet.↩︎

  89. Or until the program exits, in which case all the memory allocated by it is freed. Asterisk: some systems allow you to allocate memory that persists after a program exits, but it’s system dependent, out of scope for this guide, and you’ll certainly never do it on accident.↩︎

  90. http://www.open-std.org/jtc1/sc22/wg14/www/docs/summary.htm#dr_460↩︎

  91. https://en.wikipedia.org/wiki/Bit_bucket↩︎

  92. “Bit” is short for binary digit. Binary is just another way of representing numbers. Instead of digits 0-9 like we’re used to, it’s digits 0-1.↩︎

  93. https://en.wikipedia.org/wiki/Two%27s_complement↩︎

  94. The industry term for a sequence of exactly, indisputably 8 bits is an octet.↩︎

  95. In general, f you have an \(n\) bit two’s complement number, the signed range is \(-2^{n-1}\) to \(2^{n-1}-1\). And the unsigned range is \(0\) to \(2^n-1\).↩︎

  96. https://en.wikipedia.org/wiki/ASCII↩︎

  97. https://en.wikipedia.org/wiki/List_of_information_system_character_sets↩︎

  98. https://en.wikipedia.org/wiki/Unicode↩︎

  99. Depends on if a char defaults to signed char or unsigned char↩︎

  100. https://en.wikipedia.org/wiki/Signed_number_representations#Signed_magnitude_representation↩︎

  101. My char is signed.↩︎

  102. https://en.wikipedia.org/wiki/IEEE_754↩︎

  103. This program runs as its comments indicate on a system with FLT_DIG of 6 that uses IEEE-754 base-2 floating point numbers. Otherwise, you might get different output.↩︎

  104. It’s really surprising to me that C doesn’t have this in the spec yet. In the C99 Rationale document, they write, “A proposal to add binary constants was rejected due to lack of precedent and insufficient utility.” Which seems kind of silly in light of some of the other features they kitchen-sinked in there! I’ll bet one of the next releases has it.↩︎

  105. https://en.wikipedia.org/wiki/Scientific_notation↩︎

  106. They’re the same except snprintf() allows you to specify a maximum number of bytes to output, preventing the overrunning of the end of your string. So it’s safer.↩︎

  107. https://en.wikipedia.org/wiki/ASCII↩︎

  108. We have to pass a pointer to badchar to strtoul() or it won’t be able to modify it in any way we can see, analogous to why you have to pass a pointer to an int to a function if you want that function to be able to change that value of that int.↩︎

  109. Each character has a value associated with it for any given character encoding scheme.↩︎

  110. In practice, what’s probably happening on your implementation is that the high-order bits are just being dropped from the result, so a 16-bit number 0x1234 being converted to an 8-bit number ends up as 0x0034, or just 0x34.↩︎

  111. Again, in practice, what will likely happen on your system is that the bit pattern for the original will be truncated and then just used to represent the signed number, two’s complement. For example, my system takes an unsigned char of 192 and converts it to signed char -64. In two’s complement, the bit pattern for both these numbers is binary 11000000.↩︎

  112. Not really—it’s just discarded regularly.↩︎

  113. Functions with a variable number of arguments.↩︎

  114. This is rarely done because the compiler will complain and having a prototype is the Right Thing to do. I think this still works for historic reasons, before prototypes were a thing.↩︎

  115. https://beej.us/guide/bgclr/html/split/ctype.html↩︎

  116. https://gustedt.wordpress.com/2010/08/17/a-common-misconsception-the-register-keyword/↩︎

  117. https://en.wikipedia.org/wiki/Processor_register↩︎

  118. https://en.wikipedia.org/wiki/Boids↩︎

  119. Historially, MS-DOS and Windows programs would do this differently than Unix. In Unix, the shell would expand the wildcard into all matching files before your program saw it, whereas the Microsoft variants would pass the wildcard expression into the program to deal with. In any case, there are arguments that get passed into the program.↩︎

  120. Since they’re just regular parameter names, you don’t actually have to call them argc and argv. But it’s so very idiomatic to use those names, if you get creative, other C programmers will look at you with a suspicious eye, indeed!↩︎

  121. ps, Process Status, is a Unix command to see what processes are running at the moment.↩︎

  122. https://en.wikipedia.org/wiki/Inception↩︎

  123. https://en.wikipedia.org/wiki/Shell_(computing)↩︎

  124. In Windows cmd.exe, type echo %errorlevel%. In PowerShell, type $LastExitCode.↩︎

  125. If you need a numeric value, convert the string with something like atoi() or strtol().↩︎

  126. In Windows CMD.EXE, use set FROTZ=value. In PowerShell, use $Env:FROTZ=value.↩︎

  127. https://pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html↩︎

  128. You can’t always just wrap the code in /* */ comments because those won’t nest.↩︎

  129. This isn’t really a macro—it’s technically an identifier. But it’s the only predefined identifier and it feels very macro-like, so I’m including it here. Like a rebel.↩︎

  130. A hosted implementation basically means you’re running the full C standard, probably on an operating system of some kind. Which you probably are. If you’re running on bare metal in some kind of embedded system, you’re probably on a standalone implementation.↩︎

  131. OK, I know that was a cop-out answer. Basically there’s an optional extension compilers can implement wherein they agree to limit certain types of undefined behavior so that the C code is more amenable to static code analysis. It is unlikely you’ll need to use this.↩︎

  132. Breakin’ the law… breakin’ the law…↩︎

  133. https://www.openmp.org/↩︎

  134. Technically we say that it has an incomplete type.↩︎

  135. Though some compilers have options to force this to occur—search for __attribute__((packed)) to see how to do this with GCC.↩︎

  136. super isn’t a keyword, incidentally. I’m just stealing some OOP terminology.↩︎

  137. Assuming 8-bit chars, i.e. CHAR_BIT == 8.↩︎

  138. https://en.wikipedia.org/wiki/Type_punning↩︎

  139. I just made up that number, but it’s probably not far off↩︎

  140. There’s some devil in the details with values that are stored in registers only, but we can safely ignore that for our purposes here. Also the C spec makes no stance on these “register” things beyond the register keyword, the description for which doesn’t mention registers.↩︎

  141. You’re very likely to get different numbers on yours.↩︎

  142. There is absolutely nothing in the spec that says this will always work this way, but it happens to work this way on my system.↩︎

  143. Even if E is NULL, it turns out, weirdly.↩︎

  144. https://beej.us/guide/bgclr/html/split/stringref.html#man-memcpy↩︎

  145. Your C compiler is not required to add padding bytes, and the values of any padding bytes that are added are indeterminate.↩︎

  146. This will vary depending on the architecture, but my system is little endian, which means the least-significant byte of the number is stored first. Big endian systems will have the 12 first and the 78 last. But the spec doesn’t dictate anything about this representation.↩︎

  147. It’s an optional feature, so it might not be there—but it probably is.↩︎

  148. I’m printing out the 16-bit values reversed since I’m on a little-endian machine and it makes it easier to read here.↩︎

  149. Assuming they point to the same array object.↩︎

  150. The Go Programming Language drew its type declaration syntax inspiration from the opposite of what C does.↩︎

  151. Not that other languages don’t do this—they do. It is interesting how many modern languages use the same operators for bitwise that C does.↩︎

  152. https://en.wikipedia.org/wiki/Bitwise_operation↩︎

  153. That is, us lowly developers aren’t supposed to know what’s in there or what it means. The spec doesn’t dictate what it is in detail.↩︎

  154. Honestly, it would be possible to remove that limitation from the language, but the idea is that the macros va_start(), va_arg(), and va_end() should be able to be written in C. And to make that happen, we need some way to initialize a pointer to the location of the first parameter. And to do that, we need the name of the first parameter. It would require a language extension to make this possible, and so far the committee hasn’t found a rationale for doing so.↩︎

  155. “This planet has—or rather had—a problem, which was this: most of the people living on it were unhappy for pretty much of the time. Many solutions were suggested for this problem, but most of these were largely concerned with the movement of small green pieces of paper, which was odd because on the whole it wasn’t the small green pieces of paper that were unhappy.” —The Hitchhiker’s Guide to the Galaxy, Douglas Adams↩︎

  156. Remember that char is just a byte-sized integer.↩︎

  157. Except for isdigit() and isxdigit().↩︎

  158. For example, we could store the code point in a big-endian 32-bit integer. Straightforward! We just invented an encoding! Actually not; that’s what UTF-32BE encoding is. Oh well—back to the grind!↩︎

  159. Ish. Technically, it’s variable width—there’s a way to represent code points higher than \(2^{16}\) by putting two UTF-16 characters together.↩︎

  160. There’s a special character called the Byte Order Mark (BOM), code point 0xFEFF, that can optionally precede the data stream and indicate the endianess. It is not required, however.↩︎

  161. Again, this is only true in UTF-16 for characters that fit in two bytes.↩︎

  162. https://en.wikipedia.org/wiki/UTF-8↩︎

  163. https://www.youtube.com/watch?v=MijmeoH9LT4↩︎

  164. Presumably the compiler makes the best effort to translate the code point to whatever the output encoding is, but I can’t find any guarantees in the spec.↩︎

  165. With a format specifier like "%.12s", for example.↩︎

  166. wcscoll() is the same as wcsxfrm() followed by wcscmp().↩︎

  167. Ish—things get funky with multi-char16_t UTF-16 encodings.↩︎

  168. https://en.wikipedia.org/wiki/Iconv↩︎

  169. http://site.icu-project.org/↩︎

  170. https://en.wikipedia.org/wiki/Core_dump↩︎

  171. Apparently it doesn’t do Unix-style signals at all deep down, and they’re simulated for console apps.↩︎

  172. Confusingly, sig_atomic_t predates the lock-free atomics and is not the same thing.↩︎

  173. If sig_action_t is signed, the range will be at least -127 to 127. If unsigned, at least 0 to 255.↩︎

  174. This is due to how VLAs are typically allocated on the stack, whereas static variables are on the heap. And the whole idea with VLAs is they’ll be automatically dellocated when the stack frame is popped at the end of the function.↩︎

  175. https://en.wikipedia.org/wiki/Goto#Criticism↩︎

  176. I’d like to point out that using goto in all these cases is avoidable. You can use variables and loops instead. It’s just that some people think goto produces the best code in those circumstances.↩︎

  177. https://en.wikipedia.org/wiki/Tail_call↩︎

  178. Which isn’t quite the same, since it’s an array, not a pointer to an int.↩︎

  179. A variable used here is an expression.↩︎

  180. Both “stack pointer” and “program counter” are related to the underlying architecture and C implementation, and are not part of the spec.↩︎

  181. The rationale here is that the program might store a value temporarily in a CPU register while it’s doing work on it. In that timeframe, the register holds the correct value, and the value on the stack might be out of date. Then later the register values would get overwritten and the changes to the variable lost.↩︎

  182. That is, remain allocated until the program ends with no way to free it.↩︎

  183. This works because in C, pointers are the same size regardless of the type of data they point to. So the compiler doesn’t need to know the size of the struct node at this point; it just needs to know the size of a pointer.↩︎

  184. https://en.wikipedia.org/wiki/Complex_number↩︎

  185. This was a harder one to research, and I’ll take any more information anyone can give me. I could be defined as _Complex_I or _Imaginary_I, if the latter exists. _Imaginary_I will handle signed zeros, but _Complex_I might not. This has implications with branch cuts and other complex-numbery-mathy things. Maybe. Can you tell I’m really getting out of my element here? In any case, the CMPLX() macros behave as if I were defined as _Imaginary_I, with signed zeros, even if _Imaginary_I doesn’t exist on the system.↩︎

  186. The simplicity of this statement doesn’t do justice to the incredible amount of work that goes into simply understanding how floating point actually functions. https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/↩︎

  187. This is the only one that doesn’t begin with an extra leading c, strangely.↩︎

  188. Some architectures have different sized data that the CPU and RAM can operate with at a faster rate than others. In those cases, if you need the fastest 8-bit number, it might give you have a 16- or 32-bit type instead because that’s just faster. So with this, you won’t know how big the type is, but it will be least as big as you say.↩︎

  189. Namely, the system has 8, 16, 32, or 64 bit integers with no padding that use two’s complement representation, in which case the intN_t variant for that particular number of bits must be defined.↩︎

  190. On Earth, anyway. Who know what crazy systems they use out there↩︎

  191. OK, don’t murder me! GMT is technically a time zone while UTC is a global time system. Also some countries might adjust GMT for daylight saving time, whereas UTC is never adjusted for daylight saving time.↩︎

  192. Admittedly, there are more than two.↩︎

  193. https://en.wikipedia.org/wiki/Unix_time↩︎

  194. https://beej.us/guide/bgclr/html/split/time.html#man-strftime↩︎

  195. You will on POSIX, where time_t is definitely an integer. Unfortunately the entire world isn’t POSIX, so there we are.↩︎

  196. https://en.wikipedia.org/wiki/POSIX_Threads↩︎

  197. I’m more a fan of shared-nothing, myself, and my skills with classic multithreading constructs are rusty, to say the least.↩︎

  198. Yes, pthreads with a “p”. It’s short for POSIX threads, a library that C11 borrowed liberally from for its threads implementation.↩︎

  199. Per §7.1.4¶5.↩︎

  200. Unless you thrd_detach(). More on this later.↩︎

  201. Though I don’t think they have to be. It’s just that the threads don’t seem to get rescheduled until some system call like might happen with a printf()… which is why I have the printf() in there.↩︎

  202. Short for “mutual exclusion”, AKA a “lock” on a section of code that only one thread is permitted to execute.↩︎

  203. That is, your process will go to sleep.↩︎

  204. You might have expected it to be “time from now”, but you’d just like to think that, wouldn’t you!↩︎

  205. And that’s why they’re called condition variables!↩︎

  206. I’m not saying it’s aliens… but it’s aliens. OK, really more likely another thread might have been woken up and gotten to the work first.↩︎

  207. Survival of the fittest! Right? I admit it’s actually nothing like that.↩︎

  208. The __STDC_VERSION__ macro didn’t exist in early C89, so if you’re worried about that, check it with #ifdef.↩︎

  209. The reason for this is when optimized, my compiler has put the value of x in a register to make the while loop fast. But the register has no way of knowing that the variable was updated in another thread, so it never sees the 3490. This isn’t really related to the all-or-nothing part of atomicity, but is more related to the synchronization aspects in the next section.↩︎

  210. Until I say otherwise, I’m speaking generally about sequentially consistent operations. More on what that means soon.↩︎

  211. Sanest from a programmer perspective.↩︎

  212. Apparently C++23 is adding this as a macro.↩︎

  213. The spec notes that they might differ in size, representation, and alignment.↩︎

  214. I just pulled that example out of nowhere. Maybe it doesn’t matter on Intel/AMD, but it could matter somewhere, dangit!↩︎

  215. C++ elaborates that if the signal is the result of a call to raise(), it is sequenced after the raise().↩︎

  216. https://en.wikipedia.org/wiki/Test-and-set↩︎

  217. Because consume is all about the operations that are dependent on the value of the acquired atomic variable, and there is no atomic variable with a fence.↩︎

  218. https://www.youtube.com/watch?v=A8eCGOqgvH4↩︎

  219. https://www.youtube.com/watch?v=KeLBd2EJLOU↩︎

  220. https://preshing.com/archives/↩︎

  221. https://preshing.com/20120612/an-introduction-to-lock-free-programming/↩︎

  222. https://preshing.com/20120913/acquire-and-release-semantics/↩︎

  223. https://preshing.com/20130702/the-happens-before-relation/↩︎

  224. https://preshing.com/20130823/the-synchronizes-with-relation/↩︎

  225. https://preshing.com/20140709/the-purpose-of-memory_order_consume-in-cpp11/↩︎

  226. https://preshing.com/20150402/you-can-do-any-kind-of-atomic-read-modify-write-operation/↩︎

  227. https://en.cppreference.com/w/c/atomic/memory_order↩︎

  228. https://en.cppreference.com/w/c/language/atomic↩︎

  229. https://docs.microsoft.com/en-us/windows/win32/dxtecharts/lockless-programming↩︎

  230. https://www.reddit.com/r/C_Programming/↩︎

  231. Unless you compile with optimizations on (probably)! But I think when it does this, it’s not behaving to spec.↩︎

  232. https://beej.us/guide/bgclr/html/split/stdlib.html#man-exit↩︎

  233. https://beej.us/guide/bgclr/html/split/stdlib.html#man-abort↩︎

  234. https://en.wikipedia.org/wiki/Data_structure_alignment↩︎


| Contents |