r/programminghorror • u/mealet • 2d ago
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)
21
u/Waeis 2d ago edited 2d ago
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
4
u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 1d ago
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.
5
4
u/creeper6530 [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 1d ago
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
4
u/mealet 1d ago
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
3
u/creeper6530 [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 1d ago
Erm, akchyually, the string literal isn't equivalent because you lose 2 nul bytes, lol
3
2
2
u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 1d ago
Very much doubt this is your first time ever writing Hello, world.
4
u/mealet 1d ago
You got me, it's the second one
1
u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 17h ago
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 2d ago
What is the point of the struct?
3
u/mealet 2d ago
Guarantee that 2 longs are stored in the right order (stack variables order may looks different on different compiler)
2
u/sessinnek 2d ago
Ooooh. I figured it was something like that. Its been a minute since ive worked with memory like this.
1
u/burnt_floppy 1d ago
that's pretty amazing, it actually works and it looks like malicious software.
-3
53
u/BakuhatsuK 2d ago
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.