445,820 Members | 1,189 Online
Need help? Post your question and get tips & solutions from a community of 445,820 IT Pros & Developers. It's quick & easy.

K&R2 section 2.7 type conversions

 P: n/a 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[i] >= '0' && s[i] <= '9'; ++i) n = 10 * n + (s[i] - '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[i] >= '0' && s[i] <= '9'; ++i) n = 10 * n + (s[i] - '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 ? Mar 13 '07 #1
7 Replies

 P: n/a arnuld wrote: 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[i] >= '0' && s[i] <= '9'; ++i) n = 10 * n + (s[i] - '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 ? When a character < '0' or '9' is read. > Q 1a. last element of an a string (array of char) is '\0' which is zero so programme will proceed. It is zero, which is less than '0' for any character set I've seen. Don't confuse the character '0' with the value 0. Q 1b. what if element next to the array is a number like 2, 4 or 8. the for loop will not stop there. Please explain that a bit more. > 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[i] >= '0' && s[i] <= '9'; ++i) n = 10 * n + (s[i] - '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 ? Try printing something! -- Ian Collins. Mar 13 '07 #2

 P: n/a arnuld wrote: 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[i] >= '0' && s[i] <= '9'; ++i) n = 10 * n + (s[i] - '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 ? It doesn't. It knows when the next character is out of the range of digits, '0'...'9'. If it reaches the end of the string before reaching some other non-digit character, it will encounter the '\0' which is at the end of every string, and that '\0' character is also out of the range of digits. Q 1a. last element of an a string (array of char) is '\0' which is zero so programme will proceed. The last element of a string is always '\0'. The last element of an array of char need not be. And, no, the value of '0' (the digit zero) and of '\0' (numeric zero) are not the same. Q 1b. what if element next to the array is a number like 2, 4 or 8. the for loop will not stop there. It will, as I said, consume chars until it reaches a non-digit. Note that not only does this function have a problem with a char array without a '\0' (so not a string), but it has a problem with overflow when a string of digits interpreted to have a value greater than INT_MAX is passed to it. > 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[i] >= '0' && s[i] <= '9'; ++i) n = 10 * n + (s[i] - '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 ? You have no output statements. Why should you think that a program that makes no attempt to output anything should, quite pervesely, output something? Mar 13 '07 #3

 P: n/a Ian Collins said: arnuld wrote: >>Q 1a. last element of an a string (array of char) is '\0' which iszero so programme will proceed. It is zero, which is less than '0' for any character set I've seen. Yes. In fact, C *requires* that the integer value of '\0' is 0, and that the integer value of '0' is positive. -- Richard Heathfield "Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk email: rjh at the above domain, - www. Mar 13 '07 #4

 P: n/a On Mar 13, 10:49 am, Martin Ambuhl = '0' && s[i] <= '9'; ++i) n = 10 * n + (s[i] - '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 ? You have no output statements. Why should you think that a program that makes no attempt to output anything should, quite pervesely, output something? i have an output statement: /return i/ why it does not put "i" on my Terminal ? Mar 13 '07 #5

 P: n/a arnuld wrote: >On Mar 13, 10:49 am, Martin Ambuhl = '0' && s[i] <= '9'; ++i) n = 10 * n + (s[i] - '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 ? You have no output statements. Why should you think that a program thatmakes no attempt to output anything should, quite pervesely, outputsomething? i have an output statement: /return i/ (a) You have no `return i` statement. (b) `return i` isn't an output statement. It's a return-this-value-as- the-result-of-this-function statement. why it does not put "i" on my Terminal ? (fx:echo) You have no output statements. -- Chris "electric hedgehog" Dollin "A facility for quotation covers the absence of original thought." /Gaudy Night/ Mar 13 '07 #6

 P: n/a arnuld wrote: 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[i] >= '0' && s[i] <= '9'; ++i) n = 10 * n + (s[i] - '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 ? When the test condition of the for loop fails, i.e. when a character with a value less than '0' or greater than '9' is read. That need not correspond to the end of the string. For example in the following char sequence: 987df'\0' the loop will stop when it reads 'd' tests it against '0' and '9'. C gives a guarantee that the character sequence '0', '1', ..., '9' will occupy successively higher character code values. That guarantee is the basis for this function, both in it's loop test condition and it's actual condition. If the function is fed: gd677326'\0' it'll return immediately. On the other hand, if it's passed: 12345'\0' it'll return when it encounters the end of the string. Q 1a. last element of an a string (array of char) is '\0' which is zero so programme will proceed. No, the value of the character '0' is not 0. Similarly the value of the character '9' is not 9. In fact, in C, the value zero is always treated as the null character, so if the underlying system's character set maps '0' to 0, then the C implementation will be forced to remap the character set, but this state of affairs has, to my knowledge, never happened, since no system seems to assign value zero to character '0'. Q 1b. what if element next to the array is a number like 2, 4 or 8. the for loop will not stop there. You're confused between a character and it's representation code. 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[i] >= '0' && s[i] <= '9'; ++i) n = 10 * n + (s[i] - '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 ? Where're are you invoking an I/O function like printf to display the value returned by atoi. You simply call atoi and discard it's return value. Do: int number; number = atoi(str); printf("%d\n", number); Mar 13 '07 #7

 P: n/a arnuld wrote: On Mar 13, 10:49 am, Martin Ambuhl = '0' && s[i] <= '9'; ++i) n = 10 * n + (s[i] - '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 ? You have no output statements. Why should you think that a program that makes no attempt to output anything should, quite pervesely, output something? i have an output statement: /return i/ Actually it's 'return n;', and it's not an output statement. What it does is to return a value to the calling function, in an implementation defined manner. A function that returns a value resolves to it's return value in any expression. For example assume the function fx always returns the value 5. So in the expression below, the call to fx resolves to it's return value, i.e. 5. int a = 5, b; b = a + fx(); Where do you see any output. Input/Output, (I/O), is generally considered to happen when data is received by the program from external sources, (a keyboard, mouse, network interface etc.), or data is sent from the program to it's external environment, (screen, disk, network interface etc.). The communication of data from one place in memory to another, (which is what is happening in that return n statement), is not considered as I/O. In any case, it's a transfer between and within the processor and system memory, not to any I/O device, like the display device, so no output is visible. In C, to print output to the screen, (more precisely to the stdout stream), use the functions printf, puts and putchar. To send data to an arbitrary output stream, (i.e. other than stdout, example a disk file), use one of fputc, putc, fputs, fprintf. Similarly to get input from the stdin stream, use scanf, getchar. To get input from an arbitrary input stream, use getc, fgetc, fgets, fscanf etc. Mar 13 '07 #8