Originally published by Robert Beisert at fortcollinsprogram.robert-beisert.com

Linux + C – Adding Arguments

It’s finally time to look at those arguments we pass into main() – int argc and char *argv[].

Terminal Arguments

You might have noticed that vi and gcc are both programs. You might also have noticed that we always call these programs with arguments.

You might also notice that, up to this point, we haven’t called any of our programs with arguments. However, we already have all the tools we need to do so.

Whatever we type into the terminal is automatically interpreted by all of our C programs. The two arguments we pass into main – argc and argv – allow us to handle all these terms.

argc is a counter which tells us how many arguments we have. For example, if we call gcc with the following command:

gcc -o panda OMGPanda.c

argc will be 4, because there are four arguments on the line (counting the program name itself).

argv contains the actual arguments, separated by spaces. In the above case, the array argv contains the following strings:

argv[0] = “gcc”

argv[1] = “-o”

argv[2] = “panda”

argv[3] = “OMGPanda.c”

Note: we can pass any string we want into an argument using quotation marks. We could make argv[4] = “We’re whalers on the moon…” by putting that text into quotes.

It may take a bit of time to get used to working with argc and argv, but it opens up your programs to many more possibilities.

args.c

The program args.c is a basic math program, which performs a few simple operations. We’re not going to check the arguments yet (that’s a bit more advanced than this post), so go ahead and break the program. Try to find as many problems as you can, because error handling is about 60% of what programmers do.


#include <stdio.h>
#include <stdlib.h>

//int argc refers to the number of arguments sent to the program
//    The function name counts as one argument
// char *argv[] refers to the actual text of every argument sent to the program
//    The function name is argv[0]
int main(int argc, char *argv[])
{
int a;
int b;
//We're going to use the if-else chain for error handling
//In order to only have one escape (return value), we'll write an error code
int error_code = 0;
//This is one of the best ways to create a huge block of text to print: const char *
const char *description = "This program performs the five basic integer operations (+ - * / %) on two input integersn";

if(argc != 3)
{
//We want this program to take exactly two arguments, both integers.

//fprintf can write to a file pointer
//stderr is a file pointer for the standard error stream
fprintf(stderr, "%sUsage: %s Integer Integern", description, argv[0]);
error_code = 1;
}
else
{
a = atoi(argv[1]);
b = atoi(argv[2]);

printf("%d + %d = %dn", a, b, a+b);
printf("%d - %d = %dn", a, b, a-b);
printf("%d * %d = %dn", a, b, a*b);
printf("%d / %d = %dn", a, b, a/b);
//In a string, we use %% to print a single % sign
printf("%d %% %d = %dn", a, b, a%b);
}

return error_code;
}

Applications

Look back at all the other programs we’ve written. I guarantee you there are many interesting ways to use arguments in there, so start exploring.