Command Line Arguments in C Programming MCQ Questions and Answers
1. Which of the following parameters are used in the main function to handle command line arguments in C?
A) int argc, char *argv[]
B) int argn, char **arglist
C) int argc, char argv
D) void argc, char *argv[]
Answer: A
2. What does argc represent in a C program’s main function?
A) Number of characters in command line
B) Number of arguments passed including program name
C) Number of arguments excluding program name
D) Size of the argv array
Answer: B
3. If a program is executed as ./prog test 123, what will argv[2] contain?
A) ./prog
B) test
C) 123
D) prog
Answer: C
4. What is the data type of argv in command line arguments?
A) char argv[]
B) char **argv
C) int argv[]
D) char argv[10][10]
Answer: B
5. The last element of argv in a C program is always:
A) NULL
B) ‘\0’
C) argc
D) argv[argc-1]
Answer: A
6. Which statement about command line arguments is true?
A) They must always be integers.
B) They are read from standard input.
C) They are always passed as strings.
D) They can only be accessed using scanf.
Answer: C
7. If a program is run as ./a.out hello world, what is the value of argc?
A) 1
B) 2
C) 3
D) 0
Answer: C
8. To convert argv[1] (a string) to an integer, which function is most appropriate?
A) printf()
B) atoi()
C) itoa()
D) scanf()
Answer: B
9. What happens if no arguments are passed to a program that expects command line arguments?
A) argc becomes negative
B) argv becomes NULL
C) argc is 1
D) Program terminates with error
Answer: C
10. The base index of argv array in C is:
A) -1
B) 0
C) 1
D) Depends on compiler
Answer: B
11. The first argument argv[0] usually stores:
A) The first user-supplied argument
B) Program name or path
C) Compiler name
D) OS version
Answer: B
12. Which header file must be included to use command line arguments?
A) stdlib.h
B) stdio.h
C) No specific header required
D) string.h
Answer: C
13. If a program is executed as ./sum 4 5, what will argv[1] and argv[2] contain?
A) 4 and 5 as integers
B) ‘4’ and ‘5’ as characters
C) “4” and “5” as strings
D) Undefined values
Answer: C
14. The total number of command line arguments can be determined by:
A) sizeof(argv)
B) argc – 1
C) argv[argc]
D) strlen(argv)
Answer: B
15. In C, main can be defined to receive command line arguments using:
A) int main(int argc, char *argv[])
B) void main()
C) main(argc, argv)
D) char main(char argc, char argv)
Answer: A
16. Which library function can be used to convert argv[2] to a floating-point value?
A) atoi()
B) atof()
C) itoa()
D) strrev()
Answer: B
17. The expression argv[argc] evaluates to:
A) Last argument string
B) Undefined behavior
C) NULL
D) Address of last element
Answer: C
18. The type of argv can also be written as:
A) char *argv[]
B) char **argv
C) Both A and B
D) None
Answer: C
19. Which of the following will correctly print all command line arguments?
A) for(int i=0;i<argc;i++) printf(“%s”,argv[i]);
B) for(int i=0;i<=argc;i++) printf(“%s”,argv[i]);
C) for(int i=1;i<argc;i++) printf(“%c”,argv[i]);
D) for(int i=0;i<argc;i++) printf(“%d”,argv[i]);
Answer: A
20. The purpose of command line arguments in C is to:
A) Interact with kernel
B) Provide user input without modifying code
C) Debug program automatically
D) Create GUI programs
Answer: B
21. If a user runs ./prog “hello world”, how many arguments are counted?
A) 3
B) 2
C) 1
D) 0
Answer: B
22. Command line arguments in C are processed by:
A) Preprocessor
B) Compiler
C) Operating System
D) Linker
Answer: C
23. To check if any command line arguments were passed, you should test:
A) if(argc > 0)
B) if(argc == 0)
C) if(argc > 1)
D) if(argv == NULL)
Answer: C
24. Which of the following correctly shows passing arguments to main?
A) main(argc, argv)
B) int main(int argc, char *argv[])
C) main(int argv, char argc)
D) int main(void)
Answer: B
25. What is the output of printf(“%s”, argv[0]); if program name is run.exe?
A) Prints nothing
B) Prints run.exe
C) Prints first user argument
D) Error
Answer: B
26. Which of the following statements is false about command line arguments?
A) They are stored as strings.
B) They are separated by spaces on the command line.
C) They can be accessed inside any function.
D) They are passed to main() by the operating system.
Answer: C
27. In a program executed as ./a.out one two three, what is the value of argv[3][0]?
A) ‘o’
B) ‘t’
C) ‘h’
D) NULL
Answer: C
28. If argc is 4, then how many command line arguments are provided by the user (excluding the program name)?
A) 4
B) 3
C) 2
D) 5
Answer: B
29. Which of these main function definitions is most portable across compilers?
A) void main(int argc, char *argv[])
B) int main(int argc, char *argv[])
C) main()
D) char main()
Answer: B
30. Which of the following expressions gives the last argument in C?
A) argv[argc]
B) argv[argc-1]
C) argv[argc+1]
D) argv[0]
Answer: B
31. When command line arguments are used, they are stored in:
A) Stack memory
B) Heap memory
C) Read-only data segment
D) Environment variables
Answer: A
32. If a program is executed as ./calc 10 5, what is the value of argv[0][0]?
A) ‘c’
B) ‘1’
C) ‘5’
D) ‘.’
Answer: ‘.’
33. Which function allows conversion of string to integer safely with error checking?
A) atoi()
B) strtol()
C) itoa()
D) sprintf()
Answer: B
34. Which of the following can be used to count total characters in all command line arguments?
A) Using strlen() in a loop over argv
B) Using sizeof(argv)
C) Using argc directly
D) Using argv[argc]
Answer: A
35. What happens if argv is modified inside the program?
A) Compiler error
B) Undefined behavior if dereferenced
C) Changes visible to OS
D) Causes segmentation fault immediately
Answer: B
36. Which function prototype correctly accepts command line arguments?
A) int main(int argc, char **argv)
B) int main(char **argv, int argc)
C) void main(argv, argc)
D) main(int argc, argv)
Answer: A
37. The program ./a.out hello world has argc equal to:
A) 1
B) 2
C) 3
D) 4
Answer: C
38. Which of the following operations is invalid on argv?
A) Incrementing argv pointer
B) Accessing argv[i]
C) Modifying argv[i][0]
D) Assigning argv = NULL
Answer: D
39. What does argv point to?
A) Array of integers
B) Array of character pointers
C) Array of strings concatenated
D) Array of void pointers
Answer: B
40. In C, command line arguments are processed:
A) Before main() starts execution
B) After first function call
C) During linking
D) By the preprocessor
Answer: A
41. If a user passes no arguments, what is the minimum value of argc?
A) 0
B) 1
C) -1
D) Undefined
Answer: B
42. To print the number of arguments excluding program name:
A) printf(“%d”, argc);
B) printf(“%d”, argc-1);
C) printf(“%d”, argv);
D) printf(“%s”, argc);
Answer: B
43. Which operator is used to dereference argv?
A) *
B) &
C) ->
D) []
Answer: A
44. Which of the following declarations is equivalent to char *argv[]?
A) char argv[][]
B) char **argv
C) char *argv
D) char argv*[]
Answer: B
45. What does the function strtol() return if no valid conversion occurs?
A) 0
B) NULL
C) -1
D) argc
Answer: A
46. Which of these statements best defines argv?
A) Pointer to an array of characters
B) Array of strings passed from command line
C) List of environment variables
D) Memory location of executable file
Answer: B
47. Which is the correct way to print second command line argument?
A) printf(“%s”, argv[1]);
B) printf(“%s”, argv[2]);
C) printf(“%s”, argv[0]);
D) printf(“%s”, argv[argc]);
Answer: A
48. Command line arguments are separated by:
A) Semicolon
B) Space
C) Comma
D) Colon
Answer: B
49. If you run ./prog “A B” C, what is argc?
A) 2
B) 3
C) 4
D) 1
Answer: B
50. What is the output of printf(“%s”, argv[argc]);?
A) Prints last argument
B) Prints program name
C) Prints (null)
D) Causes compile error
Answer: C
51. Which of the following is not a valid signature for main() in C?
A) int main(void)
B) int main(int argc, char *argv[])
C) void main(int argc, char **argv)
D) int main()
Answer: C
52. Which function in C can be used to convert a string to a long integer?
A) atol()
B) itoa()
C) atof()
D) sprintf()
Answer: A
53. The C standard guarantees that argv[argc] will be:
A) Empty string
B) NULL pointer
C) Last argument repeated
D) Undefined
Answer: B
54. If argv[1] = “45”, then atoi(argv[1]) + 5 equals:
A) “455”
B) 50
C) 10
D) Error
Answer: B
55. The memory for command line arguments is allocated by:
A) Programmer
B) Operating System
C) Compiler
D) Linker
Answer: B
56. Which of the following statements about argv is true?
A) It is an array of int
B) It is a pointer to an array of char pointers
C) It must be initialized by the user
D) It stores binary values
Answer: B
57. When the program is executed, how does OS deliver command line arguments to the process?
A) Through environment variables
B) Through command interpreter
C) Via stack initialization
D) Through file descriptors
Answer: C
58. Which C statement prints all arguments except program name?
A) for(int i=1;i<argc;i++) printf(“%s “, argv[i]);
B) for(int i=0;i<=argc;i++) printf(“%s “, argv[i]);
C) for(int i=0;i<argc;i++) printf(“%s “, argv[i]);
D) printf(“%s”, argv[0]);
Answer: A
59. If command line arguments contain numbers, their type in C before conversion is:
A) int
B) char
C) string (char array)
D) float
Answer: C
60. If argc = 1, it implies:
A) Program has no arguments
B) One user argument
C) Syntax error
D) Environment variable missing
Answer: A
61. The function sscanf(argv[1], “%d”, &n); does what?
A) Reads input from user
B) Converts string argument to integer
C) Prints integer
D) Copies string
Answer: B
62. The total number of pointers stored in argv is:
A) argc
B) argc + 1
C) argc – 1
D) Undefined
Answer: B
63. What happens if the user provides fewer arguments than expected?
A) argv[i] beyond limit is NULL
B) Segmentation fault always
C) Compiler error
D) Undefined but continues
Answer: A
64. Which of the following is true about argc and argv?
A) argc counts arguments, argv holds them
B) argv counts arguments, argc holds them
C) Both store same data
D) Both are integers
Answer: A
65. The function strtod() can be used to:
A) Convert string to integer
B) Convert string to double
C) Convert string to octal
D) Convert string to hexadecimal
Answer: B
66. What will argv[argc-1] always contain?
A) Program name
B) Last user-supplied argument
C) NULL
D) Compiler name
Answer: B
67. Which of these functions can be used to process numeric arguments from command line?
A) atoi(), atof(), strtol()
B) scanf(), gets(), puts()
C) fprintf(), fscanf()
D) strlen(), strcmp()
Answer: A
68. Command line arguments are passed to main() by:
A) Standard library
B) Kernel or OS loader
C) Compiler
D) Linker
Answer: B
69. The expression *argv[1] gives:
A) Pointer to first argument string
B) First character of first argument string
C) Address of argv[1]
D) Number of arguments
Answer: B
70. When executed as ./app 1 2 3, what is argv[argc-1]?
A) “./app”
B) “3”
C) “2”
D) NULL
Answer: B
71. Which conversion function allows specifying base (binary, octal, etc.)?
A) atoi()
B) strtol()
C) atof()
D) sscanf()
Answer: B
72. What does the “argument vector” mean?
A) List of integers
B) Array of string pointers
C) Linked list
D) Function table
Answer: B
73. If user provides “12” “13” as input, what is sum using atoi(argv[1]) + atoi(argv[2])?
A) 25
B) 1213
C) 12
D) Error
Answer: A
74. What is printed if program is run without arguments and code says printf(“%s”, argv[1]);?
A) Program name
B) Empty string
C) Segmentation fault likely
D) NULL printed
Answer: C
75. Which one is true about argv pointer array?
A) It always ends with a NULL pointer.
B) It always has an extra integer.
C) It is a single pointer.
D) It has fixed length of 5.
Answer: A
76. Which of the following can replace char *argv[] safely?
A) char **argv
B) int **argv
C) void *argv[]
D) const char argv
Answer: A
77. What happens if argc is negative?
A) Program still runs
B) Undefined behavior (impossible case)
C) It means empty arguments
D) Zero arguments passed
Answer: B
78. Command line arguments are available to C programs:
A) Only on Windows
B) On all operating systems
C) Only on Linux
D) Only when compiled with GCC
Answer: B
79. To print all arguments line by line:
A) for(i=0;i<argc;i++) printf(“%s\n”,argv[i]);
B) for(i=0;i<=argc;i++) printf(“%s\n”,argv[i]);
C) while(argv[i]) printf(“%c\n”,argv[i]);
D) printf(“%s\n”,argv[argc]);
Answer: A
80. Which of the following is not a benefit of command line arguments?
A) Flexible input
B) Automation of tests
C) Compilation speed
D) No need to modify code for new inputs
Answer: C
81. What is the return type of main() when using command line arguments?
A) int
B) void
C) char
D) float
Answer: A
82. Which variable is automatically initialized by the OS when program starts?
A) argc
B) argv
C) Both A and B
D) Neither
Answer: C
83. If program executed as ./test one, what is argv[1][2]?
A) ‘e’
B) ‘o’
C) ‘n’
D) ‘\0’
Answer: D
84. The prototype of main() with environment variables included is:
A) int main(int argc, char *argv[])
B) int main(int argc, char *argv[], char *envp[])
C) void main()
D) int main(envp[])
Answer: B
85. If argv[2] is “NET”, what does argv[2][1] equal?
A) ‘N’
B) ‘E’
C) ‘T’
D) ‘\0’
Answer: B
86. In C, arguments after program name are separated by spaces unless:
A) They are enclosed in quotes
B) They contain numbers
C) They contain tabs
D) They start with –
Answer: A
87. argv is allocated:
A) Dynamically by program
B) Automatically by OS before program start
C) In heap using malloc()
D) In text segment
Answer: B
88. Which header is needed for using atoi() or atof()?
A) string.h
B) stdio.h
C) stdlib.h
D) math.h
Answer: C
89. The string length of program name can be found using:
A) strlen(argv[0])
B) sizeof(argv)
C) argc
D) strcat(argv[0])
Answer: A
90. If program name has path /usr/bin/a.out, what will argv[0] contain?
A) a.out
B) /usr/bin/a.out
C) bin/a.out
D) /usr
Answer: B
91. Which of these statements is incorrect?
A) argv can be modified
B) argc is an integer constant
C) argv[argc] is NULL
D) argv[0] stores program name
Answer: B
92. Which C function converts string to integer with base parameter support?
A) atoi()
B) strtol()
C) sscanf()
D) strcpy()
Answer: B
93. Command line arguments are processed:
A) During runtime before main() executes
B) During preprocessing
C) At compile-time
D) After program termination
Answer: A
94. Which of these lines will cause segmentation fault?
A) printf(“%s”, argv[argc]);
B) printf(“%s”, argv[argc-1]);
C) printf(“%s”, argv[0]);
D) printf(“%s”, argv[1]); when argc>1
Answer: A
95. The function sprintf() can be used to:
A) Convert numbers to strings for use as command line arguments
B) Read command line arguments from input
C) Execute command line directly
D) Count total arguments
Answer: A
96. Which of the following statements about argc is true?
A) It is always equal to zero.
B) It contains number of characters in the command line.
C) It contains total number of arguments including program name.
D) It is a pointer to argument array.
Answer: C
97. If a program is executed as ./demo “one two” three, what is stored in argv[1]?
A) “one”
B) “two”
C) “one two”
D) “three”
Answer: C
98. The function main(int argc, char **argv) returns an integer because:
A) The OS expects an exit status from program
B) It must print integer result
C) argc is integer
D) argv is pointer array
Answer: A
99. If you run ./sum 10 20 and the program prints Result = 1020, which of the following explains it?
A) atoi() was not used; strings were concatenated
B) Program used strtol()
C) Program multiplied numbers
D) Compiler optimization removed addition
Answer: A
100. Which of the following statements best summarizes command line arguments in C?
A) They provide runtime parameters as strings via main()
B) They are preprocessor directives
C) They are global constants defined by OS
D) They can only be used in header files
Answer: A
