Today I read in "C Traps and Pitfalls" about""and''the description,''A single-quoted character represents an integer, and the integer value corresponds to the sequence value of that character in the character set used by the compiler.
""A double-quoted string represents a pointer to the initial character of an unnamed array. I became interested in these two symbols, so I opened VS2010 to view the storage forms of single-quoted and double-quoted characters in memory.
The experimental method is very simple: separately view"yes"/'yes'/'ye'/'y'the storage forms of four types of characters in memory, the code is as follows:int _tmain(int argc, _TCHAR* argv[])
{
char test1[] = "yes";
int test2 = 'yes';
printf("%x\n", &test2);
int test3 = 'ye';
printf("%x\n", &test3);
int test4 = 'y';
printf("%x\n", &test4);
return 0;
}
The experimental results are as follows:

According to the storage forms in memory, it was found that''The single-quoted character, in the storage form of an integer, treats the first character as the highest-order byte and the last character as the lowest-order byte, and stores them in this order into the 4-byte storage space of an int variable;
""The double-quoted string stores the first character at the starting address and the last character at the ending address.
The experimental results confirmed the description in "C Traps and Pitfalls". Then when''the single-quoted character exceeds44 bytes, what would happen? Another experiment was conducted, and the code is as follows:
int _tmain(int argc, _TCHAR* argv[])
{
char test1[] = "yes";
int test2 = 'yes';
printf("%x\n", &test2);
int test3 = 'ye';
printf("%x\n", &test3);
int test4 = 'y';
printf("%x\n", &test4);
int test5 = 'yesa';
printf("%x\n", &test5);
return 0;
}
The experimental results are as follows:

From the experimental results, it can be seen that when the character exceeds 4 bytes, it exceeds the storage capacity of the int type, and the compiler reports an error, indicating that the constant has too many characters. This can effectively remind programmer friends that there may be symbol input errors. However, when it does not exceed the int storage capacity, it is accepted as is without any warning, posing a hidden danger of symbol misuse, and debugging will be quite troublesome if problems occur in the program later.
Original URL: https://blog.csdn.net/u013541620/article/details/43172891