tags:

views:

63

answers:

9
void display(int a)
{
  printf("\n%d",a);
  if(--a)
    display(a);
  printf("\n%d",a);
}

main()
{
  display(4);
}

Please explain the above program. i can't understand.what is the output.ya sir i got oupt like this 4 3 2 1 0 1 2 3 how is it sir

A: 

The function display is called recursively until its passed in parameter is equal to 1. The function call will print out 1 and then 0 at the end on the last recursive call.

The function was meant to have a pre-condition of passing in a positive value >= 1.

--a means to decrease the value by 1 and then return the result. So it will only call recursively when a != 1.

Brian R. Bondy
Till a is equal to 1 I think...
Sijin
A: 

You haven't given much detail about what it is that you don't understand. Hope this helps:

// Declare Display function
void display(int a) 
{ 
// Print the value of a (passed in)
    printf("\n%d",a); 

// Reduce a by 1.  If the result is non-zero then recursively call the function with the value of a
    if(--a) 
        display(a); 

//Print the value of a again        
    printf("\n%d",a); 
} 

main() 
{ 
// Start with 4
    display(4); 
}
pm_2
A: 

In pseudocode:

display(a):
  print a
  decrement a
  if a > 0, call display (a)
  print a

main
  display (4)
dsolimano
A: 

The function prints all the number from a and down until it reaches zero and then prints all number in increasing order until it reaches a-1. the output would be:

4
3
2
1
0
1
2
3

Bob
how sir u r correct.but how it willcame
thulasi
A: 

Probably the easiest way to understand an example like this is to just compile it and then single-step through it in a debugger. Watch the stack and watch how the value of a changes.

Paul R
A: 

The important thing is to realize how the condition is evaluated:

if (--a) means: decrease a by one and then check its value

if (a--) means check the value of a, and then decrease it by one

So in the first case, the decision on whether to run the code in the block is done AFTER the variable is decreased. On the other hand, the second case first evaluates the variable and THEN decreases it (and then runs the code in the if statement or not, depending on the result of the evaluation)

PeterK
A: 
  • Display(4): print "4", call display(3), print "3".
  • Display(3): print "3", call display(2), print "2".
  • Display(2): print "2", call display(1), print "1".
  • Display(1): print "1", dont call display(0), print "0".
  • So the result will be: 4 3 2 1 0 1 2 3
Andy
can u explian briefly
thulasi
Calling display(4) will, basically, print 4 and 3. In between the two values, display(3) will be called(because "if(--a){...}" is equivalent to " a = a - 1; if(a > 0){...}"). Display(3) will print 3 and 2 calling Display(2 in between. So you have: 4[ display(3)]3; => 4[3[display(2)]2]3; => 4[3[2[display(1)]1]2]3 => 4[3[2[1[ here the value of a will be 0; display(0) will NOT be called, so recursion stops]0]1]2]3
Andy
+1  A: 

The loop would be the following:

Display(4): print "4", call display(3).
    Display(3): print "3", call display(2).
        Display(2): print "2", call display(1).
            Display(1): print "1", dont call display(0), print "0", returns to Display(2).
        Display(2): print "1", returns to Display(3).
    Display(3): print "2", returns to Display(4).
Display(4): print "3", exits.

So the result will be: 4 3 2 1 0 1 2 3

pcent
why am asking like this means . i have little knowledge in c
thulasi
A: 

Paul R, I'm pretty sure that if he can't understand what that code does, he won't know how to use a debugger.

Hmmm, how can I response to a comment instead of the main thread? Thanks!

Edit: Ok, I need some more reputation. FAQ did its job.

Tronfi