Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Explain this C programme?

  1. May 10, 2009 #1
    explain this C programme???????

    #include<stdio.h>
    void func (int x);
    void func(int x)
    {
    if (x>0)
    func(x-1);
    printf("%d",x);
    }
    int main()
    {
    func(4);
    return 0;
    }

    this C programme gives the output of

    0 1 2 3 4

    can anyone explain me how this happens im totally confused ................
     
  2. jcsd
  3. May 10, 2009 #2

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Re: explain this C programme???????

    No explanation will be nearly as useful as you working it out yourself. Trace the execution of your program by hand. What is the very first thing it does when it's started?
     
  4. May 10, 2009 #3
    Re: explain this C programme???????

    yr i tried like that ,..........but according to my dry run

    first function get 4 and its greater than 0 and coz of that function get 3 like wise it goes until 1 then when the function get 1 and as it is also greater than 0 function get 0.Then 0 is NOT greater than 0 .so if loop is skipped then the 0 will be printed .but how on the earth these 1234 gets print out .............i dont how it happens please help me ..........please ignore my dumbness if this is a very easy one .....:D
     
  5. May 10, 2009 #4
    Re: explain this C programme???????

    Think about flow of control. How do these functions get called exactly? Ever heard of recursion?
     
  6. May 10, 2009 #5
    Re: explain this C programme???????

    Need more explanation .!Badly needed.........!yr of course i have heard about recursion .......its about calling a function inside it self which is same as here ...........but can u please help me with the dry run ..........it will be a great help ......coz im doing 6 subjects and dont have enough time to think about 1 for a long ..........exam is coming on the way ......any help would be greatly appreciated.thanks!
     
  7. May 10, 2009 #6

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Re: explain this C programme???????

    Speak English, please; save the txtk for texting.


    I think you haven't internalized the notion of an execution stack. You've realized that func gets called five times, but it looks like you've mentally merged all of them together.

    But that's not what happens -- each of the five calls to func give five completely different "frames", and you have to keep track of them all and keep them separate.
     
  8. May 10, 2009 #7
    Re: explain this C programme???????

    Here's a simple example... call func with 2.

    Code (Text):


    func(2)
    ...x=2
       x>2?
          yes, so...func(2-1)
                       ...x=1 ... this is a new "x" variable for this level.
                       ...x>0?
                             yes, so...func(1-1)
                                          ...x=0 ... this is another new "x" variable.
                                             x>0? no.
                                             print "0"
                       print "1"... remember, x was "1" at this level.
       print "2" ... again, x was "2" at this level.
     
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Explain this C programme?
  1. C Pointers explained! (Replies: 4)

Loading...