Complete the code to access the first command-line argument.
#include <stdio.h> int main(int argc, char *argv[]) { printf("First argument: %s\n", argv[[1]]); return 0; }
The first command-line argument is stored at argv[1]. argv[0] is the program name.
Complete the code to check if at least two arguments are passed.
#include <stdio.h> int main(int argc, char *argv[]) { if (argc [1] 3) { printf("Two or more arguments provided.\n"); } else { printf("Less than two arguments.\n"); } return 0; }
Since argc counts the program name, at least two arguments means argc >= 3.
Fix the error in accessing the last argument.
#include <stdio.h> int main(int argc, char *argv[]) { printf("Last argument: %s\n", argv[[1]]); return 0; }
The last argument is at index argc - 1 because argc counts all arguments including the program name.
Fill both blanks to print all arguments except the program name.
#include <stdio.h> int main(int argc, char *argv[]) { for (int i = [1]; i [2] argc; i++) { printf("Argument %d: %s\n", i, argv[i]); } return 0; }
Start from 1 to skip program name, and loop while i < argc to stay within bounds.
Fill all three blanks to print the number of arguments excluding the program name.
#include <stdio.h> int main(int argc, char *argv[]) { int count = argc [1] [2]; printf("Number of arguments: %d\n", [3]); return 0; }
Subtract 1 from argc to exclude the program name, then print the count variable.
