1

I have code:

static uint64_t password = 'password';

GCC say:

warning: character constant too long for its type static uint64_t password = 'password';

If I do so:

static uint64_t password = 'pass';

GCC say:

warning: multi-character character constant [-Wmultichar] static uint64_t password = 'pass';

That is, it turns out that sizeof(char) = 2, but is it not so, or is it not?

Penguin
  • 101
  • 7
  • 2
    "_That is, it turns out that sizeof(char) = 2_": It is not saying that anywhere. How would you get to this conclusion? – user17732522 May 09 '23 at 06:47
  • 1
    `sizeof(char)` is *always* one by the *standard*! What *can* differ is the number of bits in a `char`, which you can get by `CHAR_BIT` (from `limits.h`). I've met systems having 16 for that... – Aconcagua May 09 '23 at 06:55
  • [This answer](https://stackoverflow.com/a/7755280/6333444) suggests this should work – mousetail May 09 '23 at 07:34
  • @mousetail No it doesn't, the OP isn't using escape sequences. – Lundin May 09 '23 at 09:41
  • @Lundin The linked answer suggests `'xy'` as a valid string for a short type and also says a 8 character string should be valid for a 64 bit integer. It also says the exact format is implementation dependent though so it might not work in every implementation. – mousetail May 09 '23 at 09:52
  • @mousetail On common systems a character literal is _not_ a 64bit integer. So that isn't really relevant. The Wikipedia article that the answer refers to is not well-written. In any case, the value mapping is completely implementation-defined and that there is a correlation with the number of characters is purely because there is a common choice for this implementation-defined behavior. It doesn't need to be this way (and will e.g. typically break down for non-ASCII characters). – user17732522 May 09 '23 at 11:04
  • Obviously it wouldn't work for non-ASCII, as they can't be stored in 1 byte – mousetail May 09 '23 at 11:24
  • Note that the type of character constant in `int`, not `char` ! – tstanisl May 09 '23 at 16:01

2 Answers2

4

sizeof( char ) is guaranteed to be 1, by definition.

C17§6.5.3.4¶4 When sizeof is applied to an operand that has type char, unsigned char, or signed char, (or a qualified version thereof) the result is 1. [...]


'...' is an int.

C17§6.4.4.4¶10 An integer character constant has type int. [...]

You apparently have a system where sizeof( int ) == 4, which is why you could specify 4, but not 8 characters.


Note that it's really weird to use a multi-character constant.

And it's not portable.

C17§6.4.4.4¶10 [...] The value of an integer character constant containing more than one character (e.g., 'ab' ), or containing a character or escape sequence that does not map to a single-byte execution character, is implementation-defined. [...]

ikegami
  • 367,544
  • 15
  • 269
  • 518
  • Re “I have no idea how you came to the erroneous conclusion that it's `2`.”: Their (incorrect) reasoning was likely “A four-character constant fits in an eight-byte integer, but an eight-character constant does not, therefore the size of a character is greater than 8/8 but less than or equal to 8/4, and it must be an integer, so it is 2.” – Eric Postpischil May 09 '23 at 11:56
1

In c you MUST use the correct character to define a string. if you use '' like you have the compiler thinks that you are trying to define a character. For example char example = 'E';.

To define a string you need to use "". So for example char* name = "Penguin"; or char name[] = "Penguin";

Here are the differences between char * and char ...[] Link

In addition, uint64_t broken down means unsigned int 64bits type and has nearly nothing to do with characters and strings. Finally I'd suggest you watch a few tutorials before trying any more c.
Here is an ok tutorial for c I found.

Lochyj
  • 63
  • 7
  • 5
    Hmm: "watch a few tutorials", I'd rather say "Start reading your C text book". – Jabberwocky May 09 '23 at 07:12
  • 3
    They're not trying to create a string, but an `uint64_t`. The problem is that `'pass'` creates an `int` (in an implementation-defined way), which is smaller than `uint64_t` in the OP's env. – ikegami May 09 '23 at 07:14
  • it will most likely get converted into a character sort of like how \n does and converted into a 32 bit integer. I don't really know exactly but that is what i imagine will happen. Additionally, there are much better ways to do that. Please correct me if im wrong – Lochyj May 09 '23 at 07:15
  • OP didn't explain their problem and what they are trying to do so all we can do is guess... – Lochyj May 09 '23 at 07:18
  • 2
    Yeah, except you said it yourself: uint64_t has nothing to do with strings. So it's pretty far out to assume they want a string. – ikegami May 09 '23 at 07:20
  • 1
    Thanks for using my nickname, but I didn't ask about strings, and I know the difference between char * and char []. I asked about a constant from symbols. The person who answered correctly said that any sequence of characters in '...' is an int. – Penguin May 09 '23 at 09:02