Home  >  Article  >  Java  >  Comparison of data type length between java and c/c++

Comparison of data type length between java and c/c++

高洛峰
高洛峰Original
2017-01-22 10:35:091195browse

1. The integer types in C language include char, short, int, long, etc. The following are the regulations on the length of each data type in C language:
(a). The lengths of short and long types are different. Same
(b). The int type is usually the same as the physical word length of the specific machine
(c). short is usually 16bits, int is usually 16bits or 32bits. Each compiler can freely determine it according to different hardware, but short and int must be at least 16 bits, and long type must be at least 32 bits, and short must be shorter than int and long types.

2. The sizeof() operator returns the number of bytes (bytes) contained in a data type. AnsiC stipulates that sizeof(char) must return 1. When sizeof is applied to an array, the returned It is the number of bytes occupied by all members in the array (note that it is not the number of members in the array). When sizeof() is applied to structures and public bodies, it returns not only the total number of bytes of data members, but also Includes those bytes padded by the compiler to implement byte alignment.

I used to vaguely understand these rules when writing programs, but I always thought that the char type must be 8 bits. However, I recently did an embedded DSP project, and the compiler manual clearly stated that the char type is 16 bits. , but I had no choice but to dig out "The C Programming Language" and found out that ANSI C does not have a rigid requirement for the length of the char type. In the past, I didn’t pay much attention to the portability of data types when writing programs. All previous codes used in this project have to be re-examined for data type length issues.

C++ data type length issues:

1. Bytes and word length
Bytes, eight bits are one byte, which is a fixed concept. Word length refers to the length of binary data that the computer can process at one time. It is a non-fixed concept. For example, the word length of an 8-bit computer is 8, which is one byte, and the word length of a 32-bit computer is 32, or 4 bytes. Similarly, the word length of a 64-bit computer is 64, or 8 bytes.

2. Data types in C++
1. Character data char, this type is always one byte long, that is, 8 bits.
2. Integer, short int and long integer. Usually int is one word long, short is half a word long, and long is one or 2 words long (one word long on a 32-bit machine).

3. Float, double, and long double represent single-precision floating-point numbers, double-precision floating-point numbers, and extended-precision floating-point values ​​respectively. Typically, float is one word, double is two words, and long double is three or four words.

The length of each data type in Java:

boolean depends on the trial compilation environment
byte 1 byte
short 2 bytes
char 2 words Section
int 4 bytes
long 8 bytes
float 4 bytes
double 8 bytes

More java and c/c++ data type lengths For more related articles, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn