devnotes.

printf and scanf Basics

printf writes formatted output, and scanf reads formatted input — both come from <stdio.h> and rely on a format string describing the expected types.

#include <stdio.h>

int main(void) {
    int age;
    printf("Enter your age: ");
    scanf("%d", &age);              // note the & — scanf needs the variable's address
    printf("You are %d years old.\n", age);
    return 0;
}

printf's format string uses format specifiers — %d for int, %f for float/double, %c for char, %s for a string — each matched, in order, with an extra argument after the string.

scanf requires the address of each variable it fills in, using the & (address-of) operator, so it can write the value directly into that memory location — this is one of the first places beginners hit a bug, since forgetting & compiles (with a warning) but crashes or corrupts memory at runtime rather than failing cleanly. Strings are the one common exception: an array name like name already decays to an address, so scanf("%s", name) (without &) is correct.

printf and scanf Basics in C | DevNotes