How to Get User Input in C

Jinku Hu Mar 12, 2025 C C IO
  1. Using scanf for User Input
  2. Use scanf Parse User Input Based on the Given Formatting
  3. Using fgets for User Input
  4. Using gets (Not Recommended)
  5. Conclusion
  6. FAQ
How to Get User Input in C

Getting user input is a fundamental aspect of programming, especially in C. Whether you’re creating a simple console application or a more complex software solution, understanding how to gather input from users is crucial.

This article will guide you through the various methods available in C for obtaining text input from users. We will explore functions like scanf, gets, and fgets, providing clear examples and explanations for each. By the end of this article, you will have a solid grasp of how to effectively gather user input, enhancing your C programming skills.

Using scanf for User Input

One of the most common ways to get user input in C is through the scanf function. This function allows you to read formatted input from the standard input stream, which is typically the keyboard. The basic syntax of scanf is straightforward, and it can handle various data types, including strings.

Here’s a simple example of using scanf to get a string input from the user:

#include <stdio.h>

int main() {
    char name[50];
    printf("Enter your name: ");
    scanf("%s", name);
    printf("Hello, %s!\n", name);
    return 0;
}

Output:

Enter your name: John
Hello, John!

In this example, we declare a character array name to hold the user’s input. The scanf function reads a string from the user and stores it in the name variable. Note that scanf stops reading input at the first whitespace, which means it won’t capture names with spaces. This limitation is something to keep in mind when using scanf for string inputs.

Use scanf Parse User Input Based on the Given Formatting

Another useful feature of the scanf function is to parse the user input based on the given format. * character is used in the format string to discard the matching the characters by following conversion specifier.

The next code sample demonstrates when the scanf parses text inputs consisting of mandatory : symbol and only store characters after the given symbol to the end of the line. This option might be useful for scanning the fixed format texts where certain characters are present at delimiter positions.

#include <stdio.h>
#include <stdlib.h>

int main(void) {
  char str1[1000];

  printf("Input the text: ");
  scanf("%*[^:]%*c%[^\n]", str1);

  printf("'%s'\n", str1);
  exit(EXIT_SUCCESS);
}

Output:

Input the text: temporary value of var3: 324 gel
' 324 gel'

Using fgets for User Input

Another method for obtaining user input in C is the fgets function. Unlike scanf, fgets reads a whole line of text, including spaces, making it a more versatile option for capturing user input. The syntax for fgets is also quite simple.

Here’s how you can use fgets to read a string input:

#include <stdio.h>

int main() {
    char name[50];
    printf("Enter your full name: ");
    fgets(name, sizeof(name), stdin);
    printf("Hello, %s", name);
    return 0;
}

Output:

Enter your full name: John Doe
Hello, John Doe

In this example, fgets reads a line from the standard input and stores it in the name variable. The function takes three arguments: the string to store the input, the size of the string, and the input stream. Unlike scanf, fgets can handle spaces, allowing users to input their full names easily. However, it also includes the newline character in the input, which you may need to handle depending on your application’s requirements.

While you might come across the gets function in older C code, it’s important to note that it is not recommended for use. The function is unsafe because it does not perform bounds checking, which can lead to buffer overflows. However, for educational purposes, here’s how it works:

#include <stdio.h>

int main() {
    char name[50];
    printf("Enter your name: ");
    gets(name);
    printf("Hello, %s!\n", name);
    return 0;
}

Output:

Enter your name: John
Hello, John!

In this example, gets reads a string from the user without checking the length of the input. If the user enters more characters than the allocated size of name, it can cause a buffer overflow, leading to undefined behavior. Because of these risks, it is advisable to use fgets or scanf instead.

Conclusion

In this article, we explored various methods for obtaining user input in C, focusing on scanf, fgets, and the deprecated gets. Each method has its advantages and limitations, making it essential to choose the right one based on your application’s needs. For most scenarios, fgets is the preferred choice due to its ability to handle spaces and its safety compared to gets. Understanding these methods will not only enhance your programming skills but also make your applications more robust and user-friendly.

FAQ

  1. What is the difference between scanf and fgets?
    scanf reads input until a whitespace is encountered, while fgets reads an entire line, including spaces.

  2. Why is gets considered unsafe?
    gets does not perform bounds checking, which can lead to buffer overflows and undefined behavior.

  3. Can scanf read strings with spaces?
    No, scanf stops reading input at the first whitespace, so it cannot capture strings with spaces.

  4. What should I use instead of gets?
    You should use fgets or scanf with appropriate format specifiers to safely read user input.

  5. How can I remove the newline character from input captured by fgets?
    You can replace the newline character at the end of the string with a null terminator to remove it.

Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook

Related Article - C IO