简体   繁体   中英

Calling main function more than once is a good practice in C?

I saw a C code like this:

#include <stdio.h>

void main  ()
{
    static int ivar = 5;
    printf ("%d", ivar--);

    if (ivar)
        main ();
}

which outputs :

54321

I'm a novice in C and I guess until the condition fails the main method is called again and again. Since I'm a novice in C, is it good practice to call the main function more than once as in the above case? Are there any real world cases, where this kind of code is very useful?

Thanks in advance.

In your example, it doesn't matter because it's a very small piece of code. But in the general case I think calling main is a bad idea for the following reasons:

  • Readability. When examining a program no one would assume that main will be called at one point. When you see that it is, you have to back track and reread the whole thing. Besides, main is not a meaningful name in the sense that it's not clear what the intent of the recursion is. So I'd write another function with a meaningful name to reflect this.
  • Reusability. It's quite probable that a new function with a meaningful name will be useful in more than one place in a complex program.
  • Command line arguments. At one point you may need command line arguments in your program. Even GUI programs need them (for file associations etc.). And you will need to refactor all your calls to main to take that into account.
  • C++ compatibility. It's illegal in C++.

I would say it's rarely, if ever, a good idea to call the main function. If you're going to recurse, create a function to do it.

A while-loop would be more suitable. Recursion makes sense when, with each recursion, you are doing a different job -- typically a smaller one.

What this code is really doing is demonstrating function-local static variables: the ivar is only initialised in the first call of main . Each time you recurse, it is decremented despite the ivar=5 statement.

main has a special meaning. Idiomatically, it should initialise the environment and then call some other function which drives the application logic.

An optimising compiler might well transform that code into an iterative version anyway.

This is not often seen (I've never seen it done before), very confusing, since main is supposed to be called once when the program starts and end when the program ends, and largely impractical in most real programs, since you would need to stop the subsequent calls to main() from parsing the command line again.

It's far more reasonable to just write a separate recursive function and call it from main and using ordinary function arguments instead of static variables.

It's called recursion and it can be very useful. For instance traversing a tree. Some math calculations also use recursion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM