C Data types
In C language, data types refer to a broad system used to declare variables or functions of different types. The type of a variable determines the space it occupies in storage and how the stored bit pattern is interpreted.
Types in C can be divided into the following categories:
| Serial Number | Types and Descriptions |
|---|---|
| 1 | Basic Data Types They are arithmetic types, including integer (int), character (char), floating-point (float), and double-precision floating-point (double). |
| 2 | Enumeration types: They are also arithmetic types, used to define variables that can only be assigned certain discrete integer values in a program. |
| 3 | void type: Type SpecifiervoidA data type representing no value, usually used for function return values. |
| 4 | Derived types: Including array types, pointer types, and structure types. |
Array types and structure types are collectively called aggregate types. The type of a function refers to the type of the function's return value. In the following part of this chapter, we will introduce basic types; the other types will be explained in later chapters.
Integer Types
The following table lists details about the storage sizes and value ranges of standard integer types:
| Type | Storage Size | Value Range |
|---|---|---|
| char | 1 byte | -128 to 127 or 0 to 255 |
| unsigned char | 1 byte | 0 to 255 |
| signed char | 1 byte | -128 to 127 |
| int | 2 or 4 bytes | -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647 |
| unsigned int | 2 or 4 bytes | 0 to 65,535 or 0 to 4,294,967,295 |
| short | 2 bytes | -32,768 to 32,767 |
| unsigned short | 2 bytes | 0 to 65,535 |
| long | 4 bytes | -2,147,483,648 to 2,147,483,647 |
| unsigned long | 4 bytes | 0 to 4,294,967,295 |
Note that the storage size of each type depends on the system's bit count, but currently 64-bit systems are the mainstream.
The following lists the differences in storage sizes between 32-bit systems and 64-bit systems (same on Windows):
To obtain the exact size of a type or a variable on a specific platform, you can usesizeofOperator. Expressionsizeof(type)Obtains the storage byte size of an object or type. The following example demonstrates getting the size of the int type:
Example
%luis a 32-bit unsigned integer, see for detailsC Library Function - printf()。
When you compile and execute the above program on Linux, it will produce the following results:
int 存储大小 : 4
Floating-point types
The following table lists details about the storage sizes, value ranges, and precision of standard floating-point types:
| Type | Storage Size | Value Range | Precision |
|---|---|---|---|
| float | 4 bytes | 1.2E-38 to 3.4E+38 | 6 significant digits |
| double | 8 bytes | 2.3E-308 to 1.7E+308 | 15 significant digits |
| long double | 16 bytes | 3.4E-4932 to 1.1E+4932 | 19 significant digits |
The header file float.h defines macros that allow you to use these values and other details about the binary representation of real numbers in your program. The following example will output the storage space occupied by floating-point types and their range values:
Example
%Eoutputs single- and double-precision real numbers in exponential form, see for detailsC Library Function - printf()。
When you compile and execute the above program on Linux, it will produce the following results:
float 存储最大字节数 : 4 float 最小值: 1.175494E-38 float 最大值: 3.402823E+38 精度值: 6
void type
The void type specifies that no value is available. It is usually used in the following three situations:
| Serial Number | Types and Descriptions |
|---|---|
| 1 | Function returns nothing In C, various functions do not return a value, or you can say they return nothing. The return type of a function that does not return a value is void. For example:void exit (int status); |
| 2 | Function parameters are empty In C, various functions do not accept any parameters. A function without parameters can accept a void. For example:int rand(void); |
| 3 | Pointer points to void A pointer of type void * represents the address of an object, rather than a type. For example, the memory allocation functionvoid *malloc( size_t size );returns a pointer to void, which can be cast to any data type. |
If you still cannot fully understand the void type now, don't worry too much; we will explain these concepts in detail in later chapters.
Type conversion
Type casting is converting a value of one data type to a value of another data type.
There are two types of type conversion in C:
-
Implicit type conversion:Implicit type conversion occurs automatically in expressions without any explicit instruction or function call. It usually automatically converts a smaller type to a larger type, for example, converting int to long or float to double. Implicit type conversion may also cause loss of data precision or data truncation.
-
Explicit type conversion:Explicit type conversion requires the use of the type casting operator, which can force a value of one data type to be converted to a value of another data type. Force casting allows programmers to control data types more precisely when necessary, but it may also cause data loss or truncation.
Implicit type conversion example:
Example
float f = 3.14;
double d = i + f; // Implicitly convert int type to double type
Explicit type conversion example:
Example
int i = (int)d; // Explicitly convert double type to int type
