Look at the code first, as follows:
1 intMain ()2 {3 Char*P1 =1;4 Char*P2 ='1';5 Char*P3 ="1";6 7printf"%s,%s,%s", P1,P2,P3);8printf'\ n');9printf"\ n");Ten One return 0; A}
The above code first defines three pointer variables that point to the char type, and assigns the initial values to the three pointer variables. The author's intention is to use the printf function to print the above three 1, but unfortunately, the author does not want to achieve the desired effect. So where is the problem?
First we look at the 3rd line of code, char *p1 = 1; what does that mean? This code can be split into the following two code: Char *p1; P1 = 0x0000 0001, which means that the memory address 0x0000 0001 is assigned to the pointer variable P1 (here we do not consider forcing the type conversion, but in principle it is required), and then using printf in the later code to add the address 0x0000 The contents of the 0001 unit are printed out. It is usually reserved for use by the operating system at low memory addresses. It is possible to make an error by accessing it rashly. Obviously, this is not the effect the author wants to achieve.
Then we look at the 4th line of code, char *p2 = ' 1 '; what does that mean? The same code can be split into two code, char *p2;p2 = 0x0000 0031; why 0x0000 0031? In the fourth line of code is a single character ' 1 ' is assigned to the pointer variable P2, and the value of a single character is a 8-bit integer, in ASCII code the number 1 of the code is 0x31,. So the fourth line of code above means that the memory address 0x0000 0031 is assigned to the pointer variable P2, as mentioned above, this does not achieve the effect the author wants.
The 5th line of code, char *p3 = "1"; Obviously this is a familiar usage, and this code can also be split into two: Char *p3; P3 = "1";P 3 is a pointer variable pointing to the char type, and "1" is a string, and before we say it more than once, the value of the string is an address that points to the string. So if this code is used alone in printf, it must be in advance of the author's purpose.
Next we look at the 8th line of code, printf (' \ n '), is this line of code correct? I'm going to compile a bit more vs next, compile with warning, run with error. Here, let's take a look at the prototype of the printf function:
_crtimp __checkreturn_opt int __cdecl printf (__in_z __format_string const char * _format, ...);
About the modifiers of the input and output we're not going to look at it, we'll focus on one thing, the type of the first parameter, the variable of a const char *, the following ... Represents a variable parameter. Similar to the params in C #, when we execute printf (' \ n '), the compiler first converts the value of ' \ n ' to a const char* pointer variable, and the value of ' \ n ' is 0x0a, and the system accesses the content at address 0x0000 000a. , but it is clear that the contents of the address are not what we want.
Ok~ into the chase!
1. About the difference between single and double quotation marks
Single quotation marks are used in the C language to denote a single character constant, which everyone is familiar with.
For example: ' A ' indicates a character constant ' a ', in memory of a byte, ' a ' +1, indicating the ' a ' ASCII code plus one, get ' B ';
Double quotation marks are used in the C language to represent string constants.
For example: "A" represents a string constant, which accounts for two bytes in memory, because there is finally a ' plus ', "a" +1 for pointer operations, and the result points to the string "a" of Terminator ' "";
2, some summary
2.1 A character that is essentially enclosed in single quotation marks represents an integer. The integer is the ASCII code of the character.
2.2 A string that is enclosed in double quotation marks represents a pointer constant. The pointer points to the string.
2015-10-26 single and double quotation mark course notes