scanf function in C Programming

scanf function

The scanf() function in C is used for reading formatted input from the standard input (keyboard) or from a specified file stream. It is part of the standard input-output library stdio.h.

Syntax of scanf function is
scanf ("format string", argument list);

Format string specifies the format of the input. It may contain format specifiers which represent the type and format of the input expected. The format string must be a text enclosed in double quotes. It contains the information for interpreting the entire data for connecting it into internal representation in memory.
Example: integer (%d) , float (%f) , character (%c) or string (%s).

The argument list contains a list of variables each preceded by the address list and separated by comma. The number of argument is not fixed; however corresponding to each argument there should be a format specifier. Inside the format string the number of argument should tally with the number of format specifier.

Example: if i is an integer and j is a floating point number, to input these two numbers we may use
scanf ("%d%f", &i, &j);

Example:

Format Specifiers:

  • %d: Integer
  • %f: Float
  • %lf: Double
  • %c: Character
  • %s: String (character array)
  • %x, %o, %u: Hexadecimal, octal, and unsigned decimal integers, respectively
  • %p: Pointer
  • %[^ ]: String of characters excluding spaces
  • %[^\n]: String of characters excluding newline

Usage Example:

#include <stdio.h>
int main() {
    int num;
    float f_num;

    // Reading integer and float from standard input
    printf("Enter an integer and a float: ");
    scanf("%d %f", &num, &f_num);
    printf("You entered: %d and %f\n", num, f_num);

    return 0;
}

Important Notes:

  • It skips any leading whitespace (spaces, tabs, newlines) before trying to match the input.
  • If the input does not match the format specifier, it stops reading and leaves the unmatched input in the input buffer.
  • It doesn't automatically add a null terminator to strings read with %s. You need to ensure proper termination manually.
  • It can be prone to buffer overflow if not used carefully, especially with string inputs. You can mitigate this risk by specifying a maximum field width with %Ns, where N is the maximum number of characters to read.

Common Pitfalls:

  • Forgetting to use the address-of operator (&) when passing variables as arguments to scanf().
  • Not handling the return value of scanf() to check for successful input.
  • Buffer overflow when reading strings. Always specify a maximum field width or use safer alternatives like fgets() for string input.

scanf() is powerful but can be tricky to use correctly. Carefully crafting the format string and handling return values and buffer sizes can help avoid common pitfalls.

Ready to get started?

Ready to embark on your journey into the world of C programming? Our comprehensive course provides the perfect starting point for learners of all levels. With engaging lessons, hands-on exercises, and expert guidance, you'll gain the skills and confidence needed to excel in this fundamental programming language. Let's dive in and unlock the endless possibilities of C programming together!