Skip to content
QuiznExam

Command Line Arguments in C

Test your knowledge of command line arguments in C, including argc, argv, program arguments, argument indexing, and string handling.

15
Questions
12 min
Time limit
Medium
Difficulty

No sign-up needed · Last updated:

Save your score

At least 8 characters.

Forgot password?

Related quizzes

const in C

15 questions · 12 min · Medium

All 15 questions and answers Spoilers: this shows the questions and answers. Try the quiz first! Open
  1. 1. Which parameters are commonly used in the main() function to receive command line arguments in C?

    • A. argc and argv
    • B. args and values
    • C. count and args
    • D. main and params
    Show answer

    A. argc and argv

    The main() function can receive command line arguments through the argc and argv parameters.

  2. 2. What does argc represent in a C program?

    • A. The number of command line arguments
    • B. The value of the first argument
    • C. The address of the arguments
    • D. The size of the program
    Show answer

    A. The number of command line arguments

    argc contains the count of command line arguments, including the program name.

  3. 3. What does argv represent?

    • A. An array containing command line argument strings
    • B. The number of arguments
    • C. The return value of main()
    • D. The program's memory size
    Show answer

    A. An array containing command line argument strings

    argv is an array of pointers to strings representing the command line arguments.

  4. 4. What is typically stored in argv[0]?

    • A. The first user-provided argument
    • B. The program name or invocation name
    • C. The value of argc
    • D. NULL in every case
    Show answer

    B. The program name or invocation name

    argv[0] conventionally points to the program name or the name used to invoke the program.

  5. 5. If a program is executed as ./program hello, what is the typical value of argc?

    • A. 0
    • B. 1
    • C. 2
    • D. 3
    Show answer

    C. 2

    The arguments are typically argv[0] = ./program and argv[1] = hello, so argc is 2.

  6. 6. Which declaration is commonly used for command line arguments in main()?

    • A. int main(int argc, char *argv[])
    • B. int main(char argc, int argv[])
    • C. void main(string argc, string argv)
    • D. int main(int argv, char argc[])
    Show answer

    A. int main(int argc, char *argv[])

    A common standard form is int main(int argc, char *argv[]), where argc counts arguments and argv points to their strings.

  7. 7. If a program is executed as ./program one two three, what is the value of argc?

    • A. 3
    • B. 4
    • C. 5
    • D. 2
    Show answer

    B. 4

    argc includes the program name, so there are four arguments: ./program, one, two, and three.

  8. 8. Which expression accesses the second command line argument supplied by the user?

    • A. argv[0]
    • B. argv[1]
    • C. argv[2]
    • D. argv[argc]
    Show answer

    B. argv[1]

    argv[0] is conventionally the program name, so the first user-provided argument is stored at argv[1].

  9. 9. What type of data does argv[i] represent?

    • A. An integer
    • B. A character string
    • C. A floating-point number
    • D. A structure
    Show answer

    B. A character string

    Each argv element points to a null-terminated character sequence representing an argument.

  10. 10. How can a command line argument such as "25" be converted to an integer?

    • A. Using atoi()
    • B. Using int()
    • C. Using convertInt()
    • D. Using parseInt()
    Show answer

    A. Using atoi()

    The atoi() function from the standard library can convert a numeric string to an int, although functions such as strtol() provide better error handling.

  11. 11. What does argc equal when a C program is run without any additional user-provided command line arguments?

    • A. 0
    • B. 1
    • C. -1
    • D. It is always undefined
    Show answer

    B. 1

    The program name is conventionally included in argc, so a normal invocation without additional arguments has argc equal to 1.

  12. 12. Which header file declares the atoi() function?

    • A. <stdio.h>
    • B. <stdlib.h>
    • C. <string.h>
    • D. <ctype.h>
    Show answer

    B. <stdlib.h>

    The atoi() function is declared in the standard library header <stdlib.h>.

  13. 13. Which loop condition can be used to process all command line arguments except the program name?

    • A. for (int i = 1; i < argc; i++)
    • B. for (int i = 0; i <= argc; i++)
    • C. for (int i = 1; i <= argc; i++)
    • D. for (int i = argc; i > 0; i++)
    Show answer

    A. for (int i = 1; i < argc; i++)

    Starting at index 1 skips argv[0], and using i < argc ensures only valid argument indexes are accessed.

  14. 14. What is typically found at argv[argc]?

    • A. The last command line argument
    • B. The program name
    • C. A null pointer
    • D. The value of argc
    Show answer

    C. A null pointer

    The argv array is required to have a null pointer after the last argument pointer, at argv[argc].

  15. 15. Which statement about command line arguments in C is correct?

    • A. Command line arguments are initially received as strings
    • B. argc contains the text of every argument
    • C. argv can contain only integers
    • D. argv[0] must always contain the full absolute path
    Show answer

    A. Command line arguments are initially received as strings

    Command line arguments are passed to the program as strings. If numeric values are required, they must be converted to an appropriate numeric type.