Question about promotions C

Well, I learned how to use a loop to make my code more efficient, so that I can use a specific block of code that should be repeated without typing it again and again, and after trying to use something, until you learn to program something, I I feel it’s time to move on to the next chapter to learn how to use the control instructions to learn how to instruct the program to make a decision.

But the fact is that before I move on to him, I still have a few questions that require any kind of expert help on previous materials. This is actually about a data type.


and. Character type

  • I am extracting the following from C primer Plus 5th ed:

Somewhat strange, C refers to the character as a type int, not char. For example, in an ASCII system with 32-bit intand 8-bit char, the code:

char grade = 'B';

represents 'B'66 as a numeric value stored in a 32-bit block, gradeexits with 66 saved ub ab 8-bit blocks. This character characterization constant allows you to define a character constant, such as 'FATE', with four separate 8-bit ASCII codes stored in a 32-bit block. However, an attempt to assign such a symbol a constant to variables a is charonly in the last 8 bits, so the variable gets the value 'E'.

  • , , , , , , FATE char grade , printf(), , 'E', 'F'.

  • , - ? -, ?

  • , C int. , , , , 255, (e.x. 356) char.

  • 356 32- int ( Windows 7), , 356, %d.

  • 356 100, 8- .

  • ? char == int == 32-bits? ( , char ).


. Int

  • , short, , int.

  • , float, double, float, %f double %Lf long double.

  • short , , float ? float %hf - ? - ?

+3
4

... :

, ​​ "FATE", 8- ASCII, 32- . , ​​ char 8 , "E" .

, . , . C , C , .

, , "FATE", "F" , "E" . "F", , "E" - . , - , MSVC , MinGW ( GCC, Windows) .

printf(), float, , double - , , printf() , (... printf()). , , ( C99 6.5.2.2/6 " " ):

, , , , , float . .

C99 6.5.2.2/7 " "

, . .

, float printf() - double. a double.

- , short, , h short ( n, , , short). C, , n, -, .

+2

-, a char 1 . , :

sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long)

( char) , 32- Windows GCC VC (AFAIK):

sizeof(short) == 2 (byte)
sizeof(int) == sizeof(long) == 4 (byte)

"F" "E" ( , "" ).

, ? 8 . ('FATE' 356), , 8 , .

+1

To A: 3.) . endian (.. X86) ( PPC). 8 , fom int char, int .

7.) a char 8 , , int char char .

B: 3.) 16 int , . . , , , float printf.

0

char 1 . 8, 16, 32 . - 8 . , , , . limits.h, CHAR_BIT .

char x = 'FATE' , , , / "FATE". /. -, , / .

If your system has an 8-bit byte, then when you do c = 360, only the lower 8 bits of the 360 ​​binary representation will be stored in the variable, because char1 byte of memory is always always allocated. This way it %dwill print 100 because the top bits were lost when you assigned the value to the variable and the rest is only the lower 8 bits.

0
source

All Articles