This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

can not perform recursion function correctly

hi: I want to use recursion function to transform a integer from binary form to ascii form. But the result is wrong.I don't why ,please help to analyse.

soure code:

void
binary_to_ascii(unsigned int value)
{

   unsigned int quotient;
   quotient = value/10;
   if(quotient !=0 )
       binary_to_ascii(quotient);
   putchar(value%10+'0');

}


int
main()
{
 while(1){
    binary_to_ascii(4267);
  }
}


I want to produce characters '4','2','6','7' in sequence. but the result is '4','4','4'. I don't know what is wrong with my code.

Parents
  • No reason to not use recursion for cases where it gives an advantage. Finding examples of how a tool can be abused doesn't mean that a tool should never be used.

    no argument.

    The point is that if you do "C is C" and do not know the implications with the architecture, you have not taken the time to get to know the '51 aka "architectural ignorance".

    There are, indeed cases where a careful use of one of the features that have been wrested into C51 can be justified, but doing so just because "C is C" is plain foolish.

    Erik

Reply
  • No reason to not use recursion for cases where it gives an advantage. Finding examples of how a tool can be abused doesn't mean that a tool should never be used.

    no argument.

    The point is that if you do "C is C" and do not know the implications with the architecture, you have not taken the time to get to know the '51 aka "architectural ignorance".

    There are, indeed cases where a careful use of one of the features that have been wrested into C51 can be justified, but doing so just because "C is C" is plain foolish.

    Erik

Children