Fundamental data types
Integer Types
Data Type | Minimum Size (Standard) | Common Size (64-bit systems) |
|---|---|---|
| 1 byte | 1 byte |
| 1 byte | 1 byte |
| 1 byte | 1 byte |
| 1 byte | 1 byte |
| ≥ 2 bytes | 2 bytes |
| ≥ 2 bytes | 4 bytes |
| ≥ 4 bytes | 8 bytes (Linux/macOS), 4 bytes (Windows) |
| ≥ 8 bytes | 8 bytes |
Floating-Point Types
Data Type | Minimum Size | Common Size |
|---|---|---|
| ≥ 4 bytes | 4 bytes |
| ≥ 8 bytes | 8 bytes |
| ≥ 8 bytes | 16 bytes (Linux), 8 or 16 bytes elsewhere |
Pointer Types
Type | Size |
|---|---|
Any pointer ( | 4 bytes (32-bit system) |
8 bytes (64-bit system) |
All pointers have the same size on a given system. The type they point to is irrelevant.
Representation range
Assumption: int is 2 bytes (16 bits) and uses two’s complement representation.
The MSB (Most Significant Bit) represents the sign
0→ positive1→ negative
Remaining 15 bits are used to represent the value
Total values: 2^16 = 65536
Range:
Positive: 0 to +32767
Negative: -32768 to -1
Reason for asymmetry:
There is no −0 in two’s complement
One extra value goes to the negative side
Initialization
These are different ways to initialize int
Literals
Data type conversion
Coercion (Implicit)
Coercion is the automatic conversion of one data type into another by the compiler.
Coercion in assignment
Coercion in arithematic operations
Coercion in function arguments
Type Casting (Explicit)
Forced by the programmer.
Coercion can be dangerous
Why?
a / bis integer divisionResult becomes
2Then it’s coerced to
2.0
Too late. Damage done.
Correct way: