Variables and printf
C is statically typed: you declare the type of every variable.
int count = 3;
float price = 2.5f;
double precise = 3.14159;
char letter = 'A';
printf uses format specifiers to insert values:
| Specifier | Type | Example |
|---|---|---|
%d | int | printf("%d", 42); |
%f | float/double | printf("%.2f", 2.5); → 2.50 |
%c | char | printf("%c", 'A'); |
%s | string | printf("%s", "hi"); |
Try it
Print the number of apples and the total price (12 × 0.5) with two decimals:
Apples: 12
Total: 6.00