Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

scanf — formatted input

scanf reads stdin according to a format, storing results through pointers. Always check the call's return value.

Version: C23; repository compiler flag, production runtime not attested

Syntax

#include <stdio.h>
int scanf(const char * restrict format, ...);

scanf from <stdio.h> reads stdin. %d requires int *, %f requires float *, and %lf requires double *; pass, for example, &number.

The return value counts assignments. 0 can indicate a mismatch before any assignment, but it can also follow a successful conversion with assignment suppressed, as with %*d and input 42. EOF means input failure before the first conversion completes. Check the expected count.

Bound %s: %7s requires an array of at least 8 characters, including \0. Pass word, not &word. An incorrect type or an unrepresentable conversion result causes undefined behavior.

Examples

#include <stdio.h>

/* Example input: type hello and press Enter. */
int main(void)
{
    char word[8];
    if (scanf("%7s", word) != 1) {
        return 1;
    }
    puts(word);
    return 0;
}

Result

hello

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources