0
0
Cprogramming~10 mins

Accessing arguments - Interactive Code Practice

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'
A1
Bargc
C2
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using argv[0] which is the program name, not the first argument.
Using argc as an index which is invalid.
2fill in blank
medium

Complete the code to check if at least two arguments are passed.

C
#include <stdio.h>

int main(int argc, char *argv[]) {
    if (argc [1] 3) {
        printf("Two or more arguments provided.\n");
    } else {
        printf("Less than two arguments.\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 == 2 which means only one argument.
Using < instead of >= causing wrong condition.
3fill in blank
hard

Fix the error in accessing the last argument.

C
#include <stdio.h>

int main(int argc, char *argv[]) {
    printf("Last argument: %s\n", argv[[1]]);
    return 0;
}
Drag options to blanks, or click blank then click option'
Aargc + 1
Bargc - 1
Cargc
Dargc - 2
Attempts:
3 left
💡 Hint
Common Mistakes
Using argc as index which is out of bounds.
Using argc + 1 which is invalid index.
4fill in blank
hard

Fill both blanks to print all arguments except the program name.

C
#include <stdio.h>

int main(int argc, char *argv[]) {
    for (int i = [1]; i [2] argc; i++) {
        printf("Argument %d: %s\n", i, argv[i]);
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
A1
B0
C<
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Starting loop at 0 which prints program name.
Using <= which causes out of bounds access.
5fill in blank
hard

Fill all three blanks to print the number of arguments excluding the program name.

C
#include <stdio.h>

int main(int argc, char *argv[]) {
    int count = argc [1] [2];
    printf("Number of arguments: %d\n", [3]);
    return 0;
}
Drag options to blanks, or click blank then click option'
A-
B1
Ccount
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Adding 1 instead of subtracting.
Printing argc instead of count.