beginner18 min

Variables, Types, and printf/scanf Format Specifiers

C's basic types, declaring variables, and the format specifiers printf and scanf both depend on.

What you'll learn

  • Declare variables of C's basic types: int, float, double, char
  • Match a value's type to the correct printf format specifier (%d, %f, %c, %s)
  • Explain what scanf's format specifiers and & operator are for when reading input

Explanation

C is statically typed: every variable is declared with an explicit type that never changes, e.g. int age = 30; or float gpa = 3.5f;. Unlike some languages, C has no type inference keyword -- you always write the type yourself. Common basic types include int (whole numbers), float/double (single/double-precision decimals), and char (a single byte, usually one character).

printf doesn't know the types of its arguments at compile time the way a more modern language's formatter might -- it relies entirely on format specifiers in the format string matching the actual argument types you pass. %d expects an int, %f expects a float/double (optionally with precision like %.2f for two decimal places), %c expects a single char, and %s expects a C string (more on those in a later lesson). Passing a mismatched type is a serious bug -- printf will read whatever bits happen to be there and interpret them as the wrong type, but many compilers will only warn, not error.

scanf is printf's counterpart for reading input, and it needs one more thing: the address of the variable to fill in, via the & operator, e.g. scanf("%d", &age); -- because scanf needs to write into your variable's memory location, not just receive a copy of its current value. Forgetting the & (except for strings, which behave specially, covered later) is one of the most common beginner C bugs, and it can crash the program or corrupt memory rather than just failing cleanly.

Guided lab

Predict: Variables and format specifiers

CNot executed
This lab does not run in your browser or on VisaSparkSchools's servers. Read the code, predict what it does, then reveal the real expected output.

Read this program and predict exactly what it prints.

#include <stdio.h>

int main(void) {
    char grade = 'A';
    int age = 30;
    float gpa = 3.5f;

    printf("Grade: %c, Age: %d, GPA: %.2f\n", grade, age, gpa);

    return 0;
}

Stuck? Get a hint.

Common mistakes

  • Using the wrong printf format specifier for a value's actual type, e.g. `%d` for a float -- this silently produces garbage output instead of a compile error in many compilers.
  • Forgetting the `&` (address-of) operator before a variable name in `scanf`, so scanf has no valid memory location to write into.
  • Expecting C to infer a variable's type from its initial value the way some other languages do -- C always requires an explicit type in the declaration.

Knowledge check

Knowledge check

1. Which printf format specifier matches an int argument?
2. Why does scanf typically need the `&` operator before a variable name?
3. Does C infer a variable's type automatically from its initial value?

Takeaway

Match printf's format specifier to your value's real type exactly, and remember scanf needs `&variable` because it writes directly into memory.

Summary

C variables are statically typed with explicit declarations; printf/scanf rely entirely on format specifiers matching the real argument types, and scanf additionally needs `&` to know where to write.

References

Your notes

Notes save automatically.

Finished this lesson?

Mark it complete to track your progress and schedule a future review.