Bytes of each type in C Language

Source: Internet
Author: User
Various data types in C language and the bytes and value range in the system

The C language contains five basic data types: void, Int, float, double, and char.
(C ++ defines the other two basic data types: bool and wchar_t.
Some basic data types can be modified by signed, unsigned, short, and long.
Therefore, short, long, and so on are not basic data types.
This is what I mentioned in the book, so c ++ is 7 basic data types.

Null is a type, but enumeration is not. The reason is that the enumerated type can be divided, so it is not basic.

However, different books have different rules. For example, C ++ primer States bool, Char, wchar_t, short, Int, long, float, double, long double, and void, this is not final yet .)

========================================================== ================================

The basic types include char, Int, and float/double ).

When defining basic type variables, you can use the symbol attributes signed, unsigned (for char, INT), and length attributes short, long (

Int, double) to describe the value range and accuracy of the variable.

The following lists the digits and value ranges of basic types in Dev-C ++:

Symbol property Length attribute basic type occupied by the number of digits Value Range Input operator example output operator example

-- Char 8-2 ^ 7 ~ 2 ^ 7-1% C % C, % d, % u

Signed -- char 8-2 ^ 7 ~ 2 ^ 7-1% C % C, % d, % u

Unsigned -- char 8 0 ~ 2 ^ 8-1% C % C, % d, % u

[Signed] short [int] 16-2 ^ 15 ~ 2 ^ 15-1% HD

Unsigned short [int] 16 0 ~ 2 ^ 16-1% Hu, % ho, % HX

[Signed] -- int 32-2 ^ 31 ~ 2 ^31-1% d

Unsigned -- [int] 32 0 ~ 2 ^ 32-1% U, % O, % x

[Signed] long [int] 32-2 ^ 31 ~ 2 ^ 31-1% LD

Unsigned long [int] 32 0 ~ 2 ^ 32-1% Lu, % Lo, % LX

[Signed] long [int] 64-2 ^ 63 ~ 2 ^ 63-1% i64d

Unsigned long [int] 64 0 ~ 2 ^ 64-1% i64u, % i64o, % i64x

-- Float 32 +/-3.40282e + 038% F, % E, % G

-- Double 64 +/-1.79769e + 308% lf, % Le, % LG % F, % E, % G

-- Long double 96 +/-1.79769e + 308% lf, % Le, % LG

Notes:

1. Note! Each row in the table represents a basic type. "[]" Indicates that it can be omitted.

For example, Char, signed Char, and unsigned char are different types;

Int, short, and long are also different types.

You can use the function overload feature of C ++ for verification, such:

Void func (char ch ){}

Void func (signed Char ch ){}

Void func (unsigned char ch ){}

Is three different functions.

2. The data length of Char/signed Char/unsigned char is 1 byte;

Char is signed, but it is different from signed Char.

Note! Not all compilers do this. Char data is not necessarily 1 byte in length, and char is not necessarily signed.

3. When Char/signed Char is converted to int, the maximum sign bit 1 is extended, resulting in operational problems.

Therefore, if the data to be processed contains a byte value greater than 127, it is more appropriate to use unsigned char.

If bit operations are involved in the program, the unsigned variable should also be used.

4. When Char/signed Char/unsigned char is output, use the format character % C (by character );

Or use % d, % u, % x/% x, % O, and output in integer mode;

% C should be used for input. If the integer is used, Dev-C ++ will give a warning. We do not recommend this.

5. The length of the int, whether it is 16-bit or 32-bit, is related to the compiler length.

16-bit compilers (for example, if the compiler used by tcworks is downloaded, The int_16 is a 32-bit Compiler (for example, the compiler cl.exe used by vcss), and The Int Is 32

Bit.

6. integer data can be input and output in % d, % O, or % x/% x (hexadecimal without symbols.

The format character % u indicates unsigned, that is, the hexadecimal mode without symbols.

7. The integer prefix h indicates short, and l indicates long.

When the input and output short/unsigned short, we do not recommend that you directly use the int format, such as % d/% u, with the prefix H.

This habitual error comes from TC. In TC, the int length and default symbol attributes are consistent with those in short,

Therefore, the two types are treated as the same, and input and output are performed in int mode.

8. input and output of the long type:

"% LLD" and "% LlU" are the format characters used by GCC/g ++ for long int type (64 bits) Input and Output in Linux.

While "% i64d" and "% i64u" are the format descriptions used in the Microsoft Vc ++ library for input and output _ int64 type.

The compiler used by Dev-C ++ is mingw32, and mingw32 is one of the x86-win32 GCC sub-projects. The core of the compiler is GCC in Linux.

During the compilation phase, the GCC compiler checks the format string. Obviously, it does not recognize "% i64d ",

Therefore, the warning "unknown conversion type character 'I' in format" will be given ". For "% LLD" and "% LlU", GCC

Of course, I accept it.

Mingw32 uses the GCC Rule Check syntax during compilation, but uses the Microsoft library for connection and runtime.

The printf and scanf functions in this library certainly do not know "% LLD" and "% LlU" under Linux GCC, but for "% i64d" and "% i64u", it is

Willing to accept and work properly.

9.% F, % E/% E, or % G/% G can be used for float data input. scanf will automatically process the data according to the input format.

You can use % F (normal mode), % E/% E (exponential mode), or % G/% G (automatically selected) for output ).

10. Floating Point parameter pressure stack rules: Float (4 bytes) type extended to double (8 bytes) into the stack.

Therefore, float (% F) and double (% lf) must be distinguished during input, while % F can be used for output.

The printf function outputs the float (extended to double) and double data pushed into the stack according to the double-type rules.

If the % lf format character is specified during output, the GCC/mingw32 compiler will give a warning.

11. Dev-C ++ (GCC/mingw32) can select the float length and check whether it is consistent with double.

12. prefix L indicates long (double ).

Although long double is 4 bytes longer than double, the value range is the same.

The length, precision, and representation range of the Long double type are related to the compiler and operating system used.

 
====================================================================
(1) 32-bit platform: including signed and unsigned platforms. Signed type: Short occupies two bytes in the memory, range:-2 ^ 15 ~ (2 ^ 15-1) int occupies four bytes in memory, range:-2 ^ 31 ~ (2 ^ 31-1) Long occupies four bytes in the memory, range:-2 ^ 31 ~ 2 ^ 31-1 unsigned: the highest bit does not indicate that the unsigned short occupies two bytes in the memory, and the range is 0 ~ 2 ^ 16-1 unsigned int occupies four bytes in memory, ranging from 0 ~ 2 ^ 32-1 unsigned long occupies four bytes in the memory, ranging from 0 ~ 2 ^ 32-1 Real-type variables: Single-precision float and double-precision float: 4 bytes, providing 7 ~ 8-digit valid number. Double: Eight bytes, providing 15 ~ A 16-digit valid number. (2) 16-bit platform: 1) INTEGER (basic type): The type description is int, which occupies 2 bytes in the memory. 2) Short INTEGER: The type description is short Int or short. The bytes and value range are the same as those of the integer type (basic type. 3) Long Integer: The type description is long Int or long, which occupies 4 bytes in the memory. Unsigned: The type description is unsigned. The unsigned type can match the above three types: the memory space bytes occupied by various unsigned types are the same as the corresponding signed types. However, the negative number cannot be expressed because the symbol bit is omitted. Real variables are classified into three types: Single-precision (float type), double-precision (double type), and long-double-precision (Long double type. The single-precision model occupies 4 bytes (32-bit) memory space, and its value range is 3.4e-38 ~ 3.4e + 38. Only seven valid digits are allowed. The dual-precision model occupies 8 bytes (64-bit) memory space, and the value range is 1.7e-308 ~ 1.7e + 308, which provides 16 valid digits. The long dual-precision 16-byte (128-bit) memory space provides 18-19 valid digits.
============================================================================
The order of the storage space lengths of the five basic data types in C language:
char = signed char = unsigned char < short int = unsigned short int <= int = unsigned int <= long int = unsigned long int <= long long int = unsigned long long intfloat <= double <= long double
When short is used to modify an int, short int represents a short integer, which occupies 2 bytes of data. when int is modified with long, long int represents a long integer, which occupies 4 bytes of data. In VC ++ 6.0, int data also occupies 4 bytes of data, so what is the difference between int type and long type? Next let's answer this question.

We know that there are many c ++ development tools, and the number of bytes that the int type may occupy in different systems is different, the number of int-type bytes modified by short and long is fixed. This is true in any standard C ++ compiling system. therefore, if you need to write a program with good portability, declare the integer data as the int type modified by short and long.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.