Click the blue text to follow us
Data Types in C Language (Integer, Floating-Point, Character)
The data types in C define how variables are stored, their range of values, and the operations that can be performed on them. Mastering the basic data types is key to writing efficient programs. This section introduces three core types: Integer, Floating-Point, and Character.
1. Integer Types (整型)
Integers are used to represent whole numbers and are divided into different subtypes, distinguished by memory usage and value range:
|
Type |
Typical Bytes |
Value Range (signed) |
Description |
|
short |
2 bytes |
-32768 ~ 32767 |
Short integer, suitable for small range integers |
|
int |
4 bytes |
-2³¹ ~ 2³¹-1 |
Default integer, balancing memory and performance |
|
long |
4 or 8 bytes |
-2³¹ ~ 2³¹-1 or larger |
Long integer, compatibility depends on compiler and system |
|
long long |
8 bytes |
-2⁶³ ~ 2⁶³-1 |
New in C99, extremely large integer range |
Modifiers:
– `signed` (default): includes both positive and negative values, such as `signed int`.
– `unsigned`: only non-negative values, doubling the range, such as `unsigned short` (0 ~ 65535).
Example:

2. Floating-Point Types (浮点型)
Floating-point types represent decimal or scientific notation values, supporting single and double precision:
|
Type |
Bytes |
Precision |
Typical Value Range (absolute value) |
|
float |
4 bytes |
about6~7 decimal places |
1.2e-38 ~ 3.4e38 |
|
double |
8 bytes |
about15~16 decimal places |
2.3e-308 ~ 1.7e308 |
|
long double |
8~16 |
Higher precision |
Depends on compiler implementation |
Note:
– Floating-point operations may have precision errors (e.g., `0.1 + 0.2 != 0.3`), avoid precise comparisons.
– Default floating-point literals (e.g., `3.14`) are of type `double` and adding `f` suffix indicates `float` (e.g., `3.14f`).
Example:
3. Character Type (字符型)
The character type (`char`) stores a single character, actually stored as an **ASCII** code integer value, occupying 1 byte:
|
Type |
Bytes |
Value Range (signed) |
Description |
|
char |
1 byte |
-128 ~ 127 or 0~255 |
Specific range depends on whether the compiler is signed |
Characteristics:
– Characters are enclosed in single quotes (e.g., `’A’`), supporting escape sequences (e.g., `’
‘` for newline).
– `char` can interoperate with integers (e.g., `char c = 65;` is equivalent to `’A’`).
Example:
Summary
– Integers: choose `short`, `int` or `long` based on value range and memory needs.
– Floating-point: prefer `double` for precision, use `float` when memory is a concern.
– Character: handle text or small range integers, note that sign depends on the compilation environment.
Choosing the right data type can optimize program performance and memory usage while avoiding overflow or precision loss issues.
END
C Language
Programming is not about shortcuts,
Only through rigorous practice.