C Recursion (explain)

Hi,

Question: how come the output is like that? Can explain to me abit. I am learning C.

Thanks!

#include <stdio.h>
#include <string.h>
void printit(char line_of_char[], int index);
int main()
{
char line_of_char[80];
int index = -1;
 strcpy(line_of_char, "This is a string.");
 printit(line_of_char, index);
 return 0;
}
void printit(char line_of_char[], int index)
{
 if(line_of_char[index])
 {
  index++;
  printf("+%c", line_of_char[index]); /*how come this is not being printed after "-" was printed*/
  printit(line_of_char, index);
  printf("-%c", line_of_char[index]);
 }
}
// Output is:
//+T+h+i+s+ +i+s+ +a+ +s+t+r+i+n+g+.+ - -.-g-n-i-r-t-s- -a- -s-i- -s-i-h-T
void printit(char line_of_char[], int index)
{
 if(line_of_char[index])
 {
  index++;
  printf("+%c", line_of_char[index]); /*how come this is not being printed after "-" was printed*/
  printit(line_of_char, index);
  printf("-%c", line_of_char[index]);
 }
}

function call pushes data onto the stack, so what this does is to push various
values of index onto the stack. The first print statement is using the value of index as it gets incremented 1,2,3 ...

The second printf (after the recursive call) statement is going "back in time" by popping values off the stack 10,9,8, .....

Change the code so it just prints value of the index variable, nothing else. Then you will see.

BTW the "if(line_of_char[index])" has a problem, the initial value is -1, which is the character before the start of the variable line_of_char. Apparently it works for you, but that it called "programming by coincidence" a really bad thing. It could trigger a segfault on other systems. Or other problems.

---------- Post updated at 08:29 ---------- Previous update was at 08:16 ----------

Corrected code, sort of:

#include <stdio.h>
#include <string.h>

void printit(char line_of_char[], int index);
int main()
{
   char line_of_char[80]={0x0};
   int index = -1;
   
   strcpy(line_of_char, "This is a string.");
   printit(line_of_char, index);
   printf("\n");
   return 0;
}
void printit(char line_of_char[], int index)
{
   if(index ==-1 || line_of_char[index])
   {   
   	index++;
    printf("+%c", line_of_char[index]); 
    printit(line_of_char, index);
    printf("-%c", line_of_char[index]);
   }
}

"if(line_of_char[index])" evaluates to false because index "-1" is out of the array boundaries and is not at an offset position so you really should not be getting any output at all.

Edit: Sorry, I didn't see Jim's reply. :slight_smile:

Hi Guys,

Alright, the line:
if(line_of_char[index])
should have some problems with other systems, but somehow works for me right now. But I don't mind this for now :slight_smile: (Thanks)

I've changed the code and output the index:
+0+1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17-17-16-15-14-13-12-11-10-9-8-7-6-5-4-3-2-1-0

The first print statement is using the value of index as it gets incremented 1,2,3 ...
I understand this one clearly since it's clear to me what the code is doing as it never reaches the 2nd printf so it keeps on calling 1st printf.

The second printf (after the recursive call) statement is going "back in time" by popping values off the stack 10,9,8, .....

I am still tying out to understand how it prints the 17 down to 1 since I never did something (index--). I understand the stack is going back but can you maybe help me understand this? Or, should I just set my thinking that it will go back after the recursive call of the 1st printf? (I'll read more about stack later on, need to learn C bit by bit)

:smiley: Thanks a lot!

The stack is used as a temporary storage place for function arguments, return addresses and local variables.

The memory area for each function call is placed on the top of the stack, and then taken off when the execution of the call is completed.

Every time your function goes back one step in the nest, the previous value of i is popped off the stack thus it appears as if it is being decremented (but actually it's not)... what is really being decremented is the stack pointer.

All nested functions (in C at least) rely on some sort of stack so you should probably spend a bit understanding the concept. :smiley:

Now I understand it. That helped me a lot understanding this code.

Thanks alot guys!

.: Seede

[CLEARED]