Search code examples
cprogram-entry-pointargc

Output of following program


#include <stdio.h>

int main(int k)
{
    if(k<10)
            printf("%d ",main(k+1));
    return k;
}

output is:

10 9 8 7 6 5 4 3 2

In arguments of main() function, its argc but how is it used here?


Solution

  • you have used main function as a recursive function thus when you call it with argument 1 it will stack main function while k reach the value of 10, then it dequeue the stack and print values by reverse order. you pass ,2,3,..10 and after dequeue of stack it will print 10,9,..2