How scanf Works — Quick Recap
scanf() reads input from the keyboard and stores it in a variable. It needs two things every time:
- Format specifier — tells scanf what type to expect:
%dint,%ffloat,%lfdouble,%cchar,%sstring - & operator — gives scanf the memory address of the variable so it knows where to store the value
Without & the program will crash or give wrong values. Only char[] (strings) do not need & because the array name is already an address.
| Data Type | scanf specifier | printf specifier | Example |
|---|---|---|---|
| int | %d | %d | scanf("%d",&n) |
| float | %f | %f | scanf("%f",&x) |
| double | %lf | %f or %lf | scanf("%lf",&d) |
| char | %c | %c | scanf("%c",&c) |
| string | %s | %s | scanf("%s",name) — no & |
P5 — Simple User Input
This is the simplest possible input program. It asks the user to type a number, reads it with scanf, and immediately prints it back. This proves the input was received and stored correctly.
#include <stdio.h> int main() { int iNumber = 0; printf("Please enter a number: "); scanf("%d", &iNumber); // %d reads an integer printf("You entered the number: %d\n", iNumber); return 0; }
Please enter a number: 42 You entered the number: 42
Line by line explanation:
int iNumber = 0;— declares variable, initialised to 0 so it does not hold garbageprintf("Please enter a number: ");— prompts user without\nso cursor stays on same linescanf("%d", &iNumber);— waits for user to type, reads an integer, stores at address of iNumberprintf("...%d\n", iNumber);— prints back what was stored
#include <stdio.h> int main() { float price = 0.0; printf("Enter item price: "); scanf("%f", &price); // %f for float printf("Price entered: %.2f\n", price); return 0; }
#include <stdio.h> int main() { char name[50]; int age = 0; printf("Enter your name: "); scanf("%s", name); // no & for char array printf("Enter your age: "); scanf("%d", &age); printf("Hello %s! You are %d years old.\n", name, age); return 0; }
Enter your name: Ananta Enter your age: 25 Hello Ananta! You are 25 years old.
printf() prompt before every scanf() — otherwise the program just freezes with a blank screen and the user does not know what to do.
P6 — Multiple Inputs
You can read multiple values in a single scanf() call by listing multiple format specifiers. The user separates values with spaces or Enter. C reads them in order.
#include <stdio.h> int main() { int iNum1, iNum2; printf("Enter two integers separated by a space: "); scanf("%d %d", &iNum1, &iNum2); // reads two ints printf("First: %d\n", iNum1); printf("Second: %d\n", iNum2); return 0; }
Enter two integers separated by a space: 10 25 First: 10 Second: 25
#include <stdio.h> int main() { int qty = 0; float price = 0.0; char grade = ' '; printf("Enter quantity, price, grade: "); scanf("%d %f %c", &qty, &price, &grade); printf("Qty: %d\n", qty); printf("Price: %.2f\n", price); printf("Grade: %c\n", grade); return 0; }
Enter quantity, price, grade: 3 99.50 A Qty: 3 Price: 99.50 Grade: A
scanf("%d %d", &a, &b) is correct. Writing scanf("%d %d", &a, b) — forgetting & on b — is a common mistake that causes wrong or garbage output.
P7 — Simple Calculator
C has five arithmetic operators. They follow mathematical precedence — * / % are evaluated before + and -. Use parentheses () to control order.
| Operator | Name | Example | Result | Note |
|---|---|---|---|---|
| + | Addition | 10 + 3 | 13 | Works on int & float |
| - | Subtraction | 10 - 3 | 7 | Works on int & float |
| * | Multiplication | 10 * 3 | 30 | Works on int & float |
| / | Division | 10 / 3 | 3 (not 3.33!) | Int division truncates! |
| % | Modulus | 10 % 3 | 1 | Only works on integers |
#include <stdio.h> int main() { int a, b; int sum, diff, product; printf("Enter two numbers: "); scanf("%d %d", &a, &b); sum = a + b; diff = a - b; product = a * b; printf("Sum: %d\n", sum); printf("Difference: %d\n", diff); printf("Product: %d\n", product); return 0; }
Enter two numbers: 12 4 Sum: 16 Difference: 8 Product: 48
#include <stdio.h> int main() { float a, b; printf("Enter two numbers: "); scanf("%f %f", &a, &b); printf("%.2f + %.2f = %.2f\n", a, b, a + b); printf("%.2f - %.2f = %.2f\n", a, b, a - b); printf("%.2f * %.2f = %.2f\n", a, b, a * b); if (b != 0) printf("%.2f / %.2f = %.2f\n", a, b, a / b); else printf("Cannot divide by zero!\n"); return 0; }
Enter two numbers: 9 2 9.00 + 2.00 = 11.00 9.00 - 2.00 = 7.00 9.00 * 2.00 = 18.00 9.00 / 2.00 = 4.50
#include <stdio.h> int main() { int a = 9, b = 2; printf("Int division: 9 / 2 = %d\n", a / b); // 4 printf("Float division: 9 / 2 = %.2f\n", (float)a / b); // 4.50 printf("Float literal: 9 / 2 = %.2f\n", 9.0 / 2); // 4.50 return 0; }
Int division: 9 / 2 = 4 ← decimal dropped! Float division: 9 / 2 = 4.50 ← (float) cast fixes it Float literal: 9 / 2 = 4.50 ← 9.0 forces float math
9 / 2 = 4 not 4.5. 7 / 4 = 1 not 1.75. The decimal part is simply dropped. To get the real decimal result, cast one operand to float: (float)a / b
P8 — Modulus Operator %
The modulus operator % gives the remainder after integer division. It only works with integers — not float or double. It is one of the most useful operators in programming.
10 % 3 = 1because 10 = 3×3 + 115 % 5 = 0because 15 = 5×3 + 0 (divisible exactly)7 % 2 = 1because 7 = 2×3 + 1 (odd number test)
#include <stdio.h> int main() { int dividend, divisor, remainder; printf("Enter two numbers: "); scanf("%d %d", ÷nd, &divisor); remainder = dividend % divisor; printf("Remainder of %d / %d = %d\n", dividend, divisor, remainder); return 0; }
Enter two numbers: 17 5 Remainder of 17 / 5 = 2
#include <stdio.h> int main() { int n; printf("Enter a number: "); scanf("%d", &n); // n % 2 gives 0 if even, 1 if odd if (n % 2 == 0) printf("%d is EVEN\n", n); else printf("%d is ODD\n", n); return 0; }
Enter a number: 14 14 is EVEN Enter a number: 7 7 is ODD
#include <stdio.h> int main() { int n; printf("Enter a number: "); scanf("%d", &n); if (n % 3 == 0 && n % 5 == 0) printf("Divisible by both 3 and 5\n"); else if (n % 3 == 0) printf("Divisible by 3 only\n"); else if (n % 5 == 0) printf("Divisible by 5 only\n"); else printf("Not divisible by 3 or 5\n"); return 0; }
#include <stdio.h> int main() { int n = 4567; printf("Number: %d\n", n); printf("Last digit: %d\n", n % 10); // 7 printf("Remove last: %d\n", n / 10); // 456 printf("Second digit: %d\n", (n / 10) % 10); // 6 return 0; }
Number: 4567 Last digit: 7 Remove last: 456 Second digit: 6
n % 2 == 0 → even/odd checkn % 10 → extract last digitn / 10 → remove last digitn % 3 == 0 → divisibility checkcount % 5 == 0 → do something every 5th iteration
Bonus — Putting It All Together
#include <stdio.h> int main() { float price, quantity, subtotal, tax, total; printf("Enter item price: "); scanf("%f", &price); printf("Enter quantity: "); scanf("%f", &quantity); subtotal = price * quantity; tax = subtotal * 0.18; // 18% GST total = subtotal + tax; printf("\n--- BILL ---\n"); printf("Subtotal: Rs %.2f\n", subtotal); printf("GST 18%%: Rs %.2f\n", tax); printf("Total: Rs %.2f\n", total); return 0; }
Enter item price: 250 Enter quantity: 3 --- BILL --- Subtotal: Rs 750.00 GST 18%: Rs 135.00 Total: Rs 885.00
#include <stdio.h> int main() { float weight, height, bmi; printf("Enter weight (kg): "); scanf("%f", &weight); printf("Enter height (m): "); scanf("%f", &height); bmi = weight / (height * height); // BMI formula printf("Your BMI: %.1f\n", bmi); if (bmi < 18.5) printf("Category: Underweight\n"); else if (bmi < 25.0) printf("Category: Normal\n"); else if (bmi < 30.0) printf("Category: Overweight\n"); else printf("Category: Obese\n"); return 0; }
Enter weight (kg): 70 Enter height (m): 1.75 Your BMI: 22.9 Category: Normal
Quick Quiz
Why do we write &n inside scanf("%d", &n)?
What is the result of 9 / 2 when both are int?
What does 17 % 5 equal?
Which is the correct way to read a float with scanf?
How do you check if a number n is even using modulus?
Lesson Checklist
- I know the format specifier for int (%d), float (%f), char (%c), string (%s)
- I understand why & is needed in scanf for variables
- I know char arrays do NOT need & in scanf
- I can read multiple inputs in one scanf call
- I understand all 5 arithmetic operators: + - * / %
- I know integer division truncates — 9/2 = 4 not 4.5
- I can use (float) cast to get decimal division
- I understand modulus % gives the remainder
- I can use % to check even/odd and divisibility
- I completed the quiz