Choose int, double, and char
Record three kinds of value
The hinge box needs a count, a weight, and a shelf letter. Choose a type for each value before thinking about how to print it.
#include <stdio.h>
int main(void) {
int hinges = 6;
double kilograms = 1.5;
char shelf = 'B';
printf("Shelf %c: %d hinges, %.1f kg\n", shelf, hinges, kilograms);
return 0;
}
hinges is an int because it counts whole items. kilograms is a double because the measurement can have a fractional part. shelf is a char initialized with the character literal 'B'.
Match the print arguments to their placeholders:
| Placeholder | Argument | Displayed value |
|---|---|---|
%c | shelf | B |
%d | hinges | 6 |
%.1f | kilograms | 1.5 |
Run the label and follow each value across that table. '4' would mean the character used to write a digit, not the count four. Likewise, "B" is a string, not the single-character value used here.
Floating-point types are useful for measurements, but cannot represent every decimal fraction exactly. For these labels, the requested precision controls how many digits you display. The next exercise uses the same three types for a battery test.