C Programs Tutorials | IT Developer
IT Developer

C Programming - C User Input



Share with a Friend

C Programming - C User Input

C User Input

In C programming, user input allows the program to take data from the user during runtime. The scanf() function is most commonly used for this purpose, but other methods, such as using file handling or command-line arguments, are also available for more advanced use cases.

  1. Using scanf() for User Input

The scanf() function reads input from the standard input (usually the keyboard). It is included in the <stdio.h> header file.

Syntax

scanf("format_specifier", &variable);
  • format_specifier: Specifies the type of data to be input (e.g., %d for integers, %f for floats).
  • &variable: Address-of operator (&) is used to store the input value in the variable.

Example: Reading User Input

#include <stdio.h>

int main() {

    int age;

    printf("Enter your age: ");

    scanf("%d", &age);  // Reads an integer input from the user

    printf("You are %d years old.\n", age);

    return 0;

}

  1. Reading Multiple Inputs

You can read multiple inputs using multiple format specifiers in a single scanf() call.

Example

#include <stdio.h>

int main() {

    int num1, num2;

    printf("Enter two integers: ");

    scanf("%d %d", &num1, &num2);  // Reads two integers

    printf("You entered %d and %d.\n", num1, num2);

    return 0;

}

  1. Input with Strings

For strings, the %s format specifier is used. Strings do not require the address-of operator (&) in scanf() because they are already pointers.

Example

#include <stdio.h>

int main() {

    char name[50];

    printf("Enter your name: ");

    scanf("%s", name);  // Reads a string (stops at whitespace)

    printf("Hello, %s!\n", name);

    return 0;

}

Note: scanf("%s") reads until the first whitespace. To read a full line of input, use fgets().

  1. Using fgets() for Safer Input

The fgets() function is preferred over scanf("%s") for reading strings because it allows spaces in the input and prevents buffer overflow.

Syntax

fgets(variable, size, stdin);
  • variable: Array to store the input string.
  • size: Maximum size of the input (including the null character).
  • stdin: Standard input (keyboard).

Example

#include <stdio.h>

int main() {

    char name[50];

    printf("Enter your full name: ");

    fgets(name, sizeof(name), stdin);  // Reads a full line of input

    printf("Hello, %s", name);

    return 0;

}

  1. Reading Character Input

The %c format specifier is used for single character input.

Example

#include <stdio.h>

int main() {

    char grade;

    printf("Enter your grade: ");

    scanf(" %c", &grade);  // Note the space before %c to consume any leftover newline

    printf("Your grade is %c.\n", grade);

    return 0;

}

  1. Handling Input Errors

When using scanf(), you can check the return value to ensure the input was successful.

Example

#include <stdio.h>

int main() {

    int num;

    printf("Enter an integer: ");

    if (scanf("%d", &num) == 1) {  // Checks if one integer was successfully read

        printf("You entered %d.\n", num);

    } else {

        printf("Invalid input.\n");

    }

    return 0;

}

  1. Limitations of scanf()
  • Does not handle whitespace well: Stops reading strings at the first space.
  • Buffer overflow risk: Can lead to undefined behavior if the input exceeds the allocated size.

To overcome these limitations, use fgets() or input validation.

Key Points

Method Best for Example

scanf()

Simple input of numbers or single words

scanf("%d", &num);

fgets()

Reading full lines or safer input

fgets(name, sizeof(name), stdin);

getchar()

Single character input

char c = getchar();

By using the appropriate method based on the type and complexity of input, you can effectively handle user input in your C programs.