Question: Instructions 1. Write a Program that can Print All Command-Line Arguments Okay, so your objective for this assignment is to write a C program, called


Instructions 1. Write a Program that can Print All Command-Line Arguments Okay, so your objective for this assignment is to write a C program, called arguments.c, that quite simply reads ALL the command-line arguments provided and prints them out. That's it. If NO command arguments are provided to your program then your program will output a message saying it needs at least one argument. Here's an example on how your program should function once you compile it: $ ./arguments The Big Brown Fox Jumped Over The Slow Lazy Dog (Enter] At which time your program will output: Program Name: "arguments" Arguments: 10 argv[1]: "The" argv[2]: "Big" argv[3]: "Brown" argv[4]: "Fox" argv[5]: "Jumped" argv[6]: "over" argv[7]: "The" argv[8]: "Slow" argv[9]: "Lazy" argv[10]: "Dog" Note a few things: The first argument (argv[0] ) is the name of your program. You should print that even though we're not 'counting it'. Note the number additional arguments in the second line is essentially argc minus 1. After that ANY additional arguments (which starts from argv[1] ), we print and count) those. All arguments are printed in double quotes. That way, if I add spaces to the arguments I can tell they're there. You can print double quotes within a printf() statement by using " to escape it. For example, printf( "Johnny said "Hello!\" "). . Your solution will require that you can print ANY number of arguments, so you will require that you use a loop, such as a for() loop. Here's another example, where we now use double-quotes so that it is treated as a SINGLE argument passed to your program: $ ./arguments "The Big Brown Fox Jumped over the slow Lazy Dog" [Enter] At which your program should print: Note a few things: The first argument ( argv[0] ) is the name of your program. You should print that even though we're not 'counting it'. Note the number additional arguments in the second line is essentially argc minus 1. After that ANY additional arguments (which starts from argv[1] ), we print (and count) those. All arguments are printed in double quotes. That way, if I add spaces to the arguments I can tell they're there. You can print double quotes within a printf() statement by using " to escape it. For example, printf( "Johnny said \"Hello!\" "). Your solution will require that you can print ANY number of arguments, so you will require that you use a loop, such as a for() loop. Here's another example, where we now use double-quotes so that it is treated as a SINGLE argument passed to your program: $ ./arguments "The Big Brown Fox Jumped Over The slow Lazy Dog" [Enter] At which your program should print: Program Name: "arguments" Arguments: 1 argv[1]: "The Big Brown Fox Jumped Over The Slow Lazy Dog" Another example, which passes arguments with extra spaces: $ ./arguments" Go Big Red At which point your program should print: Program Name: "arguments" Arguments: 3 argv[1]: GO argv[2]: Big argv[3] : " Red Again, adding the quotes when you print the arguments makes it CLEAR that there are extra spaces that were provided as arguments because we enclosed them in double-quotes when we passed them at the command line. Here's one last example. $ ./arguments Here no arguments were provided, so your program should print the following: ERROR: Must supply one or more arguments. Try again. That's it! Do you think you can do that? :)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
