printf writes formatted output, while scanf converts input text into typed values. Their similar-looking format strings hide an important difference: printf usually receives values, and scanf usually receives addresses where converted values should be stored. Understanding that difference prevents many beginner crashes.
Print values with matching conversions
Include stdio.h before using either function. In printf("Count: %d\n", count), ordinary characters are printed directly and %d converts the following int argument. Use %f for a double, %c for a character value, %s for a null-terminated character string, and %zu for size_t. Write %% to print a literal percent sign.
A format string is a contract with the argument list. The number and types of the arguments must match the conversions. A mismatched conversion can cause undefined behavior rather than a helpful automatic conversion. Compiler warnings catch many constant-format mistakes, so read them even if the run button remains available.
Read into existing objects
Declare a variable, display a prompt, then give scanf its address. For an int number, &number has the pointer type needed by %d. Leaving out the ampersand passes an integer value where an address is required. That is not a small formatting mistake: it can make the function write to an invalid location.
Always check the number of successful assignments returned by scanf. A single %d conversion should return 1. If it returns zero, the next input does not match an integer; if input ends before a conversion, it may return EOF. Do not use an uninitialized variable after a failed conversion.
#include <stdio.h>
int main(void) {
int number;
printf("Number: ");
if (scanf("%d", &number) != 1) {
fputs("Expected an integer.\n", stderr);
return 1;
}
printf("You entered %d\n", number);
return 0;
}Enter several responses naturally
In Circuit, click Run and type in the terminal when the program requests input. A prompt without a newline leaves the response on the same line. Enter submits the line; the next prompt appears after it, and keyboard focus stays in the terminal. Repeated scanf calls communicate with the same running program.
The interface echoes the submitted line for you. Your C program does not need to print the input again merely to show what was typed. Values can also be supplied on one line when the format accepts whitespace between them. A format with two integer conversions must return 2 before you use both variables.
Understand whitespace before guessing at fixes
Most numeric conversions skip leading whitespace. %c does not: it reads the next character, which may be the newline left after a previous number. scanf(" %c", &choice) adds leading whitespace to skip such separators before reading the character. That is useful for a menu choice, but inappropriate if a space itself is meaningful input.
Avoid ending an interactive numeric format with a newline, such as "%d\n". Whitespace in a scanf format means consume any amount of input whitespace; it may wait for a later non-whitespace character before the call finishes. This can look like a frozen terminal even though the function is following its format.
Strings and floating-point values need care
For char name[32], scanf("%31s", name) leaves room for the final null terminator. The array expression already supplies an address; a plain string conversion stops at whitespace and will not read a full name containing spaces. An unbounded %s can overrun the buffer. Prefer fgets for a whole line.
For scanf, %f expects float * and %lf expects double *. For printf, float arguments are promoted to double, so %f prints either ordinary float or double values. These are separate function contracts. Memorizing one format table and applying it blindly to both functions is a common source of mistakes.
Handle input as an operation that can fail
A failed numeric conversion leaves the offending text unread. Repeating the same scanf call in a loop without consuming or replacing that input repeats the failure. For a beginner exercise, report the problem and return a nonzero status. For a robust interactive parser, read a bounded line with fgets and convert it using strtol, checking the end pointer and range.
scanf is suitable for controlled exercises, but converting an arbitrarily large number into a smaller integer type is not a safe validation strategy. Choose explicit bounds, test invalid text, and decide what the program should do on end-of-input. In Circuit, stdin waits have a separate idle limit, so a pause to type does not consume the normal active execution allowance.