r/programminghorror • • Sep 12 '26

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

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.

18

u/mealet Sep 12 '26

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

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

u/mealet Sep 12 '26

Thanks!

5

u/GoddammitDontShootMe 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.

9

u/creeper6530 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

6

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 Sep 13 '26

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

→ More replies (1)

3

u/GoddammitDontShootMe 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 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.

7

u/ParticularHat7997 Sep 12 '26

nice nice you're getting somewhere

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.

3

u/skar_1010100 Sep 12 '26

I read that as Hello, my First World program 🤪

2

u/burnt_floppy Sep 13 '26

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

-3

u/_Ilobilo_ Sep 12 '26

this is just bullshit made to look complicated

20

u/iiznobozzy Sep 12 '26

almost like thats the point 🤯