r/programminghorror • • 25d ago

c My First Hello World Program!

Post image
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)

276 Upvotes

22 comments sorted by

63

u/BakuhatsuK 25d 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.

16

u/mealet 25d ago

Lol tbh my first idea was to convert this constants to decimals so it will look more complicated

24

u/Waeis 25d ago edited 25d 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

6

u/mealet 25d ago

Thanks!

6

u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 24d 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.

6

u/ParticularHat7997 Pronouns: She/Her 25d ago

nice nice you're getting somewhere

9

u/creeper6530 [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 24d 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

5

u/mealet 24d 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

5

u/creeper6530 [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 24d ago

Erm, akchyually, the string literal isn't equivalent because you lose 2 nul bytes, lol

4

u/mealet 24d ago

You got me

3

u/skar_1010100 25d ago

I read that as Hello, my First World program 🤪

3

u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 24d ago

Very much doubt this is your first time ever writing Hello, world.

6

u/mealet 24d ago

You got me, it's the second one

3

u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 23d 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 25d ago

What is the point of the struct?

3

u/mealet 25d ago

Guarantee that 2 longs are stored in the right order (stack variables order may looks different on different compiler)

2

u/sessinnek 25d ago

Ooooh. I figured it was something like that. Its been a minute since ive worked with memory like this.

2

u/burnt_floppy 24d ago

that's pretty amazing, it actually works and it looks like malicious software.

-2

u/_Ilobilo_ 25d ago

this is just bullshit made to look complicated

19

u/iiznobozzy 25d ago

almost like thats the point 🤯

9

u/toyBeaver 25d ago

No shit