C Command Line Arguments - Tricky MCQ

Previous C Command Line Arguments Next

Tricky Questions on Command Line Arguments

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.

Previous C Command Line Arguments Next