C Command Line Arguments - Tricky MCQ

Previous C Command Line Arguments Next

Tricky Questions on Command Line Arguments

Basic Level (15 Questions)

1

What is the correct signature of main() for command line arguments?

Correct Answer: D) Both A and B are correct

char *argv[] and char **argv are equivalent in function parameters. Both represent array of pointers to strings. argc is argument count (including program name). argv[argc] is guaranteed to be NULL pointer.

2

What does argc represent?

Correct Answer: D) All of the above

argc (argument count) is number of strings in argv array. argv[0] is program name (may be empty or modified by OS). argc is always ≥ 1. User arguments start at argv[1]. argv[argc] is NULL.

3

What is argv[0]?

Correct Answer: D) All of the above

argv[0] is program name as invoked. Could be relative/absolute path, just name, or empty. Not reliable for finding executable location. On POSIX, /proc/self/exe or similar provides actual path. Should treat as opaque string.

4

What is envp (third parameter to main)?

Correct Answer: D) All of the above

int main(int argc, char *argv[], char *envp[]) is common extension. Provides environment variables as "NAME=VALUE" strings. Not standard C - use extern char **environ (POSIX) or getenv() for portability. Last entry is NULL.

5

Who allocates memory for argv strings?

Correct Answer: D) All of the above

OS allocates argv strings before main() starts. Implementation-defined whether writable. Portable code should treat as read-only. Copy with strdup() or similar if modification needed. Freed automatically on program exit.

6

What happens with empty string argument ""?

Correct Answer: D) All of the above

Empty string "" is valid argument. argv[i] points to string with single null character. Shell quoting affects passing: prog "" vs prog ''. Need to check strlen(argv[i]) == 0. Different from missing argument.

7

What is getopt() function?

Correct Answer: D) All of the above

getopt() parses command line options following POSIX conventions. Handles -a, -b value, -abc (multiple flags). getopt_long() adds GNU-style --long-options. Not part of C standard but widely available. Updates global variable optind.

8

What is optind in getopt()?

Correct Answer: D) All of the above

optind tracks parsing progress. Initialize to 1 (skip program name). getopt() increments as options processed. After getopt() returns -1, optind points to first non-option argument. Can be reset for re-parsing.

9

What is the difference between option and argument?

Correct Answer: D) All of the above

Options (flags/switches) start with dash. Arguments (operands) are positional parameters. Conventional: options before arguments. Double dash "--" indicates end of options (useful for filenames starting with -).

10

How to handle spaces in arguments?

Correct Answer: D) All of the above

Shell performs word splitting before calling program. Quotes preserve spaces as single argument: prog "hello world" results in argc=2, argv[1]="hello world". Escaping also works: prog hello\ world. Program sees already-processed arguments.

11

What about wildcards (*, ?) in arguments?

Correct Answer: D) All of the above

Shell performs glob expansion. prog *.txt expands to matching files. If no matches, some shells pass literal "*.txt". Use quotes to prevent expansion: prog "*.txt". Program must handle either case.

12

What is conventional argument ordering?

Correct Answer: D) All of the above

POSIX convention: options precede operands. getopt() stops at first non-option. GNU getopt() with reorder option permutes arguments. Some programs allow intermixing (like GCC). Consistency within program is key.

13

How to pass binary data as argument?

Correct Answer: D) All of the above

Command line arguments are null-terminated strings. Null bytes can't be included. For binary data, use encoding (hex: 48656C6C6F, base64: SGVsbG8=). Or read from file/stdin. Shell/OS may impose length limits.

14

What is maximum argument length?

Correct Answer: D) All of the above

ARG_MAX is maximum total size for arguments + environment. Typical values: 128KB-2MB. Per-argument limits also exist. Exceeding causes E2BIG error. Use xargs for many/long arguments. sysconf(_SC_ARG_MAX) gets limit at runtime.

15

What is alternative to getopt()?

Correct Answer: D) All of the above

Manual parsing works for simple cases. argp (GNU) provides help generation. docopt uses usage string to generate parser. Platform-specific: Windows GetCommandLine(). Choose based on needs: portability, features, complexity.

Hard Level (15 Questions)

16

`int main(int argc, char *argv[])` argv[0] is:

Correct Answer: A) Program name (implementation-defined format)

argv[0] is implementation-defined name; may be bare name or path.

17

If program launched with no args, argc is:

Correct Answer: B) 1

argc is at least 1 counting argv[0]; argc==1 means no additional arguments.

18

`argv[argc]` is guaranteed to be:

Correct Answer: A) NULL

Standard requires null pointer sentinel argv[argc]==NULL.

19

Environment variables accessed via:

Correct Answer: A) extern char **environ; (POSIX) or getenv

getenv is standard; environ is common extension/POSIX.

20

`main(int argc, char **argv)` vs `char *argv[]`:

Correct Answer: A) Equivalent

Array parameter decays to pointer; char**argv equivalent.

21

Parsing numbers: `strtol(argv[1], &end, 10)` detects invalid by:

Correct Answer: A) end == argv[1] or errno ERANGE

No conversion if end points to start; overflow sets errno.

22

Wildcards expanded before main on Windows typical behavior vs Unix:

Correct Answer: A) Shell expands on Unix; program may see literals on Windows depending on link

Argument expansion is shell/OS responsibility, not C standard.

23

Return `main` with `return 1;` to environment means:

Correct Answer: A) Implementation-defined exit status

Status meaning is host environment convention; EXIT_SUCCESS/FAILURE macros help portability.

24

`char **argv` const-correctness: `int main(int argc, char *argv[])` allows:

Correct Answer: C) Both modification concerns

argv elements point to modifiable buffers typically, but string literals if used must not be modified.

25

Optional third parameter `char *envp[]` in main:

Correct Answer: A) Not in standard C; POSIX historical

Third parameter for environment is not ISO C; was Unix/POSIX tradition.

26

Passing arguments with spaces in shell requires:

Correct Answer: A) Quoting on Unix shell

Shell tokenizes; quotes group words into one argument string.

27

`exit` from function other than main vs `return` from main:

Correct Answer: B) Identical always

return from main is equivalent to exit with the returned value; atexit runs.

28

Wide char command line `wmain` is:

Correct Answer: A) Microsoft extension / Windows API

wmain is not standard C; Windows wide entry point.

29

argc type is int because:

Correct Answer: A) Historical limit on argument count fits int

argc is int per standard; practical argument lists fit int range.

30

Program name with full path in argv[0] when executed via PATH:

Correct Answer: A) May be path used to invoke or name only

What appears in argv[0] is implementation-defined.

Previous C Command Line Arguments Next