DEV Community

Yunus Emre Güzel
Yunus Emre Güzel

Posted on Originally published at blog.emreguzel.ca

Why Your C Program Crashes When You Forget & in scanf (and How to Fix It)

You write your first C program, run it, and the terminal window closes instantly. Or worse, it prints a random huge number and then crashes. You check the code three times and every line looks right—except one tiny character. Forgetting the & in scanf() is one of the most common mistakes beginners make in C, and it causes problems that look much bigger than they are.

scanf code comparison: missing ampersand crashes, adding ampersand works

The Problem: scanf Without the Ampersand

Here is a small program that asks for an age and prints it back:

#include <stdio.h>

int main(void) {
    int age;
    printf("Enter your age: ");
    scanf("%d", age);   /* missing & */
    printf("You are %d years old.\n", age);
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

On some computers this crashes right away. On others it stores the number in a random memory location and prints garbage, or it seems to work once and then fail later. The line scanf("%d", age); looks almost identical to the correct version, so the bug is easy to miss.

Why It Happens

scanf() needs to know where to put the value it reads. It needs the memory address of the variable, not the variable’s current value. The & operator gives that address. When you write &age, you are saying “store the input here.”

When you forget & and write scanf("%d", age);, scanf() treats whatever number is already inside age as a memory address. That address is probably not valid, so the program writes to memory it does not own. That is undefined behavior: the compiler is allowed to crash, print nonsense, or appear to work.

The Fix: Add the Ampersand

Change the scanf() call so it passes the address of age:

#include <stdio.h>

int main(void) {
    int age;
    printf("Enter your age: ");
    scanf("%d", &age);   /* correct */
    printf("You are %d years old.\n", age);
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Now scanf() knows exactly where age lives in memory, and the input is stored safely. The rule is simple: for int, float, double, char, and other single variables, always use & with scanf().

When You Do NOT Need the Ampersand

Strings and arrays are already memory addresses in C. When you read a string with scanf("%s", name) or a whole line with fgets(name, sizeof name, stdin), you write the variable name without &:

#include <stdio.h>

int main(void) {
    char name[50];
    printf("Enter your name: ");
    scanf("%s", name);   /* no & needed for arrays */
    printf("Hello, %s!\n", name);
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

The easy way to remember: scanf() needs an address. Single variables need & to produce one; arrays already are one.

Try It Yourself

Run this program first without the & on line 9, then with it. Notice the difference:

#include <stdio.h>

int main(void) {
    int length;
    int width;

    printf("Enter length: ");
    scanf("%d", &length);
    printf("Enter width: ");
    scanf("%d", &width);

    printf("Area = %d\n", length * width);
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

For the full picture on reading numbers, strings, and validating input safely, read my complete guide to user input in C. If you are just starting out, begin with the introduction to C programming.

Have you ever forgotten the & in scanf()? Tell me what happened in the comments and I’ll help you figure it out.


Originally published on my blog. I post new C, Python and AI-agent guides there regularly.

Top comments (0)