C Input Output printf, scanf, Format Specifiers is an important C Language topic because it appears in real projects, debugging sessions, and interviews. Learn the meaning first, then connect it to a small working example so the rule does not stay abstract.
For this page, focus on what problem C Input Output printf, scanf, Format Specifiers solves, where developers usually make mistakes, and how to verify the result. The audit note for this lesson was: under 650 content words; limited checklist/practice/mistake/FAQ notes .
A strong understanding of C Input Output printf, scanf, Format Specifiers should include syntax, behavior, one realistic use case, one failure case, and one quick way to check your work with tools or output.
C Input Output printf scanf Format Specifiers should be studied as a practical C Language lesson, not as a label. Start by naming the input, the rule that changes the input, and the result a learner should be able to predict after reading the page.
In the c-language > input-output page, the notes should connect the definition with a working scenario, a mistake that beginners actually make, and the exact check that proves the fix. That makes the topic useful for coding, debugging, and interview revision.
printf() is the standard output function in C, defined in <stdio.h>. It prints formatted text to the console using format specifiers.
| Format Specifier | Type | Example |
|---|---|---|
| %d or %i | int | printf("%d", 42); |
| %f | float | printf("%f", 3.14f); |
| %lf | double | printf("%lf", 3.14); |
| %c | char | printf("%c", 'A'); |
| %s | string (char array) | printf("%s", "hello"); |
| %u | unsigned int | printf("%u", 42u); |
| %ld | long int | printf("%ld", 1000000L); |
| %lld | long long int | printf("%lld", 9999999999LL); |
| %o | Octal | printf("%o", 8); -> 10 |
| %x | Hexadecimal (lowercase) | printf("%x", 255); -> ff |
| %X | Hexadecimal (uppercase) | printf("%X", 255); -> FF |
| %e | Scientific notation | printf("%e", 12345.6); |
| %p | Pointer address | printf("%p", ptr); |
| %% | Literal % sign | printf("100%%"); |
| Escape Sequence | Meaning |
|---|---|
| \n | Newline |
| \t | Horizontal tab |
| \ | Backslash |
| \" | Double quote |
| \' | Single quote |
| \0 | Null character (string terminator) |
| \r | Carriage return |
| \a | Alert (bell) |
#include <stdio.h>
int main() {
int age = 25;
float price = 9.99f;
double pi = 3.14159265;
char grade = 'A';
char name[] = "Alice";
printf("Name: %s\n", name);
printf("Age: %d\n", age);
printf("Grade: %c\n", grade);
printf("Price: %.2f\n", price); // 2 decimal places
printf("Pi: %.4lf\n", pi); // 4 decimal places
// Width and alignment
printf("\n%-10s %5d\n", "Alice", 25); // left-align name, right-align age
printf("%-10s %5d\n", "Bob", 30);
printf("%-10s %5d\n", "Charlie", 22);
// Hex and octal
printf("\n255 in hex: %x\n", 255); // ff
printf("255 in octal: %o\n", 255); // 377
printf("Percent sign: 100%%\n");
return 0;
}
scanf() reads formatted input from the keyboard. You must pass the address of the variable using the & operator (except for strings/arrays).
getchar() reads a single character from stdin. putchar() writes a single character to stdout. gets() is unsafe (no bounds checking) - use fgets() instead.
#include <stdio.h>
int main() {
int age;
float salary;
char name[50];
// scanf reads formatted input; & gives the address of the variable
printf("Enter your name: ");
fgets(name, sizeof(name), stdin); // safer than gets()
printf("Enter your age: ");
scanf("%d", &age);
printf("Enter your salary: ");
scanf("%f", &salary);
printf("\nName: %s", name);
printf("Age: %d\n", age);
printf("Salary: %.2f\n", salary);
// getchar / putchar - single character I/O
printf("\nEnter a character: ");
getchar(); // consume leftover newline from scanf
char ch = getchar();
printf("You entered: ");
putchar(ch);
putchar('\n');
// puts - prints string with automatic newline
puts("Done!");
return 0;
}
When studying C Input Output printf, scanf, Format Specifiers, separate three things: the concept, the syntax, and the situation where it is useful. This prevents the lesson from becoming a list of commands with no practical meaning.
In C Language, C Input Output printf, scanf, Format Specifiers becomes easier when you build a tiny example first, then increase complexity. Add one realistic input, one invalid or boundary input, and one explanation of why the result changes.
#include <stdio.h>
int main(void) {
printf("C Input Output printf scanf Format Specifiers: normal path\n");
return 0;
}
#include <stdio.h>
int main(void) {
int count = 0;
if (count == 0) printf("C Input Output printf scanf Format Specifiers: empty input\n");
return 0;
}
Memorizing C Input Output printf scanf Format Specifiers without the situation where it is useful.
Connect C Input Output printf scanf Format Specifiers to a concrete C Language task.
Testing C Input Output printf scanf Format Specifiers only with the perfect input.
Include empty, missing, duplicate, incompatible, or failed cases when relevant.
Changing code before reading the visible symptom or error message.
Inspect the output, state, configuration, or stack trace connected to C Input Output printf scanf Format Specifiers.
Memorizing C Input Output printf scanf Format Specifiers without the situation where it is useful.
Connect C Input Output printf scanf Format Specifiers to a concrete C Language task.
The common mistake is memorizing syntax without understanding when the behavior changes or fails.
Remember the problem it solves in C Language, then attach the syntax or steps to that problem.
You can predict the result of a small example, explain a failure case, and choose it over a nearby alternative for a clear reason.
They often copy the syntax but skip the state, input, dependency, selector, route, type, or configuration that controls the behavior.
Explore 500+ free tutorials across 20+ languages and frameworks.