Stupid main() Function Tricks

The main() function is the first C language function you learn to write. It’s required. And you probably know that it has arguments, which are culled from the command prompt and made available to the program. So what stupid stuff can you do with main()?

First, the argument list need not be specified for the main() function if you’re not using those arguments. Traditionally, they’re coded as argc for the argument count and argv for the list of items typed after the program name at the command prompt:

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

You don’t have to use these argument names, but it’s a good idea. That way you’re code is consistent with other C programs, but both must be specified in the proper order: argument count and then argument values.

The argc integer is always greater than 0. That’s because the first argument is the program’s name. Even when you run a program in a GUI, it has a command line available for use and the first item is the command used to run the program.

The argv argument is an array of strings representing the arguments typed at the command prompt or presented to the operating system in a GUI. The list is parsed, so each item is referenced separately, as a string in the array. (Items enclosed in double quotes are treated as a single string.) The first element in the array is the program name or command issued to run the code.

The argv argument can be specified as either *argv[] or **argv.

The following code demonstrates how to use the main() function’s arguments to display the name used to launch the program:

#include <stdio.h>

int main(int argc, char *argv[])
{
    printf("This program is named %s\n",argv[0]);

    return(0);
}

Here’s the output:

This program is named ./a.out

The name is a.out because I compile my examples at the command prompt without specifying the program name output option. Therefore, the default filename, a.out is used. The ./ directs the shell to run the code from the current directory.

The second nifty thing you should know about the main() function is that it has an address, a memory location, just like any other variable or function in your code.

#include <stdio.h>

int main()
{
    printf("The main() function is at %p\n",&main);

    return(0);
}

The & operator pulls the address of the main() function; the parentheses need not be specified. The result is output by the printf() function, thanks to the %p placeholder. Here’s a sample run:

The main() function is at 0x10b27df40

I’m not sure how this information is useful, but you can collect it.

Finally, you can call the main() function just like any other function in your code. When you do, however, you must remember that main() becomes recursive at this point. To avoid the program continually calling itself, overflowing the stack and crashing, you must add a tripwire to ensure it eventually stops.

#include <stdio.h>

int main(int argc, char *argv[])
{
    printf("Greetings from the main() function!\n");
    if(argc>1) return(0);
    main(2,NULL);

    return(0);
}

In this code, the main() function is called at Line 7. The arguments 2 and NULL are specified. When the function runs again (recursively), the if statement at Line 6 tests the argument count and returns without re-calling the main() function a third time. Here’s the output:

Greetings from the main() function!
Greetings from the main() function!

This output assumes that you didn’t type a command line argument the first time you ran the code. If you do, then the value of argc is greater than 1, and the output appears only once.

Leave a Reply