> The best C programming trick is to use no tricks.
Yes! C's monotony is its killer feature. Take away power when you are coding and you have more power to share the code and debug it.The very notion of there being tricks and that knowing them makes you better is something I hate about C and C++. Most tricks I read here are bandaids over usability issues the languages have. Yes, they alleviate an issue but may introduce unexpected consequences and distance your dialect from the rest of the community.
I am so very thankful that C and C++ are no longer the only options for low level, non garbage collected programming.
E.g. this is 'named, optional arguments East Egg' is a useful trick which also improves readability:
my_func((my_struct) { .bla = 1, .blub = 2, });The compatibility with the C library is up to C11 for example.
Google even sponsored a massive effort to clean the Linux kernel of their use.
A subset of designated initializers is supported in C++20.
Took them only 21 years... And this standard being so recent, most projects won't have that for years to come.
In your example,
Point point = {.z = 2.0, .x = 1.0};
produces a compilation error. This is annoying, but workable. And at least it's a compile-time error, rather than a bug that perniciously sneaks into production.It's interesting that the other C descendant Objective-C has decided to "respect" its C subset instead of messing with it, with the result that new C standards are automatically supported in ObjC.
VLAs are dead, a broken design fixed in C11 by removing it from the standard, no idea why everyone keeps referring to them.
typedef struct pixel {
int r;
int g;
int b;
} pixel_t;
#define PIXEL(red, green, blue) \
(pixel_t) \
{ \
.r = (red), \
.g = (green), \
.b = (blue)
}
Then I could invoke the function void display_pixel(pixel_t pixel);
by calling, display_pixel(PIXEL(255, 255, 255));
And you could even, return PIXEL(255, 255, 255);
Like magic. In other words, how do you pretend that you have syntax-level OOP in C... On retrospect, the macro could reduce readability, writing the argument explicitly may be better. display_pixel((pixel_t) {.r = 255, .g = 255, .b = 255});