C Command Line Arguments - Tricky MCQ
Tricky Questions on Command Line Arguments
Basic Level (15 Questions)
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.
Hard Level (15 Questions)
`int main(int argc, char *argv[])` argv[0] is:
argv[0] is implementation-defined name; may be bare name or path.
If program launched with no args, argc is:
argc is at least 1 counting argv[0]; argc==1 means no additional arguments.
`argv[argc]` is guaranteed to be:
Standard requires null pointer sentinel argv[argc]==NULL.
Environment variables accessed via:
getenv is standard; environ is common extension/POSIX.
`main(int argc, char **argv)` vs `char *argv[]`:
Array parameter decays to pointer; char**argv equivalent.
Parsing numbers: `strtol(argv[1], &end, 10)` detects invalid by:
No conversion if end points to start; overflow sets errno.
Wildcards expanded before main on Windows typical behavior vs Unix:
Argument expansion is shell/OS responsibility, not C standard.
Return `main` with `return 1;` to environment means:
Status meaning is host environment convention; EXIT_SUCCESS/FAILURE macros help portability.
`char **argv` const-correctness: `int main(int argc, char *argv[])` allows:
argv elements point to modifiable buffers typically, but string literals if used must not be modified.
Optional third parameter `char *envp[]` in main:
Third parameter for environment is not ISO C; was Unix/POSIX tradition.
Passing arguments with spaces in shell requires:
Shell tokenizes; quotes group words into one argument string.
`exit` from function other than main vs `return` from main:
return from main is equivalent to exit with the returned value; atexit runs.
Wide char command line `wmain` is:
wmain is not standard C; Windows wide entry point.
argc type is int because:
argc is int per standard; practical argument lists fit int range.
Program name with full path in argv[0] when executed via PATH:
What appears in argv[0] is implementation-defined.