← The C Language
Lesson 4 of 77 · 10 XP
Input: scanf
Theory and problem statement
Machine translation, not yet reviewed by a native speaker.
scanf reads data by a template — it's the mirror image of printf:
int age;
scanf("%d", &age); // & means "the address of the variable": scanf must know WHERE to write
For now, take the & sign as a rule: scanf must be given the variable's address, not its value. We'll cover it in detail in the lesson about pointers.
Several values at once:
int a, b;
scanf("%d %d", &a, &b); // spaces and line breaks between the numbers are skipped
| reads | |
|---|---|
%d |
int |
%lf |
double (printf uses %f, but scanf uses %lf!) |
%c |
one character |
%s |
a word up to a space (into a char array) |
scanf returns how many values it managed to read. This matters for checking input; we'll come back to it later.
Task. Read two whole numbers and print their sum, difference and product — each on its own line.
Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.