summary refs log tree commit diff
path: root/www/text.causal.agency/003-pleasant-c.7
blob: 538f1c079e916ae5a8cce2b0c7844524784fccf5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
.Dd September 30, 2018
.Dt PLEASANT-C 7
.Os "Causal Agency"
.
.Sh NAME
.Nm Pleasant C
.Nd it's good, actually
.
.Sh DESCRIPTION
I've been writing a lot of C lately
and actually find it very pleasant.
I want to talk about some of its ergonomic features.
These are C99 features unless otherwise noted.
.
.Ss Initializer syntax
Struct and union initializer syntax
is well generalized.
Designators can be chained,
making initializing nested structs easy,
and all uninitialized fields are zeroed.
.
.Bd -literal -offset indent
struct {
	struct pollfd fds[2];
} loop = {
	.fds[0].fd = STDIN_FILENO,
	.fds[1].fd = STDOUT_FILENO,
	.fds[0].events = POLLIN,
	.fds[1].events = POLLOUT,
};
.Ed
.
.Ss Variable-length arrays
VLAs can be multi-dimensional,
which can avoid manual stride multiplications
needed to index a flat
.Xr malloc 3 Ap d
array.
.
.Bd -literal -offset indent
uint8_t glyphs[len][height][width];
fread(glyphs, height * width, len, stdin);
.Ed
.
.Ss Incomplete array types
The last field of a struct can be an
.Dq incomplete
array type,
which means it doesn't have a length.
A variable amount of space for the struct can be
.Xr malloc 3 Ap d ,
or the struct can be used as
a sort of pointer with fields.
.
.Bd -literal -offset indent
struct Line {
	enum Filter type;
	uint8_t data[];
} *line = &png.data[1 + lineSize()];
.Ed
.
.Ss Anonymous struct and union fields (C11)
Members of structs or unions
which are themselves structs or unions
can be unnamed.
In that case,
each of the inner fields
is treated as a member of the outer struct or union.
This makes working with tagged unions nicer.
.
.Bd -literal -offset indent
struct Message {
	enum { Foo, Bar } type;
	union {
		uint8_t foo;
		uint32_t bar;
	};
} msg = { .type = Foo, .foo = 0xFF };
.Ed
.
.Ss Static assert (C11)
Assertions can be made at compile time.
Most useful for checking sizes of structs.
.
.Bd -literal -offset indent
static_assert(13 == sizeof(struct PNGHeader), "PNG IHDR size");
.Ed
.
.Ss Leading-break switch
This one is just an odd style choice
I came across that C happens to allow.
To prevent accidental fall-through
in switch statements,
you can put breaks before the case labels.
.
.Bd -literal -offset indent
while (0 < (opt = getopt(argc, argv, "h:w:"))) {
	switch (opt) {
		break; case 'h': height = optarg;
		break; case 'w': width = optarg;
		break; default:  return EX_USAGE;
	}
}
.Ed
.
.Sh AUTHORS
.An Mt june@causal.agency
.
.Pp
This document is produced from
.Xr mdoc 7
source available from
.Lk https://code.causal.agency/june/src/src/branch/master/www/text.causal.agency "Code Toilet"
.
.Sh CAVEATS
This isn't meant to be advice.
It's just how I like to write C,
and I don't
.Dq ship
software in C.
'3' class='logmsg'> 2019-05-20Declare vasprintf(3) for GNUJune McEnroe who the fuck is scraeming "#define _GNU_SOURCE" at my house. show yourself, coward. i will never #define _GNU_SOURCE 2019-05-20Fix comparison warning in ttpreJune McEnroe 2019-05-20Add AuthorityJune McEnroe 2019-05-19Specify precedence of unary versions of operatorsJune McEnroe 2019-05-18Add compound assignment operators to orderJune McEnroe 2019-05-15Support simple assignment in orderJune McEnroe 2019-05-15Implement sizeof in orderJune McEnroe 2019-05-15Add orderJune McEnroe 2019-05-12Add T suffix in bitJune McEnroe 2019-05-10Highlight yacc and lex files as CJune McEnroe Their %-prefixed directives should probably be highlighted Macro. 2019-05-10Use val instead of suboptargJune McEnroe suboptarg doesn't exist in GNU. Hopefully BSD getsubopt also sets val on failure? 2019-05-09Add Parable of the SowerJune McEnroe 2019-05-07Add bit without buildJune McEnroe Need to do some stuff in the Makefile for lex and yacc and generating HTML pages for it. 2019-05-04Fix MANDIR typoJune McEnroe 2019-05-04Move relay to binJune McEnroe