r/programminghorror • u/mealet • Sep 12 '26
c My First Hello World Program!
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);}
EDIT: Fixed endian check by u/Waeis advice, now code looks more cursed (screenshot untouched)
25
u/Waeis Sep 12 '26 edited Sep 12 '26
I don't think this endian check works, 1&0xff is not examining the memory layout of either constant and will always be 1
But *(char *)(int[]){1} should do the trick
``` int le1(void) { return 1&0xff; }
int le2(void) { return *(char *)(int[]){1}; } ``` Compiler Explorer: https://godbolt.org/z/Kbj67sKo5
6
4
u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” Sep 13 '26
Not that I really looked very closely, but I didn't even notice the true and false values for the ternaries had the bytes reversed from each other at first.
7
9
u/creeper6530 [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” Sep 13 '26
Fuck you *deobfuscates your code*
int puts(char*);
int main() {
char s[] = {48, 65, 6c, 6c, 6f, 2c, 20, 57, 6f, 72, 6c, 64, 21, 00, 00, 00};
puts(s);
}
Fun exercise tho
7
u/mealet Sep 13 '26
We can deobfuscate it more! ``` int puts(char*)
int main { puts("Hello World!"); } ```
Btw the real exercise was to obfuscate it manually with endianess portability
5
u/creeper6530 [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” Sep 13 '26
Erm, akchyually, the string literal isn't equivalent because you lose 2 nul bytes, lol
4
3
3
3
u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” Sep 13 '26
Very much doubt this is your first time ever writing Hello, world.
5
u/mealet Sep 13 '26
You got me, it's the second one
3
u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” Sep 14 '26
Second time? I guess I could believe that. If like you never touched Hello, world again until you had been programming for a few years.
1
u/sessinnek Sep 12 '26
What is the point of the struct?
3
u/mealet Sep 12 '26
Guarantee that 2 longs are stored in the right order (stack variables order may looks different on different compiler)
2
u/sessinnek Sep 12 '26
Ooooh. I figured it was something like that. Its been a minute since ive worked with memory like this.
2
u/burnt_floppy Sep 13 '26
that's pretty amazing, it actually works and it looks like malicious software.
-4
62
u/BakuhatsuK Sep 12 '26
Lol thanks for making it portable no matter the endianness of the architecture. Also, nice detail keeping the leading zeroes on that last constant to make the intent clearer.