Reading GCC error messages

Every GCC diagnostic has the same shape: scratch.c:3:13: error: initialization discards 'const' qualifier from pointer target type [-Werror=discarded-qualifiers] 3 | char *s = "scratching surface"; | ^~~~~~~~~~~~~~~~~~~~ cc1: all warnings being treated as errors Line 1: file:line:col: severity: message [flag] scratch.c:3:13: where. Columns start at 1. Most terminals/editors make this clickable. severity: error stops the build; warning doesn’t; note: adds context to the diagnostic above it. [-Werror=discarded-qualifiers]: the flag that controls this check. -Werror= means it’s really the warning -Wdiscarded-qualifiers, promoted to an error by -Werror. Search man gcc for /Wdiscarded-qualifiers. -Wno-<name> disables it, which is rarely the right fix. The message has a fixed vocabulary The first words say where in the code the problem is: ...

October 3, 2026 · 2 min · Kshitij Patil
Notes

C string literals are read-only, but not const

A string literal like "/bin/sh" is stored in .rodata, which is mapped read-only (see where things live in a C program). But its C type is plain char[N], not const char[N] (C++ fixed this, C kept it for historical reasons). So the compiler happily lets you do this: char *p = "/bin/sh"; // compiles without a warning by default p[0] = 'X'; // undefined behavior; in practice SIGSEGV $ ./ub Segmentation fault (exit status 139 = 128 + SIGSEGV) -Wwrite-strings: make the compiler tell you -Wwrite-strings gives literals the type const char[N], so pointing a plain char * at one becomes a warning (an error with -Werror): ...

October 3, 2026 · 2 min · Kshitij Patil
Notes