-3
#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?

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
Nikhil
  • 13
  • 1
  • 2
  • What is your compiler/environment, that is not a standard compliant signature for main unless your implementation specifically allows it. – Shafik Yaghmour Dec 28 '13 at 18:49
  • Ooh..I am sorry..I forgot to mention that..its GCC – Nikhil Dec 28 '13 at 19:02
  • 1
    The numbers of parameters you pass to the program code will determine the initial value of k. Without passing any parameter, the initial value of k will be 1. The recursive calling to main doesn't use main the same way the O/S passes parameter to main. It calls itself just like any regular function where it passes an int argument of k+1. The recursion happens as a parameter to printf thus printf will wait for the call to main to return before it can print. The result is a reverse print order. – alvits Dec 28 '13 at 19:09

2 Answers2

4

First your signature of main is what standard defines it. Your compiler should give warning:

[Warning] 'main' takes only zero or two arguments [-Wmain]  

C11: 5.1.2.2.1 Program startup:

The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:

int main(void) { /* ... */ }

or with two parameters (referred to here as argc and argv, though any names may be used1,as they are local to the function in which they are declared):

int main(int argc, char *argv[]) { /* ... */ }

or equivalent;10) or in some other implementation-defined manner.

Now, you can give any name to argcand argv. Here argc is k. Since you are passing no parameter to main the value of k is 1 because here argv[0] is the name of the program. Now k=1 is used by the program as initial value and the value

10 9 8 7 6 5 4 3 2 

is printed by recursive call of main.


1. emphasis is mine.

haccks
  • 104,019
  • 25
  • 176
  • 264
  • 1
    +1 A program that is not passed an argument will have argv[0] == program name and argc == 1 which in the code is renamed k. – alvits Dec 28 '13 at 18:54
  • Your are making a likely but possibly incorrect assumption that the implementation does not define that signature to be valid. – Shafik Yaghmour Dec 28 '13 at 18:54
  • @ShafikYaghmour; I quoted what standard is saying and what compiler's warning is. – haccks Dec 28 '13 at 18:55
  • It also says `or in some other implementation-defined manner.`. – Shafik Yaghmour Dec 28 '13 at 18:56
  • @muradin; Do you wanna copy of that? :). Download this from [here](https://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&cad=rja&ved=0CDgQFjAB&url=http%3A%2F%2Fweb.cs.dal.ca%2F~vlado%2Fpl%2FC_Standard_2011-n1570.pdf&ei=GCC_UoCAL4-aiQfm5oGQDQ&usg=AFQjCNEryn7-KUvl7UjdOYs8mc9SE1QDTA&sig2=BAUzf0N11rB5KCh7JdNUrQ&bvm=bv.58187178,d.aGc). – haccks Dec 28 '13 at 18:58
  • @ShafikYaghmour; I removeded that contradictory word now. – haccks Dec 28 '13 at 18:58
  • @muradin [Where do I find the current C or C++ standard documents?](http://stackoverflow.com/questions/81656/where-do-i-find-the-current-c-or-c-standard-documents) probably has a link to all of the draft standards available. – Shafik Yaghmour Dec 28 '13 at 19:00
  • @haccks is it possible or copyright protected? if it's possible i will appretiate. :) – muradin Dec 28 '13 at 19:01
  • 4
    @muradin You have to pay for official standards but the drafts are freely available. – Shafik Yaghmour Dec 28 '13 at 19:02
  • @muradin; As Shafik suggested draft is free. I provided the download link. – haccks Dec 28 '13 at 19:04
0

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

muradin
  • 1,249
  • 2
  • 14
  • 34
  • 2
    Actually when the program code is called without any argument, k will be 1 because arrgv[0] is the name of the program, thus k (which was argc) is 1. The more parameters you pass to the program the higher value k will be and the less numbers to print. If you pass a parameter of 1 that will make argc == 2 or k in this code. – alvits Dec 28 '13 at 19:01