Number of bytes corresponding to common data types, int length

Source: Internet
Author: User

Number of bytes corresponding to common data types:

 

The two machines, the former 32-bit and the latter 64-bit, test the length of the following data types: the former: int: 4, long: 4, long: 8 the latter: int: 4, long: 8, long: 8 doesn't mean int changes, why is it long? In addition, if you want to write a general program, you must use 4 or 8 bytes of data type. If you do not need to re-code and compile the program, how can you deal with this change? Thank you for your advice .. The two machines are published on the 2nd floor at QUOTE: The original post is published by maxxfire at. The former is 32-bit, and the latter is 64-bit. The length of the following data types is tested: the former: int: 4, long: 4, long: 8; later: int: 4, long: 8, long: 8 doesn't mean int changes, why is it long? And if you want to write a general program, set... a: In a 16-bit system, sizeof (int) = 2; in some 64-bit systems, sizeof (int) = 8 is related to both the length of the machine and the compiler. to write a general program, we must use four or eight bytes of data. How can we deal with this change without re-encoding and compilation? "Using long is better, but it is difficult to re-compile other code! Use int32_t, int64_t, int16_t, and other types in the header file <stdint. h> The ====================================== two machine, the former is 32 bits and the latter is 64 bits. The length of the following data types is tested: The former is int: 4, long: 4, long: 8, and the latter is int: 4, long: 8, long: 8 doesn't it mean int changes, why is it long? The common programming model for existing 32-bit Unix stystems is caled the ILP32 model, denoting that integers, long integers and pointers occupy 32 bits. the model that is becoming most prevalent for 64-bit Unix systems is called LP64 model, meaming only long integers and pointers require 64 bits. also, if you want to write a general program, you must use four or eight bytes of data type to subscribe to it. How can you deal with this change without re-coding and compiling? I agree with the upstairs opinion, but the landlord does not require recompilation.

Iso/ansi does not specify the length of internal data types (the reason is simple. If they do this, most of the existing code is unavailable, therefore, the standards committee handed over tasks defining data types to compiler developers. The length of data types is determined by the compiler and the CPU. Recently, a program was debugged, consuming the loss of data length. Data length is critical to program portability. Here we will summarize the basic data type length of C and its relationship with the compiler and CPU.

 

A trap of data length

The data types of Char, short, int, and long have different lengths on different machines, not only the data length, but also the symbol definitions of these data types by different compilers. For example, in some compilers, char is signed by default, and some compilers treat char as unsigned. Portability is very important in embedded software. Therefore, when MISRA (automotive industry software reliability Federation) recommends that you use these data types, either signed or unsigned must be mentioned before, clearly describe the symbols of this data type.

Sample code

Char ch;

Ch = 0xff;

If (ch = 0xff )......

Anyone who has learned C should know the implicit type conversion rules of C. If the two data types of the operation are different, the low-level to advanced conversion will be performed. In this example, ch is of the char type, low level, 0xff is an integer, and advanced. In the if statement, ch must be converted to an integer. The conversion operation is to fill in the symbol bit. If it is an unsigned number, fill in 0. in this example, if ch is an unsigned number, after the extension ch = 0x00ff (assuming that the integer is 16 bits), the if statement is true. however, if ch is signed and ch = 0 xffff is extended, the if statement will not be valid. in addition, pay attention to logical operators and left Shifting. The right shifting operator does not support this implicit type conversion rule.

 

Now, if we assume that char is an 8-bit (this is true for most machines), the signed char range should be-127 ~ 127, the range of unsigned char should be 0 ~ 255. ANSI/ISO is written in this way. Here, some people may have doubts, because we may see that the range of signed char in some books is-128 ~ 127. this is the complement representation of it. in fact, ANSI/ISO does not specify the number of symbols to be represented by a complement code. Therefore, if you check the ANSI/ISO standard, you will find that the signed char is-127 ~ 127. however, in reality, almost all machines use a complement to represent the number of symbols. A positive complement is itself, and a positive complement is a complement of 1 in addition to the sign bit, the smallest negative number (signed char) is expressed as 0x80 in the computer, and its original code is-128.

 

2. Data types supported by common compilers

The basic data types in C are not portable. On different machines, the data length of different compilers is different. ISO recommends that programmers do not directly use the basic data type when writing code to be transplanted, but use typedef to redefine it. In this way, you only need to modify a small part of the code when porting the program. However, the premise of typedef is that you have a clear understanding of the data types supported by the compiling environment. I went online and found out that there is no summary of the data type length currently supported by mainstream compilers. So I checked some information and made a summary here (not all of them, of course, only list the ones I used)

 

Type

Compiler

Bytes

 

Unsigned/signed char

Visual studio

1

Ads

1

Avr studio (gcc)

1

Keil c

1

Turbo c

1

--------------------------

----------------

 

 

Unsigned/signed short

Visual studio

2

Ads

2

Avr studio (gcc)

2

Keil c

2

Turbo c

2

--------------------------

----------------

 

 

Unsigned/signed int

Visual studio

4

Ads

4

Avr studio (gcc)

2

Keil c

2

Turbo c

2

--------------------------

----------------

 

 

Unsigned/signed long

Visual studio

8

Ads

4

Avr studio (gcc)

4

Keil c

4

Turbo c

4

--------------------------

----------------

 

 

Long long

Visual studio

8

Ads

4

Avr studio (gcc)

8

Keil c

8

Turbo c

4

--------------------------

----------------

 

 

Double

Visual studio

8

Ads

4

Avr studio (gcc)

   4

Keil c

4

Turbo c

8

 

 

 

1. The number of bytes allocated to different data types varies with the platform where the program runs. My personal understanding of the platform is CPU + OS + Compiler, because: 1. 64-bit machines can also be installed with 32-bit systems (Windows XP x64 ); 2. A 32-bit machine can have a 16/32-bit compiler (on XP, tc is 16-bit, and other common ones are 32-bit ); 3. Even a 32-bit compiler can generate 64-bit integers (int64 ). These are based on the common wintel platform, and other platforms (other CPU and OS) that we may rarely have access to. Therefore, I personally think that the concept of the platform is a combination of the three. Although the length of the three can be different, it is clear that they work together (that is, the length is equal, 32-bit CPU + 32-bit OS + 32-bit Compiler) to maximize the energy. Theoretically, I think the number of bytes of the data type should be determined by the CPU, but it is mainly determined by the compiler (the number of bytes is determined by the compiler during compilation ). 2. The number of bytes corresponding to common data types can be obtained, such as sizeof (char) and sizeof (char *). The 32-bit compiler: char: 1 byte char * (pointer variable ): 4 bytes (32-bit addressing space is 2 ^ 32, that is, 32 bits, that is, 4 bytes. Similarly, 64-bit compiler) short int: 2 bytes int: 4 bytes unsigned int: 4 bytes float: 4 bytes double: 8 bytes long: 4 bytes long: 8 bytes unsigned long: 4 bytes 64-bit compiler: char: 1 byte char * (pointer variable): 8 bytes short int: 2 byte int: 4 bytes unsigned int: 4 bytes float: 4 bytes double: 8 bytes long: 8 bytes long: 8 bytes long: 8 bytes unsigned long: 8 bytes

 

Number of bytes corresponding to common data types, int length

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.