Theory
A marksheet should look like one
The marks program can compute everything now. But its output looks like a ransom note: values dumped anywhere, lines colliding.
A real marksheet has a name line, neat labels, and a result set apart.
Presentation is the output statements' job, and C gives you four mouths of different sizes: printf (says anything), puts (says one string, then takes a new line itself), putchar and putc (say exactly one character). Choosing between them is a guaranteed exam comparison.
Theory
Announcer, stamp, and a single bell
printf is the event announcer: reads a script with blanks ("the total is ___") and fills each blank live from your variables. puts is a rubber stamp: prints its one line and automatically moves the paper down (its built-in newline). putchar is a single bell: one clean chime, one character, nothing more. Different jobs, one stage: the screen.
At a glance
The four output statements
| Function | Prints | Newline? |
|---|---|---|
| printf(...) | Text + formatted values via % | Only if you write \n |
| puts(str) | One string | Adds its own \n, always |
| putchar(ch) | Exactly one character | Never |
| putc(ch, stdout) | One character (file-flavoured) | Never |
Practical
The marksheet, dressed properly
#include <stdio.h>
int main() {
int marks = 78;
char grade = 'A';
puts("===== MARKSHEET =====");
printf("Marks : %d\n", marks);
printf("Grade : %c\n", grade);
putchar('*');
putchar('\n');
return 0;
}This example runs in Gri-Learn on the web, where you can edit it and see the output.
Quiz
What is the key difference between puts("Hello") and printf("Hello")?
- puts adds a newline after Hello automatically; printf does not
- printf adds a newline automatically; puts does not
- puts can print variables; printf cannot
- There is no difference at all
Show the answer
puts adds a newline after Hello automatically; printf does not
puts always appends its own \n: the cursor moves to the next line without you asking. printf prints exactly what you wrote, staying on the same line unless the format string contains \n. (And it is printf that formats variables via %; puts takes just one plain string.) This newline difference is the single most asked output-statement question.
Think first
Predict the output
Walk the code block above line by line, remembering each function's newline habit. Write the exact screen output before revealing.
Show the answer
===== MARKSHEET =====
Marks : 78
Grade : A
*
The puts line ends itself; each printf ends because of its own \n; the star needed a second putchar('\n') to close its line, putchar never adds one. Trace the newlines and predict-the-output questions become free marks.
Watch out
Where marks leak
Writing printf(marks); to print a variable: printf's first argument must be a format string, so it is printf("%d", marks);. Placeholders without matching values (printf("%d %d", marks)) print garbage, the count must match. And putchar prints ONE character: putchar("Hello") is wrong twice over (double quotes, five characters). Small syntax, easy marks, frequent losses.
Theory
A little ASCII party trick
putchar takes a character, but remember what a char really is: a number in costume. putchar(65) prints A, putchar(66) prints B, straight from your BCA103 ASCII table. Exams sneak this in as predict-the-output. One mystery remains in this unit: what exactly %d, %f, %c and their cousins ARE. The next lesson decodes the whole placeholder family.
Summary
Key takeaways
- printf: formatted output with % placeholders; adds newline only via \n.
- puts: one string, plus an automatic newline, always.
- putchar/putc: exactly one character, never a newline.
- printf's placeholders must match its arguments in count and type.
- putchar(65) prints A: chars are ASCII numbers in costume.
- All four live in stdio.h.
- Memory hook: announcer (printf), stamp (puts), single bell (putchar).