0
0
Cprogramming~20 mins

Why command line arguments are used - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Command Line Arguments Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Purpose of Command Line Arguments

Why do programmers use command line arguments in C programs?

ATo create graphical user interfaces automatically.
BTo allow users to provide input values when starting the program without changing the code.
CTo store data permanently on the computer's hard drive.
DTo make the program run faster by skipping input prompts.
Attempts:
2 left
💡 Hint

Think about how programs can get information from users before running.

Predict Output
intermediate
2:00remaining
Output of Program Using Command Line Arguments

What is the output of this C program if run as ./prog Hello World?

C
#include <stdio.h>
int main(int argc, char *argv[]) {
    printf("%d\n", argc);
    printf("%s\n", argv[1]);
    return 0;
}
A
3
World
B
2
Hello
C
3
Hello
D
2
World
Attempts:
2 left
💡 Hint

Remember that argc counts the program name as the first argument.

🔧 Debug
advanced
2:00remaining
Identify the Error in Command Line Argument Usage

What error will this program produce when run with no arguments?

C
#include <stdio.h>
int main(int argc, char *argv[]) {
    printf("First argument: %s\n", argv[1]);
    return 0;
}
ASegmentation fault (accessing argv[1] when argc is 1)
BCompilation error due to missing semicolon
CPrints "First argument: " with no error
DRuntime error: division by zero
Attempts:
2 left
💡 Hint

Think about what happens if you try to access an argument that was not given.

📝 Syntax
advanced
2:00remaining
Correct Declaration of main for Command Line Arguments

Which of these is the correct way to declare the main function to use command line arguments in C?

Amain(int argc, char **argv)
Bvoid main(int argc, char *argv[])
Cint main(char *argv[], int argc)
Dint main(int argc, char *argv[])
Attempts:
2 left
💡 Hint

Remember the order and return type for the standard main function.

🚀 Application
expert
2:00remaining
Number of Arguments Passed to Program

If a program is run as ./app -f file.txt -v, what is the value of argc inside main?

A5
B3
C4
D2
Attempts:
2 left
💡 Hint

Count the program name and each separate word as one argument.