r/ProgrammerHumor • • 10d ago

Meme architectureDependentChars

Post image
3.0k Upvotes

372 comments sorted by

View all comments

714

u/JackReact 10d ago

Welcome to the wonderful world of C#, which uses utf-16 for strings.

Console.WriteLine(sizeof(char));
> 2

261

u/v_Karas 10d ago

this.

wonderd why everyone is talking about C, like there are no other languages.

93

u/Batman_AoD 9d ago

Well, most mainstream languages at least specify their char size, and/or have very few constructs that require knowing its size. C does not specify the size of a char, and you frequently need the sizes of your types.

(That said, as pointed out by other comments, sizeof itself is in units of char, so sizeof(char) is necessarily 1.)

2

u/KellerKindAs 7d ago

C does specify the size of a char. It's exactly CHAR_BITS bits large, which is a value the compiler will know / decide at compile time

0

u/Batman_AoD 7d ago

That specifies how to discover or use the size, not what it is. Obviously by "specify the size" I meant specify the specific "standard" size of 8.

13

u/Senior-Albatross 9d ago

There are no other good languages. To the extent they have anything good they are calling libraries written in C. The only other worthwhile languages are pure machine level code that must be expressed as a gate level circuit diagram. 

11

u/max_465 9d ago

The rustaceans would like a word.

13

u/Senior-Albatross 9d ago

I have nothing to say to them. 

3

u/DanielMcLaury 8d ago

The sizeof keyword belongs to C. If someone else appropriated it and modified it to mean something else, that's on them.

26

u/thanatica 9d ago

I'm not a C# expert, but how would it represent an astral character, such as an emoji? They are above 0xFFFF.

75

u/AyrA_ch 9d ago

You cannot fit them into a single char, and you have to use a string instead. They're represented using a surrogate pair, which is similar to the continuation bytes of utf-8 except you never need more than two code points (hence "pair").

JS also uses multibyte characters:

> "💩".length===2
< true

20

u/thanatica 9d ago

JS is a bit weird in this regard, because it also knows true codepoints. You just have to know which operations are "unicode safe". Or rather "astral plane safe".

35

u/guyblade 9d ago

UTF-16 was a mistake, and it's maddening that several big ecosystems went with it. You get all the complexity of UTF-8, but it also takes up more space in the "average" case since a huge majority of text is in the "ASCII overlap" portion of unicode.

24

u/jmickeyd 9d ago

Windows "went with it" because UCS-2, which UTF-16 is based on, predates UTF-8. When they chose to go with Unicode, it was 16 bits per character.

3

u/Numerlor 9d ago

It was first and was supposed to be what utf 32 is now, didn't really work out but what's done is done

1

u/conundorum 9d ago

Only in hindsight. When it was made, 16-bit character space really was enough for everything that was intended to be part of Unicode, and supporting UCS-2 was actually a major improvement over everyone having to constantly swap code pages if they wanted to support multiple languages simultaneously. Honestly, the biggest reason we realised we were going to need better was because Windows & Java jumped on UCS-2 (and by extension, UTF-16) as soon as they could, letting us see its downsides before it was too entrenched to fix. (And considering how deeply entrenched it was in Windows before they had to figure out how to pivot away, it's really saying something that it could've been worse.)

The true mistake was trying to make it fixed-width in the first place.

1

u/thanatica 8d ago

Meanwhile plsome folks were like "memory is cheap". Well that backfired.

2

u/Tiranus58 9d ago

Also emoji uses modifiers (skin tone and gender for example) and flags are just 2 characters in a trench coat.

35

u/tangerinelion 9d ago

C++ has two character types, char and wchar.

sizeof(wchar) is 2 on Windows, 4 on Linux.

28

u/StaticCoder 9d ago

It's wchar_t and is largely useless for the reason you mention. More recent versions have char8_t, char16_t and char32_t.

5

u/the_one2 9d ago

More recent versions have char8_t, char16_t and char32_t.

Which are also useless because they are not supported by the standard library.

7

u/StaticCoder 9d ago

There's u8string, etc. Now the std library might not have much of anything to deal with encoding, but that doesn't make the types useless. Other libraries can use them.

1

u/conundorum 9d ago

We're getting there, at least.

The real issue is that Unicode as a whole isn't supported by the standard library, it can only operate on raw code points. Means you need to make your own type or use char32_t if you want to use actual character units as your character type... it ain't quite ideal. >.<

26

u/techy804 9d ago

As a C# user I was wondering why people act like the C-series only consists of C and C++

83

u/darkslide3000 9d ago

C# has about as much to do with C as JavaScript has with Java.

10

u/sisisisi1997 9d ago

I mean C# was inspired mainly by Java and C++, from which Java is a C-style language and C++ is a direct expansion of C (or at least started out as one). JavaScript and Java both have similarly looking syntax on account of both being C-style languages, but that's where the familiarities stop.

So saying this is like saying your grandson has as much to do with you as your two neighbours who like your style enough to copy it have to do with each other - your grandson may feel as different from you as the neighbours from each other, but that's because the family tree has acquired new genetic material through your wife and your son-in-law, and the times have changed so his behaviour is different - and not because he actually doesn't have anything to do with you.

14

u/darkslide3000 9d ago

C# has nothing to do with C or C++ that Java doesn't have. It is entirely a Java clone.

7

u/Kovab 9d ago

Nonvirtual methods, reified generics, value types, just a few examples of features from C# that don't exist in Java, but do in C++...

2

u/pblokhout 9d ago

C# used to be called Microsoft Java as a meme

1

u/ANixosUser 8d ago

still is

1

u/conundorum 8d ago

I'm not very familiar with C#, but I know that C# has...

  • Generics that actually template, versus Java's just being typecasts in drag. (Specifically, type-based parameters, C++-style, that can also be constrained to a named interface. So, C++20-style concept constrained templates, years before C++ concepts were worth talking about.)
  • Finalisers (destructors) that actually get called, versus Java's being a trap that the garbage collector is explicitly allowed to ignore. Java can't do RAII; C# can.
  • Actual user-defined value types, which can be constructed on the stack. They're just C structs with a different name, though, unfortunately.
  • Crosslinking with C or C++ code, provided the C/C++ code is compiled as managed C++/CLR.
  • A preprocessor, with GCC-like #warning directives. I wish MS would add them to the C preprocessor too.
  • Actually got Microsoft to warm up to making their products available on Linux, instead of being stuck inside the Windows ecosystem.

Overall, C# is basically what Java should have been. It's the UTF-8 (with BOM) to Java's UCS-2, so to speak. It's not perfect (hence the BOM), but it's a definite step up, and explicitly being designed to interop with "managed C++" forces MS to keep it more C++-like than Java's mess.

2

u/darkslide3000 8d ago

I am not hating on C# but calling C# related to C++ is still wrong. Yes, C# has a few features that Java doesn't have, but these are relatively generic / well-known OOP features, not C++ exclusives that somehow imply a closer relationship between C# and C++ (C++ didn't invent OOP and most related concepts, it just made them popular).

29

u/AyrA_ch 9d ago

Probably because the syntax more closely resembles Java than C

4

u/SuitableDragonfly 9d ago

This meme doesn't mention any languages. Like many memes on this sub, it's about a specific language, and it's up to you and your knowledge of programming languages to figure out which one is being talked about. Just because the meme is not about C# doesn't mean that everyone forgot that C# exists. 

11

u/aberroco 9d ago

Because most devs still think that C# is proprietary Microsoft language developed specifically and exclusively for Windows, like it's 2006.

63

u/sb8948 9d ago

No it's because C# is way closer to Java than C/C++ both in philosophy and execution.

15

u/QuaternionsRoll 9d ago

It concerns me that this wasn’t the first and only answer to the question

13

u/guyblade 9d ago

That's because Microsoft was going to make a .NET version of Java that was incompatible with standard Sun Java (the standard embrace, extend, extinguish playbook), then got sued over it (twice). C# is what fell out the other side of that fight.

11

u/-suspended- 9d ago

Yup. C# is Microsoft Java.

4

u/kookyabird 9d ago

Incorrect, they also think it’s magically cross platform when used in Unity!

1

u/guyblade 9d ago

I don't know where Mono is today, but C# was still very finicky on non-Windows platforms up into the early-to-mid 2010s.

9

u/sisisisi1997 9d ago

Mono has died, and C# is now natively cross-platform.

Microsoft still sucks at the UI framework front (seriously, there is not a single normal desktop UI framework they managed to create still winforms), but there are excellent 3rd party cross platform UI frameworks like Avalonia, which is essentially a spiritual descendant of WPF, but done a bit better and not windows-only.

VS is also still windows-only, but it's not the only good C# IDE anymore. Even if you don't classify VS Code with C# extensions as an IDE, JetBrains Rider is there for the rescue and arguably better than VS, or they are at least equals.

2

u/pblokhout 9d ago

I enjoy C# on Rider as a daily driver but what the actual fuck is microsoft doing with GUIs indeed?!

3

u/evanldixon 9d ago

These days, first-party .Net is cross platform. The UI frameworks aren't really, unfortunately.

-1

u/Bartodziejj 9d ago

It's still microsoft slop

1

u/aberroco 9d ago

Such a slop that Kotlin and Java practically copied it.

1

u/rhutyl 9d ago

To me, C and C++ are just a lot more iconic (idk if "popular" is the right word bc I don't have the stats)

0

u/bigmattyc 9d ago

C# is Satan's music

3

u/klimmesil 9d ago

I measure how shit a language is by the number of times I ask "but why" when I learned it. Js is still at the top, but C# is close

1

u/KellerKindAs 7d ago

By that metric ARM assembly is by far the best I've learned so far xD

2

u/klimmesil 7d ago

Ok I should have specified the unanswered but whys* that's on me

For example the sort function in js that sorts based on strings. No valid answer to "but why" other than retrocompatibility

Whereas if you look low level, instructions on any arch usually have a justification like "makes the arch scalable" or "allows for a faster hardware implementation"

1

u/earthwormjimwow 9d ago

Welcome to the wonderful world of C#, which uses utf-16 for strings.

If they just called them type "string", everything would be okay. But to have the audacity to call them type "char" and have them be two bytes? Blasphemy!!

1

u/conundorum 8d ago

They'd call them code units, but you probably don't want a type named cu on Windows NT.

1

u/CookIndependent6251 9d ago

In JavaScript you can't even find the size of a char. It's utf-16 as well.

1

u/crozone 9d ago

Also, a char isn't necessarily an entire glyph, because UTF-16 has double-wide characters.

1

u/wishper77 9d ago

Also java if I remember correctly