Answer the question
In order to leave comments, you need to log in
Why is char 1 byte and character literal ('A') 4?
sizeof(char) = 1 byte
sizeof('A') = 4 bytes.
I realized that what we call characters is actually a numeric code, and therefore character literals are allocated the same amount of memory as the int type (4 bytes).
But I didn’t quite understand how a four-byte character fits into a single-byte char?
And when I declare char test = 'A'; then how much memory was allocated in the computer: 1 byte or 4?
(If you try sizeof(test), it will still be 1. But isn't 'A' 4 bytes?)
Answer the question
In order to leave comments, you need to log in
And now I will tell you the correct answer.
In C, a character literal is of type int and therefore its sizeof is 4 bytes.
In C++, its type is char and 1 byte. Therefore, those who created the CPP file did not see the problem. Obviously, it has to do with function overloading: somehow you don't want the version for int to be called in foo('A').
#include <stdio.h>
int main()
{
int sz = sizeof('A'); // латинское
printf("sz = %d\n", sz);
return 0;
}
char test='A'
char test=L'Й'
I realized that what we call symbols is actually a numerical code
and therefore character literals are allocated the same amount of memory as the int type (4 bytes).
But I didn’t quite understand how a four-byte character fits into a single-byte char?
And when I declare char test = 'A'; then how much memory was allocated in the computer: 1 byte or 4?
And there is nothing to stuff Russian letters into char
In (1), if c-char is not a numeric character sequence and is not representable as a single byte in the execution character set, the character literal is conditionally supported, has type int and implementation-defined value .
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question