What is the purpose of the scanf()
function in C?
a) To read formatted input from the user
b) To print output to the screen
c) To allocate memory
d) To close a file
Answer:
a) To read formatted input from the user
Explanation:
The scanf()
function in C is used to read formatted input from the user. It reads data from the standard input (typically the keyboard) and stores the input in the provided variables. The function uses format specifiers like %d
for integers, %f
for floats, and %s
for strings to determine the type of data being input. Proper use of scanf()
requires understanding the format specifiers and ensuring that the correct type of variable is used to store the input.
Understanding scanf()
is essential for interacting with users and receiving input in C programs, making it a fundamental part of many console applications.