help needed with K&R code on p. 87
From: Buck Rogers (who_at_cares.com.au)
Date: 02/29/04
- Next message: Mike Wahler: "Re: help needed with K&R code on p. 87"
- Previous message: Mike Wahler: "Re: [FAQs] scanf returns EOF after reading non-number"
- Next in thread: Mike Wahler: "Re: help needed with K&R code on p. 87"
- Reply: Mike Wahler: "Re: help needed with K&R code on p. 87"
- Reply: Francis Glassborow: "Re: help needed with K&R code on p. 87"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 29 Feb 2004 18:42:23 +1100
Hi guys!
Here's the code:
===============
#include <stdio.h>
/* printd: print n in decimal */
void printd(int n)
{
if (n < 0) {
putchar('-');
n = -n;
}
if ( n / 10)
printd(n / 10);
putchar(n % 10 + '0');
}
=================
1. What is the value of if ( n / 10)? I don't understand this condition
at all. I assume the condition has to be true otherwise the function would
do
nothing. But what makes it true?
So I create my own main to test the above function:
================
#include <stdio.h>
void printd(int n);
int main(void)
{
int n = 123;
printd(n);
return 0;
}
===================
2. The output I get is 123, but I am not really sure how the recursion
works
in the function - I find K&R explanations are very short and cryptic. Can
someone
please explain?
Thanks in advance!
Buck
-- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
- Next message: Mike Wahler: "Re: help needed with K&R code on p. 87"
- Previous message: Mike Wahler: "Re: [FAQs] scanf returns EOF after reading non-number"
- Next in thread: Mike Wahler: "Re: help needed with K&R code on p. 87"
- Reply: Mike Wahler: "Re: help needed with K&R code on p. 87"
- Reply: Francis Glassborow: "Re: help needed with K&R code on p. 87"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]