Thread: K&R Page 61 atoi function example

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    10

    K&R Page 61 atoi function example

    Hi,
    I am just trying to see how to run the atoi code on Page 61. I wrote the following but the below code prints just 0.

    Can anyone please help? Thank you.

    Note: I just want to thank the folks that help us newbies on this website. For beginners like myself, experts on websites like these offer tremendous opportunity to learn. Thank you for taking time to help people like myself who are starting learning C from absolute scratch (Nope, not trying to score brownie points, I sincerely mean it).

    Andrew.

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    #define MAX 5
    
    int atoi(char a[]);
    
    int main()
    {
            int val;
            char s[MAX] = {'s', ' ', 'a','b',' '};
            while(val = atoi(s)) {
                    printf("%d\n",val);
            }
            return 0;
    }
    
    /* atoi: convert s to integer; version 2 */
    int atoi(char s[])
    {
            int i, n, sign;
            for (i = 0; isspace(s[i]); i++) /* skip white space */
                    ;
            sign = (s[i] == '-') ? -1 : 1;
            if (s[i] == '+' || s[i] == '-') /* skip sign */
                    i++;
            for (n = 0; isdigit(s[i]); i++) {
                    n = 10 * n + (s[i] - '0');
            }
            return sign * n;
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    int main()
    {
            int val;
            char s[MAX] = {'+', '1', '0','0',' '};
            val = atoi(s);
            printf("%d\n",val);
            return 0;
    }
    Try using above code to test atoi.

    Tim S.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by stahta01 View Post
    Code:
    int main()
    {
            int val;
            char s[MAX] = {'+', '1', '0','0',' '};
            val = atoi(s);
            printf("%d\n",val);
            return 0;
    }
    Try using above code to test atoi.

    Tim S.
    Better yet, realise that MAX is defined as 5 and therefore neither example has a null-terminator. That's also not the best way to write a string.
    A better example would be:
    Code:
    int main()
    {
            char s[] = "-13572468";
            int val = atoi(s);
            printf("%d\n",val);
            return 0;
    }
    Yes I know it's longer than 4 characters, but by taking out the MAX from inside the square brackets, the compiler now calculates the correct size for us
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    10
    Quote Originally Posted by iMalc View Post
    Better yet, realise that MAX is defined as 5 and therefore neither example has a null-terminator. That's also not the best way to write a string.
    A better example would be:
    Code:
    int main()
    {
            char s[] = "-13572468";
            int val = atoi(s);
            printf("%d\n",val);
            return 0;
    }
    Yes I know it's longer than 4 characters, but by taking out the MAX from inside the square brackets, the compiler now calculates the correct size for us
    iMalc, Thank you.

    If my char s[] has a value as below:
    Code:
    char s[] = "-13572 468";
    I see that the value returned is
    Code:
    -13572
    I was expecting that the function is just going to skip all the white spaces, collect the integers and the sign and print the value returned as
    Code:
    -13572468
    based on my understanding of what isspace and isdigit are doing in the above atoi function code.

    Andrew

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    10
    Tim,
    Thank you.

    If we change isdigit to isalpha, and change the atoi function to something as below:
    Code:
        char atoi(char s[])
    {
            int i, n, sign;
            for (i = 0; isspace(s[i]); i++) /* skip white space */
                    ;
            sign = (s[i] == '-') ? -1 : 1;
            if (s[i] == '+' || s[i] == '-') /* skip sign */
                    i++;
            for (i = 0; isalpha(s[i]); i++) {
            //return sign * n ;
                    return n;
            }
    }
    Here is the complete code I am using...really just a modification of your solution:
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    #define MAX 5
    
    char atoi(char a[]);
    
    int main()
    {
            char val;
            char s[MAX] = {'+', '1', '0','0',' '};
    //      char s[MAX] = {'b', ' ', 'b',' ',' '};
            val = atoi(s);
            printf("%c\n",val);
            return 0;
    }
    
    /* atoi: convert s to integer; version 2 */
    char atoi(char s[])
    {
            int i, n, sign;
            for (i = 0; isspace(s[i]); i++) /* skip white space */
                    ;
            sign = (s[i] == '-') ? -1 : 1;
            if (s[i] == '+' || s[i] == '-') /* skip sign */
                    i++;
            for (i = 0; isalpha(s[i]); i++) {
                    return n;
            }
    }
    Andrew

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You've changed the string representation back again
    As I noted, you should remove MAX from inside the brackets and also specify the string like this "+100 " it's a far shorter way of representing the exact same thing and it correctly null-terminates the string.

    The reason that a space in the middle stops it from parsing the rest of the digits is that the code only skips whitespace at the start of the string. See the first loop... that code runs first and once it's skipped along past any spaces at the start then it goes on to do the other stuff which has no code in it for skipping whitespace. Therefore as an example you could use:
    Code:
            char s[] = "    +12345  6 78";
    You will get 12345 as a result. This is exactly how it should work and how we want it to behave.

    As for using isalpha instead and then returning an uninitialised variable, that's not going to do anything remotely useful. Your compiler should be telling you one or more of the bugs you have introduced such as that uninitialised variable. If not then you need to enable the proper warnings.
    I just don't understand why you broke a perfectly correct and working version of the atoi function
    Last edited by iMalc; 12-31-2011 at 12:13 AM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The atoi function
    By jamesallen4u in forum C Programming
    Replies: 6
    Last Post: 11-30-2011, 07:19 PM
  2. complete function definition i.e. atoi
    By c_lady in forum Linux Programming
    Replies: 11
    Last Post: 03-31-2010, 12:28 PM
  3. What is the negative effect for atoi function ?
    By How2BecomePro in forum C Programming
    Replies: 5
    Last Post: 06-25-2009, 09:43 AM
  4. C++.NET Can I use atoi ??
    By Swaine777 in forum C++ Programming
    Replies: 3
    Last Post: 09-18-2004, 06:54 PM
  5. atoi?
    By mayfda in forum C++ Programming
    Replies: 6
    Last Post: 11-19-2002, 09:45 AM