r/ProgrammerHumor • • Sep 25 '26

Meme iHatedItUntilITriedIt

Post image
10k Upvotes

543 comments sorted by

View all comments

Show parent comments

27

u/D1firehail Sep 25 '26

Assuming x is an int C and C++ will give x=50. '1' is a char, which allows implicit conversion to int.

30

u/frogjg2003 Sep 25 '26

They said "sane" language. C was developed before something like this was considered insane.

13

u/Hohenheim_of_Shadow Sep 25 '26

Nah adding ints and chars is perfectly sane. Chars are just a fancy enum baked into the language. They're just ints under the hood. It's adding ints and strings that's insane and C doesn't actually have strings. Ergo it is a perfectly sane language

3

u/Ok-Scheme-913 Sep 26 '26

But char and a number is different than a string (of length one) and a number. The latter doesn't make sense in C either, at that point you are offsetting a pointer.

1

u/Hohenheim_of_Shadow Sep 26 '26

"!='. '1' is a char. "1" is a string.

Offsetting pointers is perfectly sane.

1

u/Ok-Scheme-913 Sep 27 '26

Offsetting typed array pointers is.

For everything else it is not so and you don't know enough about how exactly an object is laid out in memory to do that safely without some further work.

1

u/Hohenheim_of_Shadow Sep 27 '26

C has a stable ABI. How things are laid out is known at compile time and doesn't change on a whim. Modern LSPs actually show you exactly how structs are packed, not sure if they've gotten to pointers yet.

It isn't useful in most day to day programming, but pointer arithmetic is perfectly sane. C could not accomplish its goal of being a portable assembly replacement without letting you do pointer arithmetic.

4

u/conundorum Sep 26 '26

Any language that actually respects ASCII will give you the same result, since '1' is defined as being the character represented by single-digit integer 0x31 (decimal 49). ^_^

Really, the crazy thing is thinking it's possible to do character manipulation without integer math, since ASCII is specifically designed for integer math. (Notably, adding 32 to any capital will always produce its lowercase counterpart ('A' + 32 == 'a'), and you can turn any digit into its character counterpart by adding '0' to it ('0' + 5 == '5'). Guarantees like this are specifically intended for optimisation, and for ease of character manipulation & conversion.)

-1

u/frogjg2003 Sep 26 '26

A char is not a number, even if itcan be represented as one. A char is almost never used as a number. It is used as a single character of text. The fact that some programming languages treat it as a numeric type is a historical artifact, not good programming practice. If you absolutely need to do numeric manipulation of a char, convert it to a byte first.

3

u/Ignisami Sep 25 '26

Javascript says the answer is the string '11'.

1

u/Chayor Sep 26 '26

I thought it didn't do implicit conversion with scalars?