A
arnuld
this is the programme which converts a string of digits into its
numeric equivalent, given in section 2.7:
/* atoi: convert s to integer */
int atoi(char s[])
{
int i, n;
n = 0;
for (i = 0; s >= '0' && s <= '9'; ++i)
n = 10 * n + (s - '0');
return n;
}
i ahve 2 questions:
Q 1: i want to know how this function will know that it has reached
the end of string ?
Q 1a. last element of an a string (array of char) is '\0' which is
zero so programme will proceed.
Q 1b. what if element next to the array is a number like 2, 4 or 8.
the for loop will not stop there.
i have put this programme to test:
----------------- PROGRAMME ------------------------------
/* atoi: convert s to integer */
int atoi(char s[])
{
int i, n;
n = 0;
for (i = 0; s >= '0' && s <= '9'; ++i)
n = 10 * n + (s - '0');
return n;
}
int main(void) {
char str[] = "102";
atoi(str);
}
Q 2. this does NOT output anything at all, i expected /102/ as
output,as K&R2 said. what is wrong ?
numeric equivalent, given in section 2.7:
/* atoi: convert s to integer */
int atoi(char s[])
{
int i, n;
n = 0;
for (i = 0; s >= '0' && s <= '9'; ++i)
n = 10 * n + (s - '0');
return n;
}
i ahve 2 questions:
Q 1: i want to know how this function will know that it has reached
the end of string ?
Q 1a. last element of an a string (array of char) is '\0' which is
zero so programme will proceed.
Q 1b. what if element next to the array is a number like 2, 4 or 8.
the for loop will not stop there.
i have put this programme to test:
----------------- PROGRAMME ------------------------------
/* atoi: convert s to integer */
int atoi(char s[])
{
int i, n;
n = 0;
for (i = 0; s >= '0' && s <= '9'; ++i)
n = 10 * n + (s - '0');
return n;
}
int main(void) {
char str[] = "102";
atoi(str);
}
Q 2. this does NOT output anything at all, i expected /102/ as
output,as K&R2 said. what is wrong ?