C Academy · Lesson

scanf and Input

Read user input.

Lesson 2 of 413 steps

scanf and Input is a free C Academy lesson on CoddyKit. This is lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, and your progress syncs across the web and the CoddyKit app. The C Academy course includes 4 lessons in total.

What scanf Does

scanf reads formatted input typed by the user. It uses the same specifiers as printf.

#include <stdio.h>
int main(void) {
    int x = 0;
    /* scanf("%d", &x); */
    x = 5;
    printf("x = %d\n", x);
    return 0;
}

The Address-Of Operator

scanf needs the address of a variable so it can store the value there. That is why you write &x.

#include <stdio.h>
int main(void) {
    int x = 42;
    int *p = &x;
    printf("value via address = %d\n", *p);
    return 0;
}

Reading an Integer

To read an integer, pass %d and the address of an int.

#include <stdio.h>
int main(void) {
    int n;
    /* scanf("%d", &n); */
    n = 7;
    printf("You entered %d\n", n);
    return 0;
}

Reading a Float

To read a float use %f; for a double use %lf.

#include <stdio.h>
int main(void) {
    double d;
    /* scanf("%lf", &d); */
    d = 3.14;
    printf("d = %.2f\n", d);
    return 0;
}

Reading Multiple Values

One scanf can read several values separated by whitespace.

#include <stdio.h>
int main(void) {
    int a, b;
    /* scanf("%d %d", &a, &b); */
    a = 3; b = 4;
    printf("sum = %d\n", a + b);
    return 0;
}

Reading a Character

Use %c to read one character. Beware: it also reads spaces and newlines.

#include <stdio.h>
int main(void) {
    char c;
    /* scanf(" %c", &c); */
    c = 'X';
    printf("char = %c\n", c);
    return 0;
}

Reading a Word

%s reads a whitespace-delimited word into a char array. No & is needed because an array name is already an address.

#include <stdio.h>
int main(void) {
    char word[20];
    /* scanf("%s", word); */
    word[0] = 'H'; word[1] = 'i'; word[2] = 0;
    printf("word = %s\n", word);
    return 0;
}

scanf Return Value

scanf returns how many items it successfully read. Check it to detect bad input.

#include <stdio.h>
int main(void) {
    int n = 5;
    int read = 1; /* pretend scanf returned 1 */
    if (read == 1)
        printf("got %d\n", n);
    return 0;
}

Skipping Leading Spaces

Numeric specifiers skip leading whitespace automatically, but %c does not. A space before %c fixes this.

#include <stdio.h>
int main(void) {
    char c = 'Y';
    printf("Reading with \" %%c\" skips spaces\n");
    printf("c = %c\n", c);
    return 0;
}

Validating Input

Always verify the return value before trusting the data.

#include <stdio.h>
int main(void) {
    int value = 10;
    int ok = 1;
    if (ok)
        printf("valid: %d\n", value);
    else
        printf("invalid input\n");
    return 0;
}

Putting It Together

A typical program prompts, reads, then uses the value.

#include <stdio.h>
int main(void) {
    int age;
    printf("Enter age: ");
    /* scanf("%d", &age); */
    age = 20;
    printf("Next year you will be %d\n", age + 1);
    return 0;
}

Quick Check

Test your understanding of scanf.

Recap: scanf and Input

You learned how to read user input.

  • scanf uses printf-style specifiers
  • Pass &var so it knows where to store
  • Use %lf for double, %s for words
  • Check the return value to validate input
  • Put a space before %c to skip whitespace
Free to start

Learn C with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
39
Lessons
144

Frequently Asked Questions

Is the “scanf and Input” lesson free?

Yes — the full text of “scanf and Input” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO. The C Academy course includes 4 lessons in total.

What will I learn in “scanf and Input”?

Read user input. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C Academy?

No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

How long does the “scanf and Input” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C Academy lesson?

Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. printf Format Specifiers
  2. scanf and Input
  3. Width, Precision, Flags
  4. Common I/O Pitfalls
← Back to C Academy