Thread: plz explain a small function

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    32

    plz explain a small function

    //atoi: converts 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;
    }

    how the above func. works!

    thanx

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    You've been told before... use code tags.
    This has to be your homework. Make an effort.
    DavT
    -----------------------------------------------

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    21
    Why not use the atoi() function included in stdlib?? anyways..
    i'm not even sure if the above function works.

    but you could always play knowing that the ASCII code 48 is 0. substract 48 to every ASCII code that represents a number and knowing how many spaces to the left you could multiply by ten.

    EDIT: lapsus mentis.. sorry lol i just explained what it did without really looking at the function, i just thought it didn't work because of the parameter, i would write

    Code:
    atoi(char *s)
    Last edited by ROCKaMIC; 11-11-2004 at 09:25 AM. Reason: ...

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Debugging with printf:
    Code:
    #include <stdio.h>
    
    int atoi(char s[])
    {
       int i, n = 0;
       for ( i=0; s[i]>='0' && s[i]<='9'; i++ )
       {
          printf("10 * n = 10 * %4d = %5d, ", n, 10 * n);
          printf("s[%d] - '0' = '%c' - '0' = %d - %d = %d, ",
                     i,         s[i],       s[i], '0', s[i] - '0');
          n = 10 * n + (s[i] - '0');
          printf("n = %d\n", n);
       }
       return n;
    }
    
    int main ( void )
    {
       int result = atoi("12345");
       printf("result = %d\n", result);
       return 0;
    }
    
    /* my output
    10 * n = 10 *    0 =     0, s[0] - '0' = '1' - '0' = 49 - 48 = 1, n = 1
    10 * n = 10 *    1 =    10, s[1] - '0' = '2' - '0' = 50 - 48 = 2, n = 12
    10 * n = 10 *   12 =   120, s[2] - '0' = '3' - '0' = 51 - 48 = 3, n = 123
    10 * n = 10 *  123 =  1230, s[3] - '0' = '4' - '0' = 52 - 48 = 4, n = 1234
    10 * n = 10 * 1234 = 12340, s[4] - '0' = '5' - '0' = 53 - 48 = 5, n = 12345
    result = 12345
    */
    Last edited by Dave_Sinkula; 11-11-2004 at 09:25 AM. Reason: Added another printf after the calculation of n.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    32
    thanx a lot mate!
    code tags means complete code which can be run

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM