Differences between Java language data types and C language data types

Table of contents

1. C language data type

1. Basic type:

2. Enumeration type:

3. Empty type:

4. Derived type:

2. The difference between 64-bit and 32-bit machines that need to be paid attention to in C language programming  

3. Differences


1. C language data type

First of all, let's introduce the data type classification of C language as a whole.

1. Basic type:

1.1 Integer types: basic integer (int), short integer (short int), long integer (long int), double long integer (long long int), character (char) and Boolean (bool).
Note: Integer data can be modified before the type symbol: signed (signed integer type) and unsigned (unsigned integer type), if not specified, the default is "signed type".

1.2 Floating-point types: single-precision floating-point (float), double-precision floating-point (double)


2. Enumeration type:

enumerated type. An enumeration type is only an enumeration type ( enum ).


3. Empty type:

empty type. Empty types are just empty types ( void ).


4. Derived type:

derived type. Derived types include: pointer type (*) , array type ([ ]) , structure type (struct) , union type (union)

Give you a mind map for reference only

2. The difference between 64-bit and 32-bit machines that need to be paid attention to in C language programming  

Data types, especially int-related types, have different lengths under different machine platforms. The C99 standard does not specify the length of a specific data type, only the level. Make the following comparison:

Under 32-bit platform:

char 1 byte
short: 2 bytes
int 4 bytes
long 4 bytes
long long 8 bytes
float 4 bytes
double 8 bytes
bool 1 byte
pointer          4 bytes

Under 64-bit platform:

char 1 byte
short: 2 bytes
int 4 bytes
long 4 bytes
long long 8 bytes
float 4 bytes
double 8 bytes
bool 1 byte
pointer          8 bytes

Just to clarify, this table does not cover all basic data types.

If you don't have a special understanding of bytes, you can read this article of mine: http://t.csdn.cn/myTeu

3. Differences

The data types of Java and the data types of C language have many similarities but also have many differences

Data types in Java are mainly divided into two categories: basic data types and reference data types

There are eight basic data types in four categories:

1. Four types: integer, floating point, character and Boolean
2. Eight types:

1. The Java code runs in the JVM virtual machine, which has nothing to do with the operating system, so whether it is in a 32-bit system or a 64-bit system, int in Java occupies 4 bytes, and long occupies 8 bytes. In the c language, it is necessary to look at the system.

2. The char in Java is two bytes. This is because Java uses the Unicode character set, which can represent far more characters than the ASCII character set, and includes many languages, such as Chinese, Latin, and so on. Therefore, according to the char capacity (255) of the c language, it is far from enough to store. Therefore, the char capacity of java has also come to 2 bytes. The char in Java is an unsigned number, so its capacity is 65535. Therefore, Java's character type can also represent many characters that cannot be represented by C language. The default char type in the C language is signed, and in the C language, Chinese characters cannot be represented.

3. There is a type of byte in the Java language, but not in the C language.

4. There is a string type in the Java language, but not in the C language.

5. The integer type of the Java language does not have the so-called signed type and unsigned type in the C language. The integer type of Java can represent both positive and negative numbers.

6. The boolean type (Boolean type) that is not in the c language is introduced in Java to represent true and false, among which the boolean type true is true and false is false. It is worth noting that this also leads to the statement that 0 is false in c language and non-zero is true is not true in Java. 

7. Literal values ​​in Java have default types, such as integer 1, the default type is int type, if you want to write a variable of type long, you can only write long a=1L (where L can be lowercase), floating point The type also has similar regulations, decimals are double by default, so if you want to write a variable of float type, you can only write float b=0.5f (f can be uppercase).

8. In Java, if the local variable is not assigned a value, then the compilation will not pass, and the compiler will report an error, while the C language does not need to be initialized, but the local variable is a random value.

9. The corresponding data types in Java can only be assigned from small byte numbers to large byte numbers, while C language has no effect. For example, data of type double in Java cannot be assigned to type float, but it is not affected in C language.
 

The above are some differences between the Java language and the C language in terms of data types. If there are errors or deficiencies, please correct me. 

Guess you like

Origin blog.csdn.net/m0_63562631/article/details/130124289