Hi everyone,
I've been writing programmes in C for about a year now (with some big gaps in between), and I'm never comfortable with anything I write. It seems that no matter what method I choose to solve the problem, there's always a better method that I miss. My programmes almost always work but I always write huge programmes for simple tasks and the phrase "programme efficiency" can in no way be related to any of my programmes. Today, I wrote an itoa function that uses recursion. Once again, it works, but I'm sure there's a better way of doing it. I thought maybe if I post the programme, you guys would be good enough to point out some issues, so here's the code:
I appreciate some advice.Code:char* itoa(int num) { static int cnt = 0; static char s[10]; static int i = 0; char ch; if(num < 0) { s[cnt++] = '-'; num = -num; i = cnt; } if(num / 10) { cnt++; itoa(num / 10); } ch = (char)(num % 10 + '0'); s[cnt-(cnt-i)] = ch; if (cnt == i) return s; else { i++; return NULL; } }
P.S.: Generally, when the number of variables I use gets big, I know there's something wrong.