atoi does it work like this

This is a discussion on atoi does it work like this within the C Programming forums, part of the General Programming Boards category; Hi guys, Does atoi work like this: it scans an array element by element 0=49 1=50 2=51 converts these ascii ...

  1. #1
    Unregistered
    Guest

    Arrow atoi does it work like this

    Hi guys,
    Does atoi work like this: it scans an array element by element 0=49 1=50 2=51 converts these ascii values 49 50 and 51 into there digit equivalents and add each one to an identifier/varriable.

    Thanks learner(wanting to be a master).

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No. It doesn't.

    x = atoi( "32" );
    printf("%d", x ); /* this prints 32 */

    atoi( ) takes a string of numbers and translatest it directly into the integer value of the string, as shown above.

    Quzah.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Okay, let's say this is the program...
    Code:
    #include <stdlib.h>
    main ()
    {
     char a[4];
     int i;
     
     a = {49, 50, 51, 0};
     // 49 = '1', 50 = '2', 51 = '3', 0 = '\0'
     // So a is the string "123"
     i = atoi (a)
     // Now i is the integer value 123
     return;
    }
    The 0 is very important, as atoi is meant for reading a 0 terminated character string. That is to say, if instead I used a[3] = {49, 50, 51}, then atoi(a) would have just kept reading for integer values in the memory past a[3] because it hadn't yet encounted the string terminator... '\0'.

  4. #4
    Unregistered
    Guest
    Originally posted by QuestionC
    Okay, let's say this is the program...
    Code:
    #include <stdlib.h>
    main ()
    {
     char a[4];
     int i;
     
     a = {49, 50, 51, 0};
     // 49 = '1', 50 = '2', 51 = '3', 0 = '\0'
     // So a is the string "123"
     i = atoi (a)
     // Now i is the integer value 123
     return;
    }
    The 0 is very important, as atoi is meant for reading a 0 terminated character string. That is to say, if instead I used a[3] = {49, 50, 51}, then atoi(a) would have just kept reading for integer values in the memory past a[3] because it hadn't yet encounted the string terminator... '\0'.
    a = {49, .....}???
    you try to compile this?
    probably not!
    any way this is not the meaning of atoi(ascii to integer),

    void main(void)
    {
    char *a;

    a = "49";
    printf("%d",atoi(a));
    }


    output:

    49 press any key to continue...

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Sorry, my post was intended to just get the spirit of atoi across as code, not as a program. Here's the code, fixed up to compile, and producting some output.
    Code:
    #include <stdlib.h>
    main ()
    {
     char a[4] = {49, 50, 51, 0};
     int i;
     
     printf ("a represented as a string is... %s\n", a);
     printf ("a represented as an int is... %d\n", a);
    
     // 49 = '1', 50 = '2', 51 = '3', 0 = '\0'
     // So a is the string "123"
    
     i = atoi (a);
     printf ("atoi(a) as an int is... %d\n", i);
     printf ("atoi(a) as a string is... %s\n", i);
    
     // Now i is the integer value 123
     return;
    }
    Looking at your code... well, here's the thing...
    Code:
    char *a = "49";
    is equivalent (kinda) to
    Code:
    a[3] = {52, 57, 0};
    and that's pretty much the difference between my code and yours. 52 is the ASCII value for '4', 57 is the ASCII value for '9', and 0 is the string terminator. Similarly
    Code:
    char a[4] = {49, 50, 51, 0};
    is the same as
    Code:
    char * a = "123"


    *Note to self... use atoi and pointer arithmatic to perform divisions in next ofuscated code contest*

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > return;

    Just to nit pick...you forgot to return a value!

    Quzah.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. Problems in getting OpenGL to work
    By zonf in forum C Programming
    Replies: 5
    Last Post: 02-13-2006, 03:48 AM
  5. Why won't my OpenGL work?
    By Raigne in forum C++ Programming
    Replies: 7
    Last Post: 11-26-2005, 10:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21