โœ๏ธ C Basics โ€“ Syntax, Input, Tokens
Estimated reading: 4 minutes 269 views

C Input (scanf) โ€“ Reading User Input in C

Introduction โ€“ What Is scanf() in C?
In C programming, scanf() is a standard input function used to read data from the user via the keyboard. It accepts input values and stores them in variables using format specifiers and memory addresses. scanf() is a vital tool for interactive programs that need to collect user data.

In this guide, youโ€™ll learn:

  • How scanf() works and its syntax
  • The role of format specifiers and the & operator
  • How to read different data types (int, float, char, string)
  • Common errors and input-handling tips

Syntax of scanf()

scanf("format specifier", &variable);
  • "format specifier" tells scanf() what type of data to read.
  • &variable is the address-of operator used to store input into the variable.

Example:

int age;
scanf("%d", &age);

Format Specifiers in scanf()

SpecifierDescriptionExample Input
%dReads an integer25
%fReads a float3.14
%cReads a single characterA
%sReads a string (no spaces)John
%lfReads a double5.6789

Examples of Using scanf()

1. Reading an Integer

int age;
printf("Enter age: ");
scanf("%d", &age);

2. Reading a Float

float height;
printf("Enter height: ");
scanf("%f", &height);

3. Reading a Character

char grade;
printf("Enter grade: ");
scanf(" %c", &grade);  // Note the space before %c to skip whitespace

4. Reading a String

char name[30];
printf("Enter your name: ");
scanf("%s", name);

Note: %s cannot read spaces. Use fgets() for full-line input with spaces.


Common Mistakes in Using scanf()

MistakeIssueFix
Missing & before variableStores value in wrong place or causes crashUse &var (except with strings)
Forgetting space before %cMay read newline from previous inputUse " %c" instead of "%c"
Using %s for input with spacesStops reading at first spaceUse fgets() for multi-word strings
Mismatched format specifiersCompiler warning or unexpected behaviorEnsure %d, %f, etc., match variable

Best Practices

  • Always match the format specifier with the variable type.
  • Use fgets() for reading full lines or strings with spaces.
  • Initialize variables before use when possible.
  • Clear input buffer if needed when switching from scanf() to gets() or fgets().

Summary โ€“ Recap & Next Steps

The scanf() function is essential for taking user input in C programs. It supports multiple data types, uses format specifiers, and requires careful syntax to avoid errors.

Key Takeaways:

  • scanf() reads user input and stores it in variables.
  • You must use & to pass the address of the variable (except strings).
  • Format specifiers determine the type of input to read.
  • Input with spaces requires special handling.

Real-World Relevance:

Interactive programs in Cโ€”like calculators, form entries, and command-line toolsโ€”rely heavily on scanf() and related input functions to gather data from users.


Frequently Asked Questions (FAQ)

What is scanf() used for?

scanf() is used to read formatted input from the user through the keyboard. It stores the entered value in variables passed by reference.


Why do we use & in scanf()?

scanf() needs the memory address of the variable to store input. The & operator (address-of) passes the location of the variable. Strings are the exception, as the variable name already represents an address.


How can I read strings with spaces?

Use fgets() instead of scanf() for multi-word input:

char name[50];
fgets(name, 50, stdin);

This reads the full line including spaces.


What is the purpose of the space before %c in scanf()?

When reading a character using %c, adding a space before the specifier (" %c") tells scanf() to ignore any whitespace characters (like \n from previous input).


Can I read multiple inputs in one scanf() call?

Yes. You can read multiple values using multiple format specifiers:

int a, b;
scanf("%d %d", &a, &b);

What happens if the input type doesn’t match the specifier?

scanf() may behave unpredictably. For example, entering text instead of a number when using %d can cause errors or unexpected values.


Share Now :
Share

๐Ÿ“ฅ C Input (scanf)

Or Copy Link

CONTENTS
Scroll to Top