How standard atoi works?

Discussion in 'C Programming' started by jigsaw@gmail.com, Jan 15, 2006.

  1. Guest

    hi all,

    Hereunder two versions of reading a word from stdin stream:

    /* code starts */
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>

    char *
    getnumber1(void); /* return a word from stdin */

    void
    getnumber2(char *); /* read a word from stdin and store it in the char*
    */

    int main()
    {
    int i = 0;
    char w[20];

    i = atoi(getnumber1());
    printf("getnumber1 = %d\n", i);
    i = 0;
    getnumber2(w);
    i = atoi(w);
    printf("getnumber2 = [%s]\n", w);
    printf("getnumber2 = %d\n", i);
    return 0;
    }

    char *
    getnumber1(void)
    {
    char w[20];
    char *p;
    int c;

    p = w;
    while (isspace(c = getchar()))
    ;
    if (c != EOF)
    *p++ = c;
    for (;;) {
    if (isspace(c = getchar())) {
    ungetc(c, stdin);
    break;
    }
    *p++ = c;
    }
    *p = '\0';
    printf("getnumber1 = [%s]\n", w);
    return w;
    }

    void
    getnumber2(char *w)
    {
    char *p;
    int c;

    p = w;
    while (isspace(c = getchar()))
    ;
    if (c != EOF)
    *p++ = c;
    for (;;) {
    if (isspace(c = getchar())) {
    ungetc(c, stdin);
    break;
    }
    *p++ = c;
    }
    *p = '\0';
    return;
    }
    /* code ends */

    I use bc++5.5 as compiler.
    the input and output of this small program is like this:

    D:\c\scan>getnum
    12
    getnumber1 = [12]
    getnumber1 = 1
    12
    getnumber2 = [12]
    getnumber2 = 12

    You can see that results of getnumber1 and getnumber2 are the same.
    (12)
    but the output of atoi are diffrent.
    Can anyone do me a favor to explain the diffrence?

    Thanx and
    Regards,

    jigsaw
     
    , Jan 15, 2006
    #1
    1. Advertising

  2. In article <>,
    <> wrote:
    >int main()
    >{
    > int i = 0;
    > char w[20];


    > i = atoi(getnumber1());


    >char *
    >getnumber1(void)
    >{
    > char w[20];
    > char *p;
    > int c;


    > p = w;


    > return w;


    When you return a char[n], the result is not that the character string
    contents are somehow transfered back: the results are that the
    address of the char[n] array is transferred back. Unfortunately for
    you, char w[20] in this context is a local variable of automatic
    duration, so its address is not valid after the return of the routine
    it is created in. You are thus attempting to employ undefined
    behaviour.
    --
    Prototypes are supertypes of their clones. -- maplesoft
     
    Walter Roberson, Jan 15, 2006
    #2
    1. Advertising

  3. jigsaw Guest

    Got it. Thank you Roberson.

    Thanx and
    Regards,

    jigsaw
     
    jigsaw, Jan 16, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lonelyplanet999
    Replies:
    8
    Views:
    43,253
  2. Magig Boatman
    Replies:
    1
    Views:
    474
    Russell Hanneken
    Jul 11, 2003
  3. Mike Chirico
    Replies:
    2
    Views:
    3,853
    Grumble
    Nov 19, 2003
  4. Christopher Benson-Manica

    atoi()

    Christopher Benson-Manica, Jan 15, 2004, in forum: C++
    Replies:
    16
    Views:
    11,195
    Jeff Schwab
    Jan 18, 2004
  5. Neil Vice

    Re: atoi - please help

    Neil Vice, Apr 1, 2004, in forum: C++
    Replies:
    0
    Views:
    363
    Neil Vice
    Apr 1, 2004
Loading...

Share This Page