This little program, which is heavily adapted from K&R (Exc 4-12)
converts an integer to a string, recursively. My questions are these.
1) I think that as each successive recursion occurs, the automatic
variables are retained somewhere? so that when the function is
completed at a later point ( I hope I am expressing this vaguely
correctly) those automatic variables that were retained are again used.
Is my understanding correct?
2) In this particular function, I noticed that as each recursion
completes it's evaluation, 'k' is returned. I know where 'k' goes in
the last evaluation, (back to "main") but where does it go, for lack of
a better phrase, prior to that?
Thanks in advance.
/*****/
# include <stdio.h>
# define MAXNUM 50
int main () {
int itoa( char s[], int i);
int i=-234, j;
char s[MAXNUM];
j=itoa(s,i);
printf ("the number of characters in %s is %d", s, j);
return 0;
}
/******** itoa ***********/
# include <math.h>
int itoa( char s[MAXNUM], int i){
static int k;
{
if ( i / 10)
itoa(s, i/10);
else{
k=0;
if (i < 0)
s[k++]='-';
}
s[k++]=(abs(i) % 10 + '0');
s[k]='\0';
}
return k;
}
/******/