C Command Line Arguments - Tricky MCQ
Tricky Questions on Command Line Arguments
What is the correct signature of main() for command line arguments?
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.
What does argc represent?
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.
What is argv[0]?
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.
What is envp (third parameter to main)?
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.
Who allocates memory for argv strings?
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.
What happens with empty string argument ""?
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.
What is getopt() function?
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.
What is optind in getopt()?
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.
What is the difference between option and argument?
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 -).
How to handle spaces in arguments?
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.
What about wildcards (*, ?) in arguments?
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.
What is conventional argument ordering?
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.
How to pass binary data as argument?
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.
What is maximum argument length?
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.
What is alternative to getopt()?
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.