[C/C ++] number of digits and value range of basic types in C ++

Source: Internet
Author: User

Number of digits and value range of basic types in 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.
ProgramIf bitwise operations are involved, 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-bit compiler is 32-bit (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 ". GCC naturally accepts "% LLD" and "% LlU.
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 can work normally.

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.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/shaochx/archive/2010/02/22/5315954.aspx

Related Article

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.