How to Use the main() Function Arguments in C Programming - dummies

How to Use the main() Function Arguments in C Programming

By Dan Gookin

Back in the old days, programs featured command-line options or switches. For example, to compile and link a C program, you would type something like this:

cc ex1501.c -o ex1501

The three tidbits of text after the cc command are options or switches. They are also arguments to the main() function. A program can read these arguments, even today, when the world runs graphical operating systems. All you need to do in your code is examine the arguments to the main() function.

How to read the command line

Pretend that it’s 1987 and you’re writing a program that says “Hello” to the user by name. The way you get the user’s name is to have your code swallow the first chunk of text that appears after the program name at the command line. That code may look something like Well, Hello There!


#include <stdio.h>
int main(int argc, char *argv[])
 printf("Greetings, %s!n",argv[1]);

Line 5 uses the int value argc to determine whether any additional items were typed after the program name at the command prompt.

Line 6 uses the string value (char array) argv[1] to display the first item after the program name at the command prompt.

Exercise 1: Type the source code from Well, Hello There! into a new project. Build and run.

The program displays no output unless a command-line argument is specified. To make that happen in Code:: Blocks, follow these steps:

  1. Choose Project→Set Programs’ Arguments.

    The Select Target dialog box appears.


  2. Type command-line text in the Program Arguments portion of the Select Target dialog box.

  3. Click the OK button.

  4. Run your program again to see its output given the command-line arguments.

If you’re coding at the command prompt, you can run the program like this:

ex1501 Shadrach

Press the Enter key to run the program.

The code uses only the first command-line argument, so if you type more, they’re ignored. For example:

ex1501 Shadrach Meshach Abednego

In the preceding line, only Shadrach’s name appears in the output.

Basics of main()’s arguments

When you don’t plan on your program accepting any command line arguments, you can leave the main() function’s parentheses empty. Like this:

int main()

When arguments are used in your code, they must be declared. Using them looks like this:

int main(int argc, char *argv[])

argc is the argument count value. It’s an integer that ranges from 1 through however many items were typed after the program name at the command prompt.

*argv[] is an array of char pointers. You can think of it instead as an array of strings, which is how it can be used in your code.

The code in Argument Counter merely counts the number of arguments typed at the command line. That value, argc, is displayed.


#include <stdio.h>
int main(int argc, char *argv[])
 printf("You typed %d arguments.n",argc);

Exercise 2: Type the preceding source code. Build and run by typing no arguments.

The main() function receives information about the command-line argument directly from the operating system. The command line is evaluated, and arguments are tallied and referenced. The tally appears as argc, and the references are stored in the argv[] array.

When no arguments are typed — in Code::Blocks, that means the Program Arguments window remains empty — you see this output:

You typed 1 arguments.

That’s because the program name itself is considered the first argument. You can prove it by adding a single line to the code:

printf("That argument is %s.n",argv[0]);

Exercise 3: Modify your source code by adding the preceding line, inserting it after the first printf() statement. Build and run.

The program’s output now displays the program’s name — most likely, a full path to the program, which is accurate but a bit of overkill.

Exercise 4: Modify the code again, this time adding a for loop to work through all the arguments and displaying each one. For example, the output may look like this:

begc4d$ ./ex1504 Shadrach Meshach Abednego
Arg#1 = ./ex1504
Arg#2 = Shadrach
Arg#3 = Meshach
Arg#4 = Abednego