You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Here's a list of mildly interesting things about the C language that I learned over time. There are many more mildly interesting features of C++, but the language is literally known for being weird, whereas C is usually considered smaller and simpler, so this is (almost) only about C.
Here's a list of mildly interesting things about the C language that I learned mostly by consuming Clang's ASTs. Although surprises are getting sparser, I might continue to update this document over time.
There are many more mildly interesting features of C++, but the language is literally known for being weird, whereas C is usually considered smaller and simpler, so this is (almost) only about C.
## 1. Combined type and variable/field declaration, inside a struct scope [https://godbolt.org/g/Rh94Go]
fay59
revised
this gist Sep 26, 2018.
1 changed file
with
20 additions
and
0 deletions.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10. What’s an lvalue, anyway: https://godbolt.org/g/5echfM
11. Void globals: https://godbolt.org/z/C52Wn2
12. Alignment implications of bitfields: https://godbolt.org/z/KmB4CB
Special mentions:
1. The power of UB: https://godbolt.org/g/H6mBFT. This happens because:
1. LLVM sees that `side_effects` has only two possible values: NULL (the initial value) or `this_is_not_directly_called_by_main` (if `bar` is called)
2. LLVM sees that `side_effects` is called, and it is UB to call a null pointer
3. UB is impossible, so LLVM assumes that `bar` will have executed by the time `main` runs rather than face the consequences
4. Under this assumption, `side_effects` is always `this_is_not_directly_called_by_main`.
2. A macro that tells you if an expression is an integer constant, if you can't use `__builtin_constant_p`: https://godbolt.org/g/a41gmx (from Martin Uecker, on the Linux kernel ML)
3. You can make some pretty weird stuff in C, but for a real disaster, you need C++. Labels inside expression statements in really weird places: https://godbolt.org/g/k9wDRf.
(I have a bunch of mildly interesting in C++ too, but so does literally everyone who’s used the language for more than an hour, so it’s not as interesting.)
Here's a list of mildly interesting things about the C language that I learned over time. There are many more mildly interesting features of C++, but the language is literally known for being weird, whereas C is usually considered smaller and simpler, so this is (almost) only about C.
## 1. Combined type and variable/field declaration, inside a struct scope [https://godbolt.org/g/Rh94Go]
```c
struct foo {
struct bar {
int x;
} baz;
};
voidfrob() {
struct bar b; // <-- defined in body of `struct foo`
}
```
## 2. Compound literals are lvalues [https://godbolt.org/g/Zup5ZB]
You can make some pretty weird stuff in C, but for a real disaster, you need C++.
```c++
classfoo {
int x;
public:
foo();
};
foo::foo() : x(({ a: 4; })) {
goto a;
}
```
Needless to say, statement expressions are not standard C++ (or standard C), but if your compiler has them, chances are that you can use them in *really* interesting ways.
fay59
revised
this gist Sep 10, 2018.
1 changed file
with
1 addition
and
1 deletion.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I thought that I’d throw together the quirks of the C language that I learned about over time for all to see. Some are plain weird, some are actually kinda neat. Most are plain weird.
Here's a list of mildly interesting things about the C language that I learned over time.
1. Combined type and variable/field declaration, inside a struct scope: https://godbolt.org/g/Rh94Go
2. Compound literals are lvalues: https://godbolt.org/g/Zup5ZB
@@ -10,6 +10,8 @@ I thought that I’d throw together the quirks of the C language that I learned
8. Modifiers to array sizes in parameter definitions: https://godbolt.org/z/SKS38s
10. What’s an lvalue, anyway: https://godbolt.org/g/5echfM
11. Void globals: https://godbolt.org/z/k2sBJs
12. Alignment implications of bitfields: https://godbolt.org/z/KmB4CB
Special mentions:
@@ -18,7 +20,7 @@ Special mentions:
2. LLVM sees that `side_effects` is called, and it is UB to call a null pointer
3. UB is impossible, so LLVM assumes that `bar` will have executed by the time `main` runs rather than face the consequences
4. Under this assumption, `side_effects` is always `this_is_not_directly_called_by_main`.
2. A macro that tells you if an expression is an integer constant: https://godbolt.org/g/a41gmx (from Martin Uecker, on the Linux kernel ML)
2. A macro that tells you if an expression is an integer constant, if you can't use `__builtin_constant_p`: https://godbolt.org/g/a41gmx (from Martin Uecker, on the Linux kernel ML)
3. You can make some pretty weird stuff in C, but for a real disaster, you need C++. Labels inside expression statements in really weird places: https://godbolt.org/g/k9wDRf.
(I have a bunch of weird things in C++ too, but so does literally everyone who’s used the language for more than an hour, so it’s not as interesting.)
(I have a bunch of mildly interesting in C++ too, but so does literally everyone who’s used the language for more than an hour, so it’s not as interesting.)
fay59
revised
this gist Aug 18, 2018.
1 changed file
with
4 additions
and
4 deletions.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -14,10 +14,10 @@ I thought that I’d throw together the quirks of the C language that I learned
Special mentions:
1. The power of UB: https://godbolt.org/g/H6mBFT. This happens because:
1. LLVM sees that `side_effects` has only two possible values: NULL (the initial value) or `this_is_not_directly_called_by_main` (if `bar` is called)
2. LLVM sees that `side_effects` is called, and it is UB to call a null pointer
3. UB is impossible, so LLVM assumes that `bar` will have executed by the time `main` runs rather than face the consequences
4. Under this assumption, `side_effects` is always `this_is_not_directly_called_by_main`.
1. LLVM sees that `side_effects` has only two possible values: NULL (the initial value) or `this_is_not_directly_called_by_main` (if `bar` is called)
2. LLVM sees that `side_effects` is called, and it is UB to call a null pointer
3. UB is impossible, so LLVM assumes that `bar` will have executed by the time `main` runs rather than face the consequences
4. Under this assumption, `side_effects` is always `this_is_not_directly_called_by_main`.
2. A macro that tells you if an expression is an integer constant: https://godbolt.org/g/a41gmx (from Martin Uecker, on the Linux kernel ML)
3. You can make some pretty weird stuff in C, but for a real disaster, you need C++. Labels inside expression statements in really weird places: https://godbolt.org/g/k9wDRf.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I thought that I’d throw together the quirks of the C language that I learned about over time for all to see. Some are plain weird, some are actually kinda neat. Most are plain weird.
1. Combined type and variable/field declaration, inside a struct scope: https://godbolt.org/g/Rh94Go
2. Compound literals are lvalues: https://godbolt.org/g/Zup5ZB
10. What’s an lvalue, anyway: https://godbolt.org/g/5echfM
Special mentions:
1. The power of UB: https://godbolt.org/g/H6mBFT. This happens because:
1. LLVM sees that `side_effects` has only two possible values: NULL (the initial value) or `this_is_not_directly_called_by_main` (if `bar` is called)
2. LLVM sees that `side_effects` is called, and it is UB to call a null pointer
3. UB is impossible, so LLVM assumes that `bar` will have executed by the time `main` runs rather than face the consequences
4. Under this assumption, `side_effects` is always `this_is_not_directly_called_by_main`.
2. A macro that tells you if an expression is an integer constant: https://godbolt.org/g/a41gmx (from Martin Uecker, on the Linux kernel ML)
3. You can make some pretty weird stuff in C, but for a real disaster, you need C++. Labels inside expression statements in really weird places: https://godbolt.org/g/k9wDRf.
(I have a bunch of weird things in C++ too, but so does literally everyone who’s used the language for more than an hour, so it’s not as interesting.)