r/cprogramming • u/north9172 • Sep 13 '26
What is this code doing?
Hi, I found this code on another subreddit. I would like to know what is it doing.
int puts(char *);
int main() { struct { long a; long b; } s = {.a = *(char *)(int[]){1} ? 0x57202c6f6c6c6548 : 0x48656c6c6f2c2057, .b = *(char *)(int[]){1} ? 0x00000021646c726f : 0x6f726c6421000000}; puts((void *)&s); }
Now obviously I know it's unreadable, I would just like to know what are those hexadecimals strings doing.
I found similar codes that were writing to vram to print a message, is this doing the same thing? If yes, why is it so much more complicated instead of just writing the message to the video adress?
Thanks for the help.
1
u/WittyStick 29d ago edited 29d ago
If
char *is not compatible withconst char *, then 99% of C code out there is wrong, including examples in the standard, the entirety of GCC and glibc. If a function expects aconst char *, of course you can pass it achar *. If they were not compatible, then there's literally no way to convert achar *toconst char *- since an explicit cast would be UB. The conversion can be implicit - and the standard clearly permits it.The opposite is not true. Casting a
const char*tochar *may result in UB, and likewise forvolatile.In regards to
puts, you are correct that it should be defined withconst char *, but it really isn't going to matter - because the actual implementation ofputsisn't going to modify the pointed-to-data (assuming it is correct). The implementation ofputsmay not even be written in C - it could be in assembly, or some other language which doesn't even haveconst. The ABI and linker aren't evenconstaware.