What is the purpose of command-line arguments in C?
a) To pass arguments to a program at runtime
b) To compile a program
c) To declare global variables
d) To manage memory allocation
Answer:
a) To pass arguments to a program at runtime
Explanation:
Command-line arguments in C allow users to pass information to a program at the time of its execution. These arguments are passed to the main()
function as parameters: argc
(argument count) and argv
(argument vector). argc
indicates the number of arguments passed, and argv
is an array of strings representing the arguments. This mechanism is useful for creating flexible programs that can operate differently based on the input provided by the user at runtime.
Understanding command-line arguments is crucial for developing programs that require user input or need to interact with the operating system’s command line.