What is the purpose of the ‘scanf’ function in C?
a) To print output to the console
b) To allocate memory
c) To read formatted input from the console
d) To terminate a program
Answer:
c) To read formatted input from the console
Explanation:
The scanf()
function in C is used to read formatted input from the standard input (usually the keyboard). It reads the data according to the specified format and stores the input in the provided variables. For example, scanf("%d", &num);
reads an integer from the user and stores it in the variable num
.
Understanding how to use scanf()
is crucial for interacting with users and processing input in C programs.