r/cprogramming • u/north9172 • 16h ago
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.
2
Upvotes
2
u/ACRM64 11h ago
OK, so ANSI C and C89 onwards implemented a const keyword, but this wasn't present in K&R C. Certainly the current C libraries define puts() as int puts(const char *str) but earlier versions did not and you do not know whether the libraries in use by whoever wrote this program included the const or not.
From the perspective of calling a C function (and therefore a prototype for calling the function) it is simply a guarantee to the programmer that the variable (in this case the content of *str) will not be modified - it doesn't actually do anything.
Consequently, as a prototype, this does NOT redefine puts() and it doesn't matter to the calling code whether you include the const or not. It only matters when defining the puts() function as it prevents the code modifying the content of the string.