0
0
Cprogramming~10 mins

Why command line arguments are used - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to access the first command line argument.

C
#include <stdio.h>
int main(int argc, char *argv[]) {
    printf("First argument: %s\n", argv[[1]]);
    return 0;
}
Drag options to blanks, or click blank then click option'
A0
Bargc
C1
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Using argv[0] instead of argv[1] to get the first argument.
2fill in blank
medium

Complete the code to check if at least one command line argument is provided.

C
#include <stdio.h>
int main(int argc, char *argv[]) {
    if (argc [1] 1) {
        printf("Argument provided\n");
    } else {
        printf("No argument provided\n");
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
A<
B>
C==
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Checking if argc == 1 to find arguments, which is incorrect.
3fill in blank
hard

Fix the error in the code to print all command line arguments.

C
#include <stdio.h>
int main(int argc, char *argv[]) {
    for (int i = 1; i [1] argc; i++) {
        printf("Argument %d: %s\n", i, argv[i]);
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
A<
B>
C>=
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= in the loop condition causing out-of-bounds access.
4fill in blank
hard

Fill both blanks to create a program that prints the number of arguments excluding the program name.

C
#include <stdio.h>
int main(int argc, char *argv[]) {
    int count = argc [1] 1;
    printf("Number of arguments: %d\n", [2]);
    return 0;
}
Drag options to blanks, or click blank then click option'
A-
Bcount
Cargc
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Printing argc directly without subtracting 1.
5fill in blank
hard

Fill all three blanks to create a program that prints each argument and its length.

C
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[]) {
    for (int i = 1; i [1] argc; i++) {
        printf("Argument: %[2], Length: %d\n", argv[i], [3](argv[i]));
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
A<
Bs
Cstrlen
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= in the loop causing out-of-bounds errors.
Using wrong format specifier for string.