C Addition Function Hack
finally worked out how to make a function for addition. C isn't as hard as people say ; cat add.c #include <stdio.h> #include <stddef.h> int add(int m, int n) { struct add { char a[m]; char b[n]; char c; }; return offsetof(struct add, c); } int main(void) { printf("%d\n", add(2, 3)); return 0; } ; cc add.c ; ./a.out 5
A programming post shows a supposedly simple C function for addition, but the code abuses struct layout and offsetof to calculate 2 + 3. The joke is that the author claims C is easy while using an unnecessarily complicated and obscure trick to print 5. It is shared as a reaction to overengineering a simple programming task.
code screenshot / скриншот кодаC source code / исходный код на Cterminal window / окно терминалаtext post / текстовый постprogramming function / функция программированияstruct declaration / объявление структурыcharacter arrays / массивы символовterminal prompt / приглашение терминалаmonospace text / моноширинный текст
- @profunctor_io / 5823 2020-11-18
- @right_wing_dev_squads / 600 2020-11-19
- @right_wing_dev_squads / 10859 2024-11-07
- @gentoomemes / 14562 2024-11-07
- Type
- image
- Size
- 558×800 · 48.6 KiB
- Mood
- sarcastic
- Language
- en
- Added