2 ms·
I taught first-semester C for several years, and still got a couple of these wrong - although, to be honest, the way we taught the class at my alma mater we ste
by einpoklum 4y ago
I taught first-semester C for several years, and still got a couple of these wrong - although, to be honest, the way we taught the class at my alma mater we steered well clear of these situations. We told students that C perform type promotions and automatic conversions, but either had them use a uniform type to begin with - int typically - or told them to convert explicitly and avoid
Still, the most important suggestion I would make here is: Always compile with warnings enabled, in particular:
* -Wstrict-overflow=1 and maybe even -Wstrict-overflow=3
* -Wsign-compare
* -Wsign-conversion
* -Wfloat-conversion
* -Wconversion
* -Wshift-negative-value
(or just `-Wall -Wextra`)
And maybe also:
* -fsanitize=signed-integer-overflow
* -fsanitize=float-cast-overflow
(These are GCC flags, should work for clang as well.)