In C, the scanf function is used to read input from the user through the standard input (keyboard). It allows the program to capture various data types like integers, floating-point numbers, characters, and strings.
Syntax:
scanf(“format_specifier”, &variable1, &variable2, …);
The format specifier indicates the type of data expected (e.g., %d for integers, %f for floats, %s for strings).
The & operator is used to pass the address of the variable to scanf.
Example:
#include <stdio.h>
int main() {
int age;
float height;
printf(“Enter age and height: “);
scanf(“%d %f”, &age, &height);
printf(“Age: %d, Height: %.2f\n”, age, height);
return 0;
}
In this example, scanf reads an integer and a float from the user. It’s important to ensure that the correct format specifiers are used to avoid input errors.