C Data Types
In C, a data type refers to a broad system used to declare variables or functions of different types. The type of a variable determines the space occupied by the variable's storage and how the stored bit pattern is interpreted.
Types in C can be classified as follows:
| No. | 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 specifier void A data type representing no value, typically used as a function's return type. | |
| 4 | Derived types: |
| Include array types, pointer types, and structure types. |
Array types and structure types are collectively called aggregate types. A function's type refers to the type of its return value. In the rest of this chapter we will introduce the basic types; the other types will be explained in the following chapters.
Integer type
The following table lists details about the storage size and value ranges of the 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 is related to the system's word size, but currently 64-bit systems are the most common.
The following lists the storage size differences between 32-bit and 64-bit systems (the same on Windows):
To get the exact size of a type or a variable on a specific platform, you can use the sizeof operator. The expression sizeof(type) yields the size in bytes of an object or type. The following example demonstrates getting the size of the int type:
Example
#include
<
stdio.h
#include
<
limits.h
int
main
(
)
{
printf
(
"int storage size: %lu
\
n
"
,
sizeof
(
int
)
)
;
return
0
;
}
%lu is a 32-bit unsigned integer; for details see C library function - printf().
When you compile and run the above program on Linux, it produces the following result:
int 存储大小 : 4
Floating-point types
The following table lists details about the storage size, value range, and precision of the 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 you can use in your program for these values and other details about the binary representation of real numbers. The following example outputs the storage space occupied by floating-point types and their range values:
Example
#include
<
stdio.h
#include
<
float.h
int
main
(
)
{
printf
(
"float storage size in bytes: %lu
\
n
"
,
sizeof
(
float
)
)
;
printf
(
"
float minimum value: %E
\
n
"
,
FLT_MIN
)
;
printf
(
"
float maximum value: %E
\
n
"
,
FLT_MAX
)
;
printf
(
"
precision value: %d
\
n
"
,
FLT_DIG
)
;
return
0
;
}
%E to output single- and double-precision real numbers in exponential form; for details see C library function - printf().
When you compile and run the above program on Linux, it produces the following result:
float 存储最大字节数 : 4
float 最小值: 1.175494E-38
float 最大值: 3.402823E+38
精度值: 6
void type
The void type specifies that no value is available. It is typically used in the following three situations:
| No. | Types and Descriptions |
|---|---|
| 1 | Function returns empty |
| There are various functions in C that return no value, or you could say they return empty. The return type of a function that returns no value is empty. For example void exit (int status); | |
| 2 | Function arguments empty |
| There are various functions in C that accept no arguments. A function with no arguments can take a void. For example int rand(void); | |
| 3 | Pointer to void |
| A pointer of type void * represents the address of an object, not its type. For example, the memory allocation function *void malloc( size_t size ); returns a pointer to void, which can be converted to any data type. |
If you still cannot fully understand the void type right now, do not worry too much; we will explain these concepts in detail in later chapters.
Type Conversion
Type conversion is the process of converting the value of one data type into the value of another data type.
There are two kinds of type conversion in C:
-
Implicit type conversion:Implicit type conversion occurs automatically in an expression without any explicit instruction or function call. It usually converts a smaller type to a larger type automatically, for example, converting int to long or float to double. Implicit conversion may also cause loss of precision or data truncation.
-
Explicit type conversion:Explicit type conversion requires a type casting operator, which forces a value of one data type to be converted to another data type. Casting gives the programmer more precise control over data types when needed, but it may also lead to data loss or truncation.
Implicit type conversion example:
Example
int
i
=
10
;
float
f
=
3.14
;
double
d
=
i
f
;
// Implicitly convert int type to double type
Explicit type conversion example:
Example
double
d
=
3.14159
;
int
i
=
(
int
)
d
;
// Explicitly convert double type to int type
