C Input (Using scanf)

In C programming, input functions are used to receive information from the user. The most commonly used function for input is scanf() from the stdio.h library. It allows reading formatted input from the standard input stream.

Key Topics

1. Using scanf()

The scanf() function is used to read data from the user, such as numbers or strings.

Example: Reading an Integer

#include <stdio.h>

int main() {
    int number;
    printf("Enter an integer: " );
    scanf("%d", &number);
    printf("You entered: %d
", number);
    return 0;
}

Output:

Enter an integer: 5
You entered: 5
                    

Code Explanation: The scanf() function reads an integer from the user and stores it in the variable number. The printf() function then displays the entered value.

2. Format Specifiers

Just like with printf(), format specifiers in scanf() indicate the type of data to be read. Below are some commonly used format specifiers:

Specifier Data Type Example
%dSigned integerint age; scanf("%d", &age);
%fFloating-point numberfloat price; scanf("%f", &price);
%cSingle characterchar grade; scanf("%c", &grade);
%sStringchar name[20]; scanf("%s", name);
%lfDouble precision floating-point numberdouble pi; scanf("%lf", &pi);

Example: Reading Multiple Inputs

#include <stdio.h>

int main() {
    int age;
    float height;
    char name[20];

    printf("Enter your name, age, and height: " );
    scanf("%s %d %f", name, &age, &height);
    printf("Name: %s, Age: %d, Height: %.1f
", name, age, height);
    return 0;
}

Output:

Enter your name, age, and height: Alice 25 5.6
Name: Alice, Age: 25, Height: 5.6
                    

Code Explanation: The scanf() function reads multiple inputs in a single statement. Each format specifier corresponds to a variable in the list.

3. Handling Multiple Inputs

When reading multiple inputs, ensure that the input matches the expected format. For example, use spaces to separate inputs when reading multiple variables.

Example: Reading Input with Validation

#include <stdio.h>

int main() {
    int num;
    printf("Enter an integer: " );
    if (scanf("%d", &num) == 1) {
        printf("You entered: %d
", num);
    } else {
        printf("Invalid input.
");
    }
    return 0;
}

Output:

Enter an integer: 10
You entered: 10
                    

Code Explanation: This program validates user input using the return value of scanf(). If the input does not match the expected format, an error message is displayed.

Best Practices

  • Always match format specifiers to the variable data type.
  • Validate user input to ensure reliability.
  • Use appropriate buffer sizes for strings to avoid overflow.

Don'ts

  • Don't forget to use the address-of operator (&) for non-array variables.
  • Don't mismatch format specifiers with variable types.
  • Don't skip validation for critical inputs.

Key Takeaways

  • The scanf() function is essential for receiving input in C programs.
  • Format specifiers enable reading data of various types.
  • Always validate inputs to ensure robustness.