Post APj1ryL0nU04TRPCAS by Elizafox@mystic.lgbt
(DIR) More posts by Elizafox@mystic.lgbt
(DIR) Post #APhq7j4Pw7St9CQf3I by Elizafox@mystic.lgbt
2022-11-17T20:32:02Z
0 likes, 0 repeats
My favourite thing about C/C++:Given foo(int x)Did you know all of the following will decay into an int without a cast?foo('a');foo(42);foo(3.5);foo(true);enum { a, b, c }; foo(a);
(DIR) Post #APhq7jcRtageqkLq64 by ukko@p.enes.lv
2022-11-17T20:41:54.321170Z
0 likes, 0 repeats
@Elizafox 'a' will actually not decay into an int in C, it IS an int. Also not 100% certain about what enums do in C, they are very sus there
(DIR) Post #APhq7jwegSqlrPnyam by Elizafox@mystic.lgbt
2022-11-17T20:35:16Z
0 likes, 0 repeats
C++'s type promotion and decay rules are also such thatfoo(double x)Will accept both floats and doubles. Floats will be promoted to double automatically and silently. This isn't usually a problem, but I've had cases where I've had to be aware of which floating-point type is in use.
(DIR) Post #APhqJcRGPyeW3TmoU4 by Elizafox@mystic.lgbt
2022-11-17T20:43:17Z
1 likes, 0 repeats
@ukko It won't decay correct. This is also why isalnum and family take ints. I felt the need to oversimplify because C/C++ is cursed enough as it is.enums in C just define a bunch of names in global scope assigned values 0, 1, 2, ... unless explicitly defined.
(DIR) Post #APj1q8oqrC8GOyUPqq by JensHannemann@mastodon.online
2022-11-17T22:45:07Z
1 likes, 0 repeats
@ukko @Elizafox ‘a’ is not an int, it’s a char. And the standard says it can be either signed or unsigned. sizeof(char)==1, always. sizeof(int)>=sizeof(char) , but typically at the very least sizeof(int)>=2.
(DIR) Post #APj1q9MArIms4K51n6 by ukko@p.enes.lv
2022-11-18T10:27:50.065494Z
0 likes, 0 repeats
@JensHannemann @Elizafox 6.4.4.4 of C99 final draft, Semantics, “An integer character constant has type int. …”Same in current C2x draft.In C++ it depends on whether the literal is encodable, multichar, or ordinary, but 'a' would be char, yes.
(DIR) Post #APj1ryL0nU04TRPCAS by Elizafox@mystic.lgbt
2022-11-17T23:06:11Z
1 likes, 0 repeats
@JensHannemann @ukko I had forgotten all about that stuff. It hurts my head.
(DIR) Post #APj1sVQTmAyv5xDWls by JensHannemann@mastodon.online
2022-11-18T00:22:29Z
1 likes, 0 repeats
@Elizafox @ukko If you ever struggle with the more obscure parts of C or C++, this is a good starting point:https://isocpp.org/faq
(DIR) Post #APjRjTedaiXZnoC9nU by JensHannemann@mastodon.online
2022-11-18T15:00:04Z
1 likes, 0 repeats
@ukko @Elizafox That's very interesting. I was not aware that C and C++ differ on something this fundamental. So what happens when in C you write:char c = 'a';Is this an implicit cast? If it is a truncation, will the result be implementation defined, as it may yield a different result on little- or big-endian platforms?
(DIR) Post #APjTWjbz9R5dR80RLU by ukko@p.enes.lv
2022-11-18T15:38:04.041646Z
0 likes, 0 repeats
@JensHannemann @Elizafox Yes, that would be a cast, but I don’t think it would be implementation defined, because single-character[1] constants will have the same value as the character (basically, the compiler does a char->int cast). So ‘a’ will be (int)97. Buut ‘\xFF’ will be (int)255 if char is unsigned and (int)-1 if char is signed :blobgrin:. I, personally, don’t see how a truncation back to char could end up being implementation defined (unless you do something like ‘a’+1).[1] there are also multi-character (not to be confused with multi-byte characters) constants such as ‘abcd’, and those are completely implementation-defined. Usually how they’re implemented is something like “reinterpret the memory containing these characters as an int” and that does become different for different endianness (actually it’s how some old code detects endianness, #define LITTLE (('abcd'&0xFF)=='a'))Multi-byte characters (eg, L’a’) are a bit better: their type is wchar_t. Unicode characters (u8’a’, u’a’, U’a’) are the most sane: they don’t allow multi-character constants at all and have proper char8_t, char16_t, char32_t types (tho it’s arguable whether it’s a good thing to add even more char types).
(DIR) Post #APjUER5lMtY8jxlyMa by ukko@p.enes.lv
2022-11-18T15:45:56.780977Z
0 likes, 0 repeats
@JensHannemann @Elizafox Also, an integer type truncation should get the low-order bytes no matter of the memory representation, so (int8_t)0x12345678 will always be 0x78 (unless in very unusual cases like error: int8_t not defined or warning: 0x12345678 doesn’t fit in an int)