Home > database >  Self-study c encounter a problem, thought for two days didn't think and consult
Self-study c encounter a problem, thought for two days didn't think and consult

Time:03-31

On the first code
#include
Void itoa (int n, char [] s);
//atoi function: converting s int
Int main ()
{
int n;
Char s [100];
Printf (" Input n: \ n ");
The scanf (" % d ", & amp; N);
Printf (" the string: \ n ");
Itoa (n, s);
return 0;
}
Void itoa (int n, char [] s)
{
Int I, j, sign;
If ((sign=n) & lt; Recording symbols
0)//N=- n;//make n positive
i=0;
Do {
S [i++]=10 + n % '0'.//to remove a number
}
While ((n=10)/& gt; 0);//delete the number
If (sign<0)
S [i++]='-';
S [I]='\ 0';
for(j=i; J>=0; J -)//generated Numbers are reverse, so let's reverse output
Printf (" % c ", s [j]);
}
Is s [I]='\ 0', suppose I input the integer - 12, then s [0] is' 2 ', s [1] is' 1 ', s [2] is' - ', then the problem to the s [I] to [3], that is, '\ 0' s, I think printf output became '\ 0' - 1 instead of 2-12 '\ 0', but why tutorial so to write? Really think impassability, two days, is really annoyed
  • Related