Definition
📌 Input & Output
C uses two core functions for console I/O: printf() to print output, and scanf() to read input from the keyboard.
Syntax
syntax
printf("format string", values...);
scanf("format string", &variable);Example
c
#include <stdio.h>
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
printf("You are %d years old.\n", age);
return 0;
}Output
outputEnter your age: 20
You are 20 years old.
Important Points
%dis forint,%fforfloat,%cforchar,%sfor strings.scanfneeds&before variable names (except for arrays/strings, which decay to a pointer already).\ninsideprintfmoves the cursor to a new line.
Common Mistakes
- Forgetting
&before a variable name inscanf. - Using the wrong format specifier for the variable's type.
- Not checking
scanf's return value in real programs, which can hide input errors.
Interview Tip
🎯 Before your interview
Be ready to explain why
scanf needs a pointer (&variable): it has to modify the variable's actual memory, not a copy of its value.
