Beginner C query: unexpected behaviour in programme
I am a beginner programmer reading through the K&R C book and attempting exercises. I am writing in C and compiling in gcc on a Mac.
On one specific exercise, my solution gives unusual results in the output and I can't understand why. I've taken a look at solutions available online, but they don't indicate how i've made an error in my own code. I've also attempted debugging of sorts by putting in printf()s at various points, but that hasn't shed any light. I don't need to solve this problem, since it's purely a hobby, but I think it would be good for me to understand.
The requirement states: "write the function itob(n,s,b) that converts the integer n into a base b character representation in the string s. In particular, itob(n,s,16) formats n as a hexidecimal integer in s". I wrote code with a function itob() and then some test data in main() to check how it works.
I'll post the whole code - sorry if it's long, but I'm not sure on exactly where the error lies.
The 2 unexpected results I find in this programme's output are:
void itob(int n,char s,int b)
1) the output printed by main() shows an "x" at the beginning of string s (e.g. "x10" for hexadecimal version of decimal 16)
2) the output printed by main() appears to show binary ouptut back to front (e.g. "x10100" for binary version of decimal 20)
Any comments from programmers on where I am going wrong would be much appreciated. Thanks.