Join Dan Gookin for an in-depth discussion in this video Working with arguments in the main() function, part of Advanced C Programming.
- [Voiceover] No matter how graphical the operating system, internally, programs are launched and services started by using a command line. Yes, just like in text-based operating systems. First comes a command name, then a series of options, switches or other information, and this text is made available to every C program as the main function's arguments. Your program can access those arguments, evaluate their contents, and do all sorts of wonderous C program-y things with them. Open Exercise File 02-01, arguments1.
Here, you see the main function's arguments specified at line 3. You don't need to specify these arguments if the code doesn't use them. You can leave the parentheses blank or insert the word "void". Otherwise, both arguments must be specified. Argc is the argument count, an integer. It represents the number of items typed at the command prompt. Argv is an array of strings with each string representing an item typed at the command prompt. Now what you see here is array notation for an array of strings.
It can also be written by using pointer notation. Most people just use the asterisk argv bracket, bracket, or array notation. You can save and build and run this code. One command line item is always present. That's true for all programs. That single item is the name of the program being run. You can view that name as it's known to the operating system by adding another printf statement. The first element of the argv array, element 0, is the program name.
Save this change, and then, build and run the code. And there you see the program name. Actually, it's a full path name in this case to where the program lives on the storage system. This code as-is displays the number of command line arguments. But in code blocks, you need to specify those arguments. For that you need to create a project. Otherwise, the arguments can't be said in this IDE. So, create a new project. Choose File, New, Project. Create a console application, using the C language.
For the project title, name it Chapter0201. Be sure that the folder is properly specified. Click here to choose a location for the folder. Set a release version only. You do not need to create a de-bug version, so you can remove that checkmark. Click finish. The source code for the project is kept here. Double click to view it. It's a predefined skeleton, but we want to replace that with the contents of the exercise file. So, go back to the exercise file, select everything, copy, and paste to replace.
Save the project. Now you can set the command line arguments. Choose Project, Set Programs arguments. Type the arguments into the Select Target dialog box. I'll type, this, that, and "the other", in double quotes. Click OK. Now, build and run the program. This time, it reads, "five arguments are present". That's because the text, "the other", is in double quotes and is read as a single argument. To display the arguments, you can add a loop to the code.
Add an integer variable X. Add a for loop after the last print statement. Loop from 0 to the count stored in argc. And add a printf statement to display each argument. Save these changes. Build and run the code. If you've kept the same command line arguments as before, you'll see output similar to what's shown here. The first argument is the program name. The next few arguments were specified at the command prompt, or in the code block's equivalent.
Note the fifth argument. The two words are enclosed in double quotes, so the entire string becomes the argument. Also, see how the double quotes are not included in the string. This behavior holds true for all operating systems. Once you have the arguments in your code, you can treat them like any other strings. You can compare them to other values, use them to open a file, or do whatever other manipulation is required.
- Using assignment operators
- Working with arguments in the main function
- Setting up global variables
- Using static variables
- Sorting an array
- Building an array of structures
- Working with the ampersand (&) and asterisk (*) pointer operators