Bird
Raised Fist0
Cprogramming~10 mins

Why command line arguments are used - Test Your Understanding

Choose your learning style10 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.

Practice

(1/5)
1. Why are command line arguments used in a C program?
easy
A. To change the program's source code automatically
B. To make the program run faster
C. To allow users to provide input values when the program starts
D. To display graphics on the screen

Solution

  1. Step 1: Understand the purpose of command line arguments

    Command line arguments let users give input to the program when it starts, without changing the code.
  2. Step 2: Compare options with this purpose

    Only To allow users to provide input values when the program starts matches this purpose; others are unrelated to command line arguments.
  3. Final Answer:

    To allow users to provide input values when the program starts -> Option C
  4. Quick Check:

    Command line arguments = user input at start [OK]
Hint: Command line arguments pass input when program starts [OK]
Common Mistakes:
  • Thinking command line arguments speed up the program
  • Confusing command line arguments with code changes
  • Assuming command line arguments control graphics
2. Which of the following is the correct way to declare the main function to use command line arguments in C?
easy
A. int main(char argc, int argv)
B. void main()
C. int main()
D. int main(int argc, char *argv[])

Solution

  1. Step 1: Recall the standard main function signature for command line arguments

    The main function must accept two parameters: an integer argc and an array of strings argv.
  2. Step 2: Match the options with this signature

    Only int main(int argc, char *argv[]) matches the correct signature: int main(int argc, char *argv[]).
  3. Final Answer:

    int main(int argc, char *argv[]) -> Option D
  4. Quick Check:

    Main with argc and argv = correct signature [OK]
Hint: Main with (int argc, char *argv[]) is correct [OK]
Common Mistakes:
  • Using void main() which lacks arguments
  • Swapping types of argc and argv
  • Using int main() without parameters
3. What will be the output of this C program if run as ./program Hello World?
#include <stdio.h>
int main(int argc, char *argv[]) {
    printf("%d %s %s", argc, argv[1], argv[2]);
    return 0;
}
medium
A. 3 Hello World
B. 2 Hello World
C. 3 program Hello
D. Error: argv out of range

Solution

  1. Step 1: Count argc and identify argv values

    Running as './program Hello World' means argc = 3 (program name + 2 arguments). argv[0] = "./program", argv[1] = "Hello", argv[2] = "World".
  2. Step 2: Check printf output

    printf prints argc (3), argv[1] (Hello), argv[2] (World) separated by spaces.
  3. Final Answer:

    3 Hello World -> Option A
  4. Quick Check:

    argc=3, argv[1]=Hello, argv[2]=World [OK]
Hint: argc counts args including program name [OK]
Common Mistakes:
  • Assuming argc is 2 instead of 3
  • Confusing argv[0] with first argument
  • Expecting error accessing argv[2]
4. Identify the error in this C program that uses command line arguments:
#include <stdio.h>
int main(int argc, char *argv[]) {
    printf("%s", argv[argc]);
    return 0;
}
medium
A. Missing return type for main
B. Accessing argv[argc] causes out-of-bounds error
C. argc should be a char pointer
D. printf format specifier is incorrect

Solution

  1. Step 1: Understand argv indexing

    argv array indexes go from 0 to argc-1. Accessing argv[argc] is out of bounds and unsafe.
  2. Step 2: Check other options

    Main has return type int, argc is int, and printf uses %s correctly. So only Accessing argv[argc] causes out-of-bounds error is correct.
  3. Final Answer:

    Accessing argv[argc] causes out-of-bounds error -> Option B
  4. Quick Check:

    argv valid indexes: 0 to argc-1 [OK]
Hint: argv index max is argc-1, not argc [OK]
Common Mistakes:
  • Using argv[argc] instead of argv[argc-1]
  • Confusing argc type with argv
  • Ignoring array bounds in argv
5. You want to write a C program that prints the number of command line arguments provided (excluding the program name). Which code snippet correctly achieves this?
hard
A. printf("%d", argc - 1);
B. printf("%d", argc);
C. printf("%d", argv);
D. printf("%d", argc + 1);

Solution

  1. Step 1: Understand argc meaning

    argc counts the program name plus all arguments, so total arguments including program name.
  2. Step 2: Calculate number of arguments excluding program name

    Subtract 1 from argc to exclude the program name.
  3. Step 3: Check options

    Only printf("%d", argc - 1); prints argc - 1, which is correct.
  4. Final Answer:

    printf("%d", argc - 1); -> Option A
  5. Quick Check:

    Arguments excluding program name = argc - 1 [OK]
Hint: Subtract 1 from argc to exclude program name [OK]
Common Mistakes:
  • Printing argc directly including program name
  • Using argv as integer in printf
  • Adding 1 to argc instead of subtracting
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard