Chapter 4: Input and Output
1. printf() – Printing Output (We Already Know Some)
printf() is used to print messages and values on the screen.
Basic Syntax:
|
0 1 2 3 4 5 6 |
printf("message", variables); |
Format Specifiers (very important – you must use the correct one):
| Data Type | Format Specifier | Example | What it does |
|---|---|---|---|
| int | %d | printf(“%d”, age); | Prints integer |
| float | %f | printf(“%f”, height); | Prints float (6 decimals by default) |
| double | %lf or %f | printf(“%.2lf”, pi); | Prints double |
| char | %c | printf(“%c”, grade); | Prints single character |
| string | %s | printf(“%s”, name); | Prints string |
Formatting Tricks:
- %.2f → Show 2 decimal places
- %10d → Right-align with 10 spaces
- %-10s → Left-align string with 10 spaces
- \n → New line
- \t → Tab space
Example: Nice Formatted Output
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <stdio.h> int main() { int age = 25; float height = 5.9; char grade = 'A'; char name[20] = "Alex"; printf("Student Information\n"); printf("--------------------\n"); printf("Name : %-20s\n", name); // Left-aligned printf("Age : %d years\n", age); printf("Height : %.1f feet\n", height); // 1 decimal place printf("Grade : %c\n", grade); return 0; } |
Output:
|
0 1 2 3 4 5 6 7 8 9 10 11 |
Student Information -------------------- Name : Alex Age : 25 years Height : 5.9 feet Grade : A |
2. scanf() – Taking Input from the User
scanf() reads input from the keyboard and stores it in variables.
Basic Syntax:
|
0 1 2 3 4 5 6 |
scanf("format specifiers", &variable1, &variable2, ...); |
Important Rule: Always use & (address of) before the variable name! & tells scanf: “Put the input at this memory location.”
Format Specifiers for scanf() (same as printf):
| Data Type | Format Specifier |
|---|---|
| int | %d |
| float | %f |
| double | %lf |
| char | %c |
| string | %s |
Example 1: Simple Input
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <stdio.h> int main() { int age; printf("Enter your age: "); scanf("%d", &age); // &age is very important! printf("You entered: %d\n", age); return 0; } |
How it works:
- Program prints: “Enter your age: “
- You type 25 and press Enter
- scanf stores 25 in the variable age
- Then it prints: “You entered: 25”
3. Full Practical Example – Interactive Program
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
/* Program: Simple Student Details Purpose: Take input from user and display formatted output */ #include <stdio.h> int main() { char name[30]; // For storing name (string) int age; float height; char grade; // Taking input printf("Enter your name: "); scanf("%s", name); // For string, NO & needed! printf("Enter your age: "); scanf("%d", &age); printf("Enter your height (in feet): "); scanf("%f", &height); printf("Enter your grade (A/B/C): "); scanf(" %c", &grade); // Notice space before %c (explained below) // Printing nicely formatted output printf("\n=== Your Details ===\n"); printf("Name : %s\n", name); printf("Age : %d years\n", age); printf("Height : %.1f feet\n", height); printf("Grade : %c\n", grade); return 0; } |
Sample Run:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Enter your name: Alex Enter your age: 20 Enter your height (in feet): 5.8 Enter your grade (A/B/C): A === Your Details === Name : Alex Age : 20 years Height : 5.8 feet Grade : A |
4. Important Tips & Common Mistakes
Tip 1: Why space before %c?
|
0 1 2 3 4 5 6 |
scanf(" %c", &grade); |
After entering number (age), Enter key (newline) stays in buffer. Space before %c eats that newline. If you forget space → grade may take wrong value!
Tip 2: For strings with spaces (full name) Use %[^\n] or fgets() (we’ll learn fgets later – safer than scanf for strings).
Common Mistakes:
- Forgetting & → program crashes or wrong value
- Wrong format specifier → garbage output
- Not putting space before %c
5. Another Fun Example – Simple Calculator
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <stdio.h> int main() { int num1, num2; float result; printf("Enter first number: "); scanf("%d", &num1); printf("Enter second number: "); scanf("%d", &num2); result = (float)(num1 + num2); // Type casting to get float result printf("\nSum of %d and %d is %.2f\n", num1, num2, result); return 0; } |
Today’s Homework
- Run the Student Details program above.
- Change it to ask for:
- Your full name (use scanf(“%[^\n]”, name); – or just one word for now)
- Your city
- Your favorite number
- Print everything in a beautiful format (use lines, tabs, etc.)
- Create a program that asks for two numbers and prints their sum, difference, product, and division (use float for division).
- Try forgetting the & in one scanf and see what happens (error or wrong output).
