Theory
The costume party
You have been trusting %d and %f for three lessons. Time to decode them.
Run this experiment mentally: printf("%d", 78.5);, printing a float in an int's costume. The output is not 78, not 79, but garbage: perhaps -1717986918.
printf does not check who wears what. The type specifier tells it how to read raw bytes, and bytes read in the wrong costume become nonsense. Matching costumes to guests is today's whole art.
Theory
Fill-in-the-blanks with shaped blanks
A printf format string is a sentence with blanks: "Marks: __, Grade: __". But each blank is SHAPED: a %d blank fits an int, a %c blank fits one character, a %f blank fits a decimal. Push a float through an int-shaped blank and it comes out mangled, like forcing a round dabba into a square slot. The blanks are the specifiers; the shapes must match.
At a glance
The specifier family
| Specifier | Type | Example value |
|---|---|---|
| %d | int | 78 |
| %ld | long | 8930012345 |
| %f | float (printf: double too) | 78.500000 |
| %lf | double (needed in scanf) | 78.53 |
| %c | char (one character) | A |
| %s | string (char array) | Riya |
Practical
One marksheet, every specifier
#include <stdio.h>
int main() {
char name[20] = "Riya";
int marks = 78;
float percentage = 78.53;
char grade = 'A';
printf("Name : %s\n", name);
printf("Marks: %d\n", marks);
printf("Perc : %.2f\n", percentage);
printf("Grade: %c (ASCII %d)\n", grade, grade);
return 0;
}This example runs in Gri-Learn on the web, where you can edit it and see the output.
Think first
One char, two costumes
The last printf prints grade twice: once as %c and once as %d. Predict that line's exact output, using what a char really is.
Show the answer
Grade: A (ASCII 65)
Same variable, same byte: %c dresses it as a character (A), %d as its number (65). Nothing converts; only the reading costume changes. This one line is the cleanest proof that a char is an ASCII number in disguise, and a favourite predict-the-output item.
Quiz
What does printf("%d", 78.5); actually print?
- Unpredictable garbage: the bytes are read in the wrong format
- 78, C removes the decimal part
- 79, C rounds it
- 78.5, printf adjusts automatically
Show the answer
Unpredictable garbage: the bytes are read in the wrong format
Float bytes read through an int lens are meaningless: the output is garbage and formally undefined behaviour. The truncate-to-78 answer confuses this with ASSIGNMENT (int x = 78.5 does truncate); printf converts nothing, it only reads bytes as told. Distinguishing the two cases is exactly what examiners are probing.
Quiz
You are reading a double with scanf. Which specifier is REQUIRED?
- %lf
- %f
- %d
- %s
Show the answer
%lf
scanf must know the exact box size it is filling: %f fills 4 float bytes, %lf fills 8 double bytes, so a double NEEDS %lf. The asymmetry trips everyone: in printf, %f happily prints both float and double. Compact rule for the margin of your answer sheet: scanf is strict, printf is lenient.
Watch out
Where marks leak
Mismatched pairs: %d with floats, %f with ints, garbage both ways. %c vs %d on a char changes what appears (A vs 65), watch which the question wants. In scanf, forgetting that %lf is required for double. And a neat habit worth marks: %.2f prints two decimals (78.53 instead of 78.530000), the display rounds, the stored value stays untouched.
Theory
Unit 1 is complete
Look at what the marks program can now do: declare typed boxes, take input, compute with operators, and print a formatted marksheet, every Unit 1 topic is muscle now, not theory. One small preview hid in today's code: scanf for %s needs no &, because an array's name is already an address; that mystery unlocks in Unit 3 (arrays and pointers). Next: Unit 2, where the program starts making decisions.
Summary
Key takeaways
- Specifiers tell printf/scanf how to read bytes: %d int, %ld long, %f float, %lf double, %c char, %s string.
- Mismatch = garbage: printf converts nothing (%d with 78.5 is undefined).
- scanf is strict (%lf required for double); printf is lenient (%f prints both).
- %c vs %d on the same char: letter vs ASCII number.
- %.2f trims the DISPLAY to two decimals; the stored value is unchanged.
- Memory hook: shaped blanks, the costume must match the guest.