3 ms·
I've been programming in C forever, one advantage is that the language has not evolved much (especially compared with C++), but it has evolved. There was the b
by jhallenworld 4y ago
I've been programming in C forever, one advantage is that the language has not evolved much (especially compared with C++), but it has evolved.
There was the big K&R C to ANSI C function declaration transition. For portable code, you used K&R C well into the 90s (because older machines only had the K&R compiler), or used ugly macros to automatically convert from ANSI to K&R.
Another was the addition of 'const' to the language. It used to be said that const was a virus: once you start using it, you need to use it universally in your entire code-base.
A more recent big one is stdint.h (types like uint32_t). To correctly use these types you must also use the macros in inttypes.h for scanf and printf conversions. IMHO, they both should have been in the same header files, they go along with each other.
So in the old days, you would say:
unsigned int x;
printf("%u\n", x);
But now, you should tediously say:
uint32_t x;
printf("%"PRIu32"\n", x);
(Because uint32_t might be defined as a long even if it's the same size as in int, so you will get compiler warnings. You could use %lu, but then you get compiler warnings the other way.)
Another: On UNIX, we don't have to worry about "wide" characters (use UTF-8 instead) and wide strings, but you certainly do on Windows. "Wide" characters are obsolete but Windows is stuck with them since they are built into the OS.
- suprjami 4y ago> stdint.h / inttypes.h Including inttypes also includes stdint. You use stdint where you need the types only, like a header or non-IO module, and use inttypes where you need the print formatting. I agree it's a bit weird but that's the way I understand the intended usage.
- a1369209993 4y ago> It used to be said that const was a virus: once you start using it, you need to use it universally in your entire code-base. In order for const to actually work for what it's supposed to do, it does have to be viral in the direction of data flow. You should start by adding const to function arguments that point to data the function only reads (and doesn't pass the pointer to any subroutines) and expand from there. Eg: _Bool isurl(char /*const here*/* s) { while(isalpha(*s)) s++; return *s == ':'; } /* s is never written through */ Then anything that passes pointers (only) to functions like isurl, and so on as is convenient.