Chao-c' on Nostr: Please, can anybody explain this #C #programming #riddle? It was originally intended ...
Please, can anybody explain this #C #programming #riddle? It was originally intended as example for first lesson of C course for beginners, but I fell into trap!
The source:
#include <stdio.h>
void main(void)
{
int a=1, b=1;
printf("Hello++: a==%d, b==%d, a++==%d, ++b==%d, a==%d, b==%d, a--==%d, --b==%d, a==%d, b==%d\n",a,b,a++,++b,a,b,a--,--b,a,b);
}
The output:
Hello++: a==1, b==1, a++==0, ++b==1, a==1, b==1, a--==1, --b==1, a==1, b==1
(Hint: it works as expected, if you separate the single printf into 5 printf-s with 2 arguments each. The problem is unexpected behavior of C++-ish ++ and -- operators, when used multiple times as multiple function arguments. Remainds me of multiple evaluation of macro arguments, or something like that)
Published at
2026-03-28 10:29:20 UTCEvent JSON
{
"id": "3b3251492d9e7cf8f20b07be5c2216c1694f465c13b9d11318ef1d6de9f68b4b",
"pubkey": "fa2f9d5d030b561875226d56c5b3c50de1f52f934abe9fe5260360bfcb453c3c",
"created_at": 1774693760,
"kind": 1,
"tags": [
[
"t",
"c"
],
[
"t",
"programming"
],
[
"proxy",
"https://f.cz/@xChaos/116306330286661319",
"web"
],
[
"t",
"include"
],
[
"t",
"riddle"
],
[
"proxy",
"https://f.cz/users/xChaos/statuses/116306330286661319",
"activitypub"
],
[
"L",
"pink.momostr"
],
[
"l",
"pink.momostr.activitypub:https://f.cz/users/xChaos/statuses/116306330286661319",
"pink.momostr"
],
[
"-"
]
],
"content": "Please, can anybody explain this #C #programming #riddle? It was originally intended as example for first lesson of C course for beginners, but I fell into trap! \n\nThe source:\n\n#include \u003cstdio.h\u003e\n\nvoid main(void)\n{\n int a=1, b=1;\n printf(\"Hello++: a==%d, b==%d, a++==%d, ++b==%d, a==%d, b==%d, a--==%d, --b==%d, a==%d, b==%d\\n\",a,b,a++,++b,a,b,a--,--b,a,b);\n}\n\nThe output:\n\nHello++: a==1, b==1, a++==0, ++b==1, a==1, b==1, a--==1, --b==1, a==1, b==1\n\n(Hint: it works as expected, if you separate the single printf into 5 printf-s with 2 arguments each. The problem is unexpected behavior of C++-ish ++ and -- operators, when used multiple times as multiple function arguments. Remainds me of multiple evaluation of macro arguments, or something like that)",
"sig": "3a26c537628c66b070d2ea088eb81e6174dbc0d6ac4ea3535460d01fdb5867314fa14c06015d8c4e8e584730abee7ef3931a980d26bb7151bfbb4cfc11689976"
}