Lesson 7: Fundamental Data Types: Floating Point and Character
Floating Point Types
Used for storing real numbers (numbers with a decimal point).
| Type | Description | Typical Precision (Decimal Places) | Typical Size (Bytes) |
|---|---|---|---|
float | Single precision | 6-7 | 4 |
double | Double precision (standard choice) | 15-16 | 8 |
long double | Extended precision | Usually 18+ | 10 or 12 |
c // Floating point examples float temperature = 25.5f; // 'f' suffix indicates float literal double pi = 3.1415926535; // Default floating-point literal is double
Character Type (char)
Used to store a single character (letters, digits, symbols). Characters are internally stored as their corresponding ASCII (or Unicode) integer values.
- A
charis usually 1 byte (8 bits). - It can be treated as a small integer (signed or unsigned).
c char grade = 'A'; // Single quotes for characters char newline = '\n'; // Storing an escape sequence
// Char treated as an integer char ascii_val = 65; // ASCII 65 is 'A'
sizeof() Operator
The sizeof() operator tells us how many bytes of memory a type or variable occupies. This is useful for understanding portability and memory usage.
c #include <stdio.h>
int main() { printf("Size of int: %zu bytes\n", sizeof(int)); printf("Size of double: %zu bytes\n", sizeof(double)); return 0; }
(Note: %zu is the format specifier for sizeof result.)