4 ms·
Switching to C++ gets you lots of things that aren’t “C-like” (that, of course, is a vague term that, as this thread shows, people will disagree about, but I th
by Someone 4y ago
Switching to C++ gets you lots of things that aren’t “C-like” (that, of course, is a vague term that, as this thread shows, people will disagree about, but I think there’s consensus that C++ has many features that aren’t C-like), and may get you subtle bugs because of small incompatibilities between C and C++. For example, sizeof('x') is 1 in C++, but >1 in C because 'x' is a char in C++ and an int in C.
https://en.wikipedia.org/wiki/Compatibility_of_C_and_C%2B%2B https://en.wikipedia.org/wiki/Compatibility_of_C_and_C%2B%2B
- nequo 4y agoWhy is sizeof('x') equal to 4 if char letter = 'x'; sizeof(letter) is equal to 1, just like `sizeof(char)`? If `'x'` is represented as an `int` in C, shouldn't `letter` in this example also be represented as an `int`?
- eMSF 4y agoThe type of the expression 'x' is int, not char (in C). The type of an expression consisting of a variable name is the type of the variable (as far as sizeof is concerned).
- tialaramex 4y agoNo. The type of 'x' is int. It so happens on your platform (and most available systems today) sizeof(int) == 4. The type of letter was explicitly char, and sizeof(char) == 1 by definition in C. char letter = 'x'; is a type coercion. That literal is an integer, with the value 120 and then it's coerced to fit in the char type, which is an 8-bit integer of some sort (might be signed, might not, doesn't matter in this case).
- nequo 4y agoDoes this mean that `word` in char *word = "xyz"; is a pointer to an array of four `int`s, `'x'`, `'y'`, `'z'`, and `'\0'`? When I evaluate sizeof(*word) I do get 1 instead of 4, even though `*word` is pointing to `'x'`. Where are the remaining 3 bytes in memory?
- lucozade 4y agoIf you specifically type it as char * the it's a pointer to chars each of which has size 1.
- seba_dos1 4y agoNot at all. There are no character literals in "xyz", this is a string literal and it's unrelated to what your parent was saying.
- jcranmer 4y agoA char is 1 byte by definition. But the type of a character literal (the 'x' syntax) is not a char, but an int instead. The C type system generally matters so little that the type of an expression has little relevance (sizeof is the most notable exception to that rule), which obscures this fact.
- kzrdude 4y agoyou'll have to understand the 'x' syntax and the "xyz" syntax as two different things. Different quotes.
- nequo 4y agoI know. But my understanding was that `"xyz"` is an array of characters so that these two would have the same representation in memory: char word[] = {'x', 'y', 'z', '\0'}; // sizeof(word) = 4, sizeof(*word) = 1 char word[] = "xyz"; // sizeof(word) = 4, sizeof(*word) = 1 What I did not realize was that the above two are not the same as this: char *word = "xyz"; // sizeof(word) = 8, sizeof(*word) = 1
- kzrdude 4y agoWhat I see there is that you seem to highlight the difference between using sizeof with an array and sizeof with a pointer, which makes a difference, even if array-decays-to-pointer is a rule in most other contexts.
- 4y ago
- gallier2 4y agoPeople often forget that 'ab' or '1ert' multi-char immediate are allowed in C. They are almost unusable as they are highly un-portable (because of endianess issues between the front-end and the back-end).
- tialaramex 4y agoThis is once in a while kinda useful, aside from the data layout issue for stuff like a FourCC. Rust has e.g. u32.to_le_bytes() to turn an integer into some (little endian) bytes, but I don't know if there's a trivial way to write the opposite and turn b"1ert" (which is an array of four bytes) into a native integer. Edited to Add: oh yeah, it has u32.from_le_bytes(b"1ert"). I should have checked.
- _kst_ 4y agoIn the declaration char letter = 'x'; the initialization expression 'x', which for historical reasons is of type int in C, is implicitly converted to the type of the object. `letter` is a `char` because you defined it that way. If you had written int letter = 'x'; that would be perfectly valid, and the conversion would be trivial (int to int). It's just like: double x = 42; `sizeof 42` might be 4 (sizeof (int)), but `sizeof x` will be the same as `sizeof (double)` (perhaps 8).