2 ms·
C has a fair amount of overcomplicated-ness. For example, I can never remember integer promotion rules, or other implicit conversions [0]. It's like a Rube Gol
by trealira 3y ago
C has a fair amount of overcomplicated-ness.
For example, I can never remember integer promotion rules, or other implicit conversions [0]. It's like a Rube Goldberg machine of rules. I often end up looking it up. I would rather it were more like Rust, where explicit casts are always required, even if it's seen as annoying sometimes.
I'll give a really basic example. For example, it's not that obvious what should happen in this situation. Someone unfamiliar with C type conversion rules could assume that c is zero-extended to the size of an unsigned int and stored in u. What actually happens is that c is promoted to int, thereby sign-extending it, and then that result is cast to unsigned int.
signed char c = -1;
// u == UINT_MAX, assuming two's complement signed integers
unsigned int u = c;
If you want the zero-extension behavior, you need to cast c to unsigned char, first. If you cast it to unsigned int, c will still be implicitly promoted to int (sign-extending it first) and then it casts the result of that to unsigned int.
signed char c = -1;
// u == 0xFF, if c is 8 bits
unsigned int u = (unsigned char)c;
// u1 == UINT_MAX
unsigned int u1 = (unsigned int)c
Anyway, I just think C could be simpler. It could have a simpler syntax, and simpler semantics, without changing the basic model of everything being functions, pointers, primitive types, and structures.
[0]: https://en.cppreference.com/w/c/language/conversion https://en.cppreference.com/w/c/language/conversion