scanf("%d", whatnumber);
is incorrect because the variable
whatnumber is passed directly instead of passing its
memory address.
The correct statement is:
scanf("%d", &whatnumber);
The & (address-of) operator tells scanf() where
the entered value should be stored.
Why Is It Wrong?
The scanf() function stores the user input into a variable.
Therefore, it requires the memory address of the variable,
not its current value.
| Statement | Correct? | Reason |
|---|---|---|
scanf("%d", whatnumber); |
No ❌ | Variable value is passed instead of its address. |
scanf("%d", &whatnumber); |
Yes ✔ | The address of the variable is passed. |
Incorrect Program
#include
int main()
{
int whatnumber;
scanf("%d", whatnumber);
printf("%d", whatnumber);
return 0;
}
Problem
-
scanf()receives an integer value instead of its address. - The program may produce a compilation warning or undefined behavior.
- The entered value cannot be stored correctly.
Correct Program
#include
int main()
{
int whatnumber;
scanf("%d", &whatnumber);
printf("%d", whatnumber);
return 0;
}
Sample Output
Enter a number: 25
25
How scanf() Works
User enters 25
│
▼
scanf("%d", &whatnumber)
│
▼
Address of whatnumber is passed
│
▼
25 is stored in memory
│
▼
printf() prints 25
Why Is the & Operator Needed?
| Expression | Meaning |
|---|---|
whatnumber |
The value stored in the variable. |
&whatnumber |
The memory address of the variable. |
scanf() |
Requires the variable's memory address to store user input. |
scanf() require the address-of operator
(&), such as int, float,
double, and char.
Exception: Character arrays (strings) do not use the
& operator because the array name itself represents the
address of its first element.
char name[20];
scanf("%s", name);
Common Data Types with scanf()
| Data Type | Format Specifier | Example |
|---|---|---|
| int | %d |
scanf("%d", &num); |
| float | %f |
scanf("%f", &price); |
| double | %lf |
scanf("%lf", &value); |
| char | %c |
scanf("%c", &ch); |
| String | %s |
scanf("%s", name); |
Common Mistakes
Avoid These Mistakes
- Forgetting to use the
&operator with numeric variables. - Using the wrong format specifier for the data type.
- Using
&with a string (character array). - Passing an uninitialized pointer to
scanf().
Best Practices
- Always pass the variable's address for numeric input.
- Use the correct format specifier for each data type.
- Read compiler warnings carefully.
- Test your program with different input values.
Prerequisites
Before Learning This Topic
- Basic understanding of variables and data types.
- Knowledge of the
scanf()function. - Basic understanding of memory addresses.
- Familiarity with the address-of (
&) operator.
Interview Questions
- Why does
scanf()require the address-of (&) operator? - What is wrong with
scanf("%d", number);? - Why is
¬ used with%sinscanf()? - What is the difference between
numberand&number? - Which data types require the
&operator inscanf()?
Key Takeaway
The statement scanf("%d", whatnumber); is incorrect because
scanf() needs the memory address of the
variable to store the user's input. The correct statement is
scanf("%d", &whatnumber);, where the
& operator provides the address of the variable.