The difference between single and double quotes in C language
Category Programming Techniques
Today, I read the description of ""
and ''
in "C Traps and Pitfalls". A character caused by ''
represents an integer, and the integer value corresponds to the sequence value of the character in the character set used by the compiler.
A string caused by ""
represents a pointer to the starting character of a nameless array. I became interested in these two symbols, so I opened VS2010 to see the storage form of characters in memory caused by single and double quotes.
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 form in memory, it is found that the string caused by ''
is stored in the form of an integer, identifying the first character as the most significant bit and the last character as the least significant bit, and storing it in the 4-byte storage space of the int variable in this order;
The variable caused by ""
stores the first character at the starting address and the last character at the ending address.
The experimental results confirm the description in "C Traps and Pitfalls". So what will happen when the character caused by ''
exceeds 4 bytes? Let's conduct another experiment with the following code:
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:
According to 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 there are too many constant characters. This can effectively remind programmer friends that there may be a symbol input error, but when it does not exceed the storage capacity of the int type, it accepts it without any prompts, and there is a hidden danger of symbol misuse. Debugging will be quite troublesome if problems occur in the future.
>
Original article link: https://blog.csdn.net/u013541620/article/details/43172891
** Click to share notes
-
-
-