C language (forced type conversion) and 'nominal character ', nominal character
C LanguageExplicit/ImplicitType conversion, there isIntermediate variableThe original data type and content remain unchanged.
The following code usesGCCCompile.
1 # include <stdio. h> 2 3 int main () 4 {5 printf ("% d \ n", 'A', sizeof (int) 'A ')); // The output is. the target object of sizeof is an int-type intermediate variable 6 7 char c = 255; 8 printf ("% d \ n", c); // output-1. char-type c has exceeded. After implicit conversion to int, it is also maintained as-1 9 10 return 0; 11}
I thought that the single quotes were too many characters. For example, '123' would report an error. After a test, I found that only the warning was reported (GCC, the warning information provided in the Code). It can be compiled through:
1 # include <stdio. h> 2 3 int main () 4 {5 printf ("% d \ n", '1'); // output 49 6 printf ("% d \ n ", '12'); // output 12594 warning: multi-character constant 7 printf ("% d \ n", '123'); // output 123 warning: same as 8 printf ("% d \ n", '000000'); // output 1234 warning: Same as 9 printf ("% d \ n", '000000 '); // output 842216501 warning: character constant too long for its type10 printf ("% d \ n", '000000'); // output 123456 warning: same as 11 printf ("% d \ n", '000000'); // output 1234567 warning: Same as 12 printf ("% d \ n", '000000 '); // output 892745528 warning: Same as 13 printf ("% d \ n", '000000'); // output 123456789 warning: Same as 14 15 return 0; 16}
Output 1 ~ The literal value of 9 digits. The output type is int type. There is an implicit type conversion process in the middle.
The first line outputs '1' ASCII code value 49;
In the second row, the ASCII values of '1' and '2' are respectively assigned to the lower two bytes of the int-type intermediate variable, that isZero x 3132Therefore, the output is 12594 = (49 <8) + 50. The warning message is displayed as a multi-character literal value;
The results of the fourth line are the same as those of the previous line;
The principle is the same as above, but only4 lowerFor example, if the number of rows is 9th, only '123' is used, that isZero x 36373839The warning message indicates that the literal value of a character exceeds the number of bytes of the int type.
Forced type conversion in C Language
You understand it correctly. The answer to both questions is: yes.
The START val is defined as int, that is, the val address (that is, & val) is the integer number. However, once the address & val is retrieved, You can redefine the content of this address (char *) to tell the compiler that the pointer content of this address should be read by the char pointer.
This can be regarded as forced type conversion, but not standard. The standard forced type conversion is: (char) val or char (val ).
In a 32-bit Windows operating system, the int count occupies 4 bytes (byte) and is automatically converted to char at the lowest byte (8 bits ), this is done automatically, so you don't have to worry about it. However, because pointer is defined as a pointer to char, when it is used (pointer ++), it only adds one byte (8 bits ).
Forced type conversion in C Language
Good question!
In the C language, this type of conversion is the data closest to (double) to the right. The operation level is lower than parentheses, but higher than multiplication, addition, subtraction, and value assignment; the key to k/m is the highest level of accuracy for Binary calculation. Among the two variables of binary calculation, the lower accuracy will be automatically converted to a higher accuracy;
# Include <stdio. h>
Main (){
Int m, n, num;
Char t = '0', th;
Double dou_1, dou_2, dou_3;
M = 5; n = 321;
Num = t/(float) m/n );
Dou_1 = (double) (n/m );
Dou_2 = n/m;
Dou_3 = (double) n/m;
Th = (double) n/m;
Printf ("% d, % f, % c", num, dou_1, dou_2, dou_3, th );
Getch ();
}
Try this one more and have a deep understanding.