-1

I'm printing the following pattern using loops:

1 12 123 1234 12345

Why is the following code displaying some random values before displaying the pattern? It works fine when I do so using recursion to some random function func(int a, int b) and passing values (1, 1) from main to this function.

int a=1,b=1;
int main(int a, int b)
{
    if(a>5)
       return 0;

    else if(b<a)
    {
        printf("%d",b);
        main(a,++b);
    }

    else if(b==a)
    {
        printf("%d ",b);
        main(++a,1); 
    } 
}
honk
  • 9,137
  • 11
  • 75
  • 83
kashish
  • 15
  • 1
  • 9
    `int main(int a, int b)` is not one of the valid signatures for `main`. The operatimg system calls `main` with the number of arguments and the list of argument strings as expected and you get grabage results. Calling `main` recursively is very geeky, but rarely useful. – M Oehm Sep 15 '15 at 12:23
  • Take a look: http://stackoverflow.com/questions/13948562/recursion-using-main-function – Leo Chapiro Sep 15 '15 at 12:30
  • 4
    Don't ever call `main` yourself. Write another function and call it recursively. – Wolf Sep 15 '15 at 12:35
  • one question .. Why do this ???? Use another function . – ameyCU Sep 15 '15 at 12:37

2 Answers2

5

According to the C standard1, the following signatures for the main() function are valid:

int main(void)

and

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

As you get passed the parameters of the command line to the main() function (there is always at least one: the name of the program is always the first element of argv[], i.e., argv[0]), you get strange results, because the parameters are casted to your integer values.

You should therefore use an own function for recursion tasks, for example int func(int a, int b), as you mentioned.

1 Note: My answer is a bit over-simplified. For a more detailed and correct description please refer to this answer.

Community
  • 1
  • 1
honk
  • 9,137
  • 11
  • 75
  • 83
  • This answer is an over-simplification and not really correct. [See this](http://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c/31263079#31263079). – Lundin Sep 15 '15 at 12:45
  • @Lundin: Thanks! I linked your answer as detailed reference. – honk Sep 15 '15 at 13:05
2

int main(int a, int b) is not one of the standard forms of main().

A compiler is only allowed to change the type of main() in some rare cases (listed here). In case it does so, what happens is implementation-defined behavior and the compiler must document for the programmer how main should be used.

The programmer may never invent their own custom format of main.

I very much doubt there ever existed a conforming compiler where the form int main(int a, int b) was supported and documented.

Most likely you are abusing the lack of type safety for the parameters of main on some old compiler, which explains why it even compiles in the first place. I suspect you are simply invoking undefined behavior and anything might happen.

Since you are not allowed to invent your own form of main, since there is never a reason to call main() and since there is never a reason to call it recursively, simply don't do it.

Community
  • 1
  • 1
Lundin
  • 195,001
  • 40
  • 254
  • 396
  • The problem statement seems to be use main recursively. This can be done just manipulating argc, without changing argv. – rcgldr Sep 15 '15 at 18:40
  • @rcgldr Be that as it may, the only thing important here is to understand why you can't and shouldn't write code like this. – Lundin Sep 16 '15 at 06:14
  • @Lundin - I'm not stating that this would be good code, only that the title is "recursion using main function ... ", and I've seen it here at SO before. I'm curious if this is the actual assignment or if the OP misunderstood it. – rcgldr Sep 16 '15 at 06:52