Thread: cannot understand this output - atoi

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

    cannot understand this output - atoi

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    #include<stdlib.h>
    
    int main(void)
    {
            char arr[50],a;
            char *ar;
            int x;
            
            printf("GETS --> ");
            ar = gets(arr);
    
            printf("PUTS --> ");
            puts(arr);
    
            sscanf(arr,"%c",&a);
            printf("PRINTF --> ");
            printf("%c\n",a);
    
            printf("ATOI --> ");
            x = (int) atoi(&a);
            printf("%d\n",x);
    
            getch();
    }
    OUTPUT

    GETS --> 2
    PUTS --> 2
    PRINTF --> 2
    ATOI --> 22 ? It should be 2
    Last edited by Salem; 10-02-2004 at 04:18 AM. Reason: CODE TAGS PEOPLE!!!!!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    The "READ THIS BEFORE POSTING" really needs to be made more prominent, because no-one bothers to read it (or the FAQ)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    atoi() expects a string - a sequence of chars ending in \0
    A single char cannot be that, so your call to atoi() is wrong - simple arranging for the correct type of parameter to be passed isn't enough.

    atoi(arr);
    would be better

    Using strtol() would be even better - then you can add error checking as well

    Plus you use gets(), only the world's most evil function.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    30

    atoi

    in my case i am to read an input line and it could contain anything so i use gets() to read the line into string.

    then i use sscanf to read each character from this string and determine whether it is a number or not.

    if it is a number i need to convert the character to int.
    hence i have used atoi.
    Syra
    Amateur's urge to master C/C++

  5. #5
    UNIX chick
    Join Date
    Mar 2003
    Posts
    92
    This program won't compile for me (due to conio.h), but remember to include a return status for main. Your code is, unfortunately, not very portable, since *NIX systems needs curses.h for getch().

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > in my case i am to read an input line and it could contain anything so i use gets() to read the line into string.
    Then use fgets() to read a line.
    This is in the FAQ.

    > then i use sscanf to read each character from this string and determine whether it is a number or not.
    Except you don't validate anything, you just copy one character.

    > if it is a number i need to convert the character to int.
    > hence i have used atoi.
    strtol does the same, only better
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    UNIX chick
    Join Date
    Mar 2003
    Posts
    92
    Quote Originally Posted by Salem
    > if it is a number i need to convert the character to int.
    > hence i have used atoi.
    strtol does the same, only better
    Isn't atoi() actually deprecated?

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Salem
    The "READ THIS BEFORE POSTING" really needs to be made more prominent, because no-one bothers to read it (or the FAQ)
    They should make it required reading before you can post. Have a quiz on it. I'm serious. That way, people could have the announcement open in one tab...er..."window"...and the quiz in another, and could answer it. This would basicly force them to read at least part of it.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Apr 2004
    Posts
    210
    Quote Originally Posted by quzah
    They should make it required reading before you can post. Have a quiz on it. I'm serious. That way, people could have the announcement open in one tab...er..."window"...and the quiz in another, and could answer it. This would basicly force them to read at least part of it.
    What happend to the "use code tags" message when trying to make a post containing "{" or "}"?

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > What happend to the "use code tags" message when trying to make a post containing "{" or "}"?
    It broke in the board upgrade
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Isn't atoi() actually deprecated?
    Sadly, no.
    My best code is written with the delete key.

  12. #12
    UNIX chick
    Join Date
    Mar 2003
    Posts
    92
    Prelude; In my man page is says: "The atoi() function has been deprecated by strtol() and should not be used in new code."

    But this may not be a standard, though, and perhaps also only written in the BSD version of the man page.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  2. execl()/fork() output
    By tadams in forum C Programming
    Replies: 19
    Last Post: 02-04-2009, 03:29 PM
  3. Replies: 4
    Last Post: 11-30-2005, 04:44 PM
  4. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM