Home > Back-end >  Char variable input a value greater than 127, and returns the result of the problem
Char variable input a value greater than 127, and returns the result of the problem

Time:05-01

For example, the code:
 # include 
Void main ()
{
Char c1 and c2.
C1=197;
C2=198;
Printf (" c1=% c, c2=% c \ n ", c1, c2);
Printf (" c1=% d, c2=% d ", c1, c2);
return;
}


Returns the [
 c1=? C2=? 
C1=59, c2=- 58


 a return value of % d want to ask how to calculate, the result of observation is 
Such as c1 turn 2 base: 11000101 and the first keep other take complement 1 0111010
The results calculated are the result of terminal return,
Want to know is that right? Why so calculate?

CodePudding user response:

"Baidu" negative, complement
% c question marks because can't find the corresponding ASCII characters

CodePudding user response:

reference 1st floor NorZ response:
baidu "negative, complement
"% c question marks because can't find the corresponding ASCII characters

I know the % c is can't find the character is not very understand the principle of his % d output

CodePudding user response:

This is a simple and complex problems,

First, % and % d c require corresponding change is type int, the only difference is that % c is to convert the type int variable parameter for unsigned char type of characters, and then display, and % d is to convert it into a signed decimal format of digital characters display,

Second, there is no question of procedure, although the incoming change is the char type, but in accordance with the default argument conversion (the default argument conversion) rules, after it is converted to type int is passed to the printf,

Finally, for the printf, % c is to pass along the the value of the int type (length), and then transform (narrow) for unsigned char type (length), and regard it as a character encoding, then print the characters; % d is to pass along the the value of the int type (length), split and converted into digital signed decimal number character printing, the key here is to % d asked the conversion process must distinguish between values of positive and negative, it is very easy, only need to judge its highest close binary form (the sign bit) ok: 0 is a positive number, 1 is negative, however, in 197 and 198 the two Numbers in binary form, the sign bit does not seem to be 1, it's back to the start: in converts the value of the char type int type and passed to the printf, how convert? Further, two constants in 197 and 198 type is int, c1 and c2 to assign them to variables, need to transfer from int to char, the conversion process and how?

It involves numerical transformation rules, first of all, converts an integer value of type to another integer types, except _Bool if this value can be used to represent the new type, is transformed values,

See
 char c1 and c2. 
C1=197;
C2=198;


Here, if char is equivalent to unsigned char, then, in the mainstream of machine, it can be said of the numerical value range is 0 to 255, so can accommodate, and printf impossible to print two negative, obviously, under the environment of the building Lord, char was actually signed char, the mainstream of machine, it can be said of numerical range is 127 to + 128, so cannot say 197 and 198, need to transform,

However, if there is a new type symbol and cannot be said of the value, the result of the transformation depends on the specific compiler, and may even trigger signal mechanism, i.e., not provided how to convert C

On many platforms (including the original poster is obviously machine environment), is the length for the int 197 directly to the truncated to the length of the char types, so eventually it said a signed number to 59,

CodePudding user response:

references are ling rat spiderman 3 floor response:
this is a simple and complex problems,

First, % and % d c require corresponding change is type int, the only difference is that % c is to convert the type int variable parameter for unsigned char type of characters, and then display, and % d is to convert it into a signed decimal format of digital characters display,

Second, there is no question of procedure, although the incoming change is the char type, but in accordance with the default argument conversion (the default argument conversion) rules, after it is converted to type int is passed to the printf,

Finally, for the printf, % c is to pass along the the value of the int type (length), and then transform (narrow) for unsigned char type (length), and regard it as a character encoding, then print the characters; % d is to pass along the the value of the int type (length), split and converted into digital signed decimal number character printing, the key here is to % d asked the conversion process must distinguish between values of positive and negative, it is very easy, only need to judge its highest close binary form (the sign bit) ok: 0 is a positive number, 1 is negative, however, in 197 and 198 the two Numbers in binary form, the sign bit does not seem to be 1, it's back to the start: in converts the value of the char type int type and passed to the printf, how convert? Further, two constants in 197 and 198 type is int, c1 and c2 to assign them to variables, need to transfer from int to char, the conversion process and how?

It involves numerical transformation rules, first of all, converts an integer value of type to another integer types, except _Bool if this value can be used to represent the new type, is transformed values,

See
 char c1 and c2. 
C1=197;
C2=198;


Here, if char is equivalent to unsigned char, then, in the mainstream of machine, it can be said of the numerical value range is 0 to 255, so can accommodate, and printf impossible to print two negative, obviously, under the environment of the building Lord, char was actually signed char, the mainstream of machine, it can be said of numerical range is 127 to + 128, so cannot say 197 and 198, need to transform,

However, if there is a new type symbol and cannot be said of the value, the result of the transformation depends on the specific compiler, and may even trigger signal mechanism, i.e., not provided how to convert C

On many platforms (including the original poster is obviously machine environment), is the length for the int 197 directly to the truncated to the length of the char types, so, in the end it said a signed number to 59,



Hello, thank you very much for your reply, I use the compiler is VS2012, I am a C language for beginners, I still have some questions for your reply, in your reply said "in 197 and 198 the two Numbers in binary form, the sign bit does not seem to be 1," 197 - & gt; 11000101-198 & gt; The first 11000110 directly into the binary will become the sign bit directly? Please you help the process of 197-59 detailed said?

CodePudding user response:

refer to 4th floor, thanks to your reply:
Quote: refer to the third floor were ling man replies:
this is a simple and complex problems,

First, % and % d c require corresponding change is type int, the only difference is that % c is to convert the type int variable parameter for unsigned char type of characters, and then display, and % d is to convert it into a signed decimal format of digital characters display,

Second, there is no question of procedure, although the incoming change is the char type, but in accordance with the default argument conversion (the default argument conversion) rules, after it is converted to type int is passed to the printf,

Finally, for the printf, % c is to pass along the the value of the int type (length), and then transform (narrow) for unsigned char type (length), and regard it as a character encoding, then print the characters; % d is to pass along the the value of the int type (length), split and converted into digital signed decimal number character printing, the key here is to % d asked the conversion process must distinguish between values of positive and negative, it is very easy, only need to judge its highest close binary form (the sign bit) ok: 0 is a positive number, 1 is negative, however, in 197 and 198 the two Numbers in binary form, the sign bit does not seem to be 1, it's back to the start: in converts the value of the char type int type and passed to the printf, how convert? Further, two constants in 197 and 198 type is int, c1 and c2 to assign them to variables, need to transfer from int to char, the conversion process and how?

It involves numerical transformation rules, first of all, converts an integer value of type to another integer types, except _Bool if this value can be used to represent the new type, is transformed values,

See
 char c1 and c2. 
C1=197;
C2=198;


Here, if char is equivalent to unsigned char, then, in the mainstream of machine, it can be said of the numerical value range is 0 to 255, so can accommodate, and printf impossible to print two negative, obviously, under the environment of the building Lord, char was actually signed char, the mainstream of machine, it can be said of numerical range is 127 to + 128, so cannot say 197 and 198, need to transform,

However, if there is a new type symbol and cannot be said of the value, the result of the transformation depends on the specific compiler, and may even trigger signal mechanism, i.e., not provided how to convert C

On many platforms (including the original poster is obviously machine environment), is the length for the int 197 directly to the truncated to the length of the char types, so, in the end it said a signed number to 59,



Hello, thank you very much for your reply, I use the compiler is VS2012, I am a C language for beginners, I still have some questions for your reply, in your reply said "in 197 and 198 the two Numbers in binary form, the sign bit does not seem to be 1," 197 - & gt; 11000101-198 & gt; The first 11000110 directly into the binary will become the sign bit directly? Please you help the process of 197-59 detailed said?


Yes, I understand the two words don't say,
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related