C Programming Tricky Questions
Tricky Questions on Command Line Arguments
What are the parameters of main() function for command line arguments?
main() can have two parameters: int argc (argument count) and char *argv[] (argument vector). argc is at least 1 (program name). argv[0] is program name, argv[1] to argv[argc-1] are arguments, argv[argc] is NULL.
What is the difference between argv[0] and actual program name?
argv[0] is the name used to invoke the program, which may differ from actual executable filename. It could be a symlink, alias, or different name used in shell. It's whatever was typed in command line.
Tricky Point: argv[0] can be empty string or NULL in some edge cases (executed without proper shell).
What is the third parameter envp in main() function?
Some implementations support third parameter: char *envp[] (environment pointer). Contains environment variables. However, it's non-standard. Portable code should use getenv() function instead.
How are command line arguments separated and handled by the shell?
Shell splits arguments by whitespace (space, tab). Arguments with spaces can be quoted with single or double quotes. Special characters ($, \, `, etc.) have special meaning and need escaping.
What happens when you pass arguments with spaces or special characters?
Shell handles quoting and escaping before passing to program. The program receives already-processed arguments. argv elements don't contain quotes or escape characters (unless escaped from shell).
What is the difference between int main() and int main(void)?
int main() means main can take any number of arguments (old style). int main(void) means main takes no arguments (explicit). For command line arguments, use int main(int argc, char *argv[]).
Can main() have different return types?
Standard allows int main() or int main(int argc, char *argv[]). Some compilers allow void main() as extension, but it's non-standard and limits portability. Always use int for standard compliance.
What is the purpose of returning value from main()?
Return value indicates program success/failure to calling process (usually shell). 0 means success, non-zero means error (convention). Exit status can be checked in shell with $? variable.
What is the difference between exit() and return from main()?
return from main() calls exit() implicitly. exit() can be called anywhere to terminate program immediately. Both flush buffers and call atexit() registered functions. exit() doesn't return to caller.
How are command line arguments stored in memory?
Arguments are stored in process memory space, typically above stack. They're null-terminated strings. argv array points to these strings. The entire block is read-only (string literals).
What is the maximum number of command line arguments?
Limited by system constraints: available memory, ARG_MAX constant (POSIX), or command line length limit. Typically thousands of arguments or total length limit (e.g., 128KB-2MB on Linux).
What happens when argc is 1?
argc = 1 means no arguments passed besides program name. argv[0] is program name, argv[1] is NULL. Program should handle this case (usually display usage message).
What is the difference between getopt() and manual argument parsing?
getopt() provides standardized option parsing (-x, --long). Manual parsing gives full control but more work. getopt() handles common patterns: short/long options, arguments, error messages.
What are environment variables and how to access them?
Environment variables are key-value pairs for program environment. Access with getenv("VARNAME"), setenv(), putenv(). Third main() parameter envp is non-standard alternative.
What is the -- argument separator?
Double dash (--) signals end of options. Arguments after -- are treated as filenames/arguments even if they start with dash. Useful for processing files named like "-f" or "--help".
Can command line arguments be modified?
Arguments are stored in read-only memory (string literals). Modifying them causes undefined behavior. If modification needed, copy to writable memory first.
Warning: Attempting to modify argv strings directly can cause segmentation fault or unexpected behavior.
What is the difference between short and long options?
Short options: single dash, single letter (-h, -v). Can be combined (-xvf). Long options: double dash, descriptive (--help, --version). getopt_long() handles both.
What is the significance of argv being NULL-terminated?
argv[argc] is guaranteed to be NULL. This allows iteration without knowing argc: while(argv[i] != NULL). Useful for functions that expect NULL-terminated array of strings.
What is the atexit() function and how does it relate to main()?
atexit() registers functions to be called when program exits (via return from main() or exit()). Registered functions are called in reverse order of registration (LIFO).
What is the difference between system() and exec() family for running programs?
system() runs shell command (creates shell process). exec() replaces current process with new program. exec() is more efficient but doesn't return. Both can pass command line arguments.
What is the purpose of standard streams (stdin, stdout, stderr) in command line programs?
stdin for input (keyboard or pipe), stdout for normal output, stderr for error messages. Can be redirected: ./program > output.txt 2> error.txt. Important for Unix philosophy.
What is the difference between shell expansion and program arguments?
Shell expands wildcards (*.txt), variables ($HOME), command substitution (`date`), before passing to program. Program receives expanded values. Understanding this prevents security issues.
What is the getopt() optstring and how does it work?
optstring specifies accepted options: "abc:" means -a, -b, -c (c requires argument). Colon after letter means required argument. Double colon means optional argument.
What is the difference between POSIX getopt() and GNU getopt_long()?
getopt() handles only short options. getopt_long() adds support for long options (--help). GNU extension also provides getopt_long_only() for mixed - and -- handling.
What is the significance of program returning different exit codes?
Exit codes signal success/failure to calling process. 0 = success. 1-125 = program-defined errors. 126 = command found but not executable. 127 = command not found. 128+ = terminated by signal.
Tricky Point: Shell scripts and other programs can check exit codes to make decisions, making proper exit codes crucial for automation.
Note: These tricky questions cover important concepts about command line arguments in C, including argument parsing, memory management, standard compliance, and best practices. Understanding command line interfaces is crucial for writing useful and user-friendly programs.