Thread: atoi() for char

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    23

    atoi() for char

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main()
    {
    
       char *status;
       char buffer[1024];
       FILE *fp;
       char answer[50];
       int i = 0;
       int total = 0;
       int converter = 0;
       char ans[50];
     
       fp = fopen("trial.txt","r");
       status = fgets(buffer,sizeof(buffer),fp);
       strcpy(answer,status);	
    
    	for(i = 0; i < strlen(answer) - 1;i++)
    	   {
    	       if(i % 2 == 0)
    		{
    		   printf("%c",answer[i]);
    		   converter = atoi(&answer[i]);
    		   printf("%d\n",converter);
    		}
    	   }
    status = fgets(buffer,sizeof(buffer),fp);
    fclose(fp);
    getchar();
    return 0;
    
      
    }
    I am reading over the web about how atoi() only takes strings because it has a null terminator in the end. the problem is that, I didn't get much of a solution on how to fix when I am converting a single char. I read about putting - '0', but I really don't know where.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why not try "in place of the atoi"?

    Or alternatively, read up on ASCII.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    23
    Quote Originally Posted by tabstop View Post
    Why not try "in place of the atoi"?

    Or alternatively, read up on ASCII.
    I'll read up on ASCII,

    bt if you mean in place of the atoi,

    converter = atoi(&answer[i] - '0');
    it, all gave me 0.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That would be in addition to, not in place of. In place of means that atoi does not appear anymore, having been replaced by subtracting the '0'.

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    23
    Quote Originally Posted by tabstop View Post
    That would be in addition to, not in place of. In place of means that atoi does not appear anymore, having been replaced by subtracting the '0'.
    oh...it works..O_O

    puzzling indeed.

    uh..thanks..

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Write your own for fun.
    Code:
    int ctoi( char c )
    {
        return c=='0'?0:c=='1'?1:c=='2'?2:c=='3'?3:c=='4'?4:
               c=='5'?5:c=='6'?6:c=='7'?7:c=='8'?8:c=='9'?9:-1;
    }
    Yay! What else can we do ...
    Code:
    int ctoi( char c )
    {
        int r = 10;
        switch( c )
        {
            case '0': r--; case '1': r--; case '2': r--;
            case '3': r--; case '4': r--; case '5': r--;
            case '6': r--; case '7': r--; case '8': r--;
            case '9': r--; break;
            default:  r=-1;
        }
        return r;
    }
    I really didn't want to make default just implicitly set the value. You could use a lookup table of your own creation.

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

  7. #7
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    puzzling indeed.
    In ASCII, '0' through '9' are contiguous and in order, so subtracting '0' (which the preprocessor interprets as (char) 48) means that for your character

    '0' - '0' = 0
    '1' - '0' = 1
    etc.

    EDIT: The big idea is that chars are just numbers and have no special meaning (besides mapping those numbers to glyphs to print to the screen)
    Last edited by bernt; 04-28-2010 at 06:37 PM.
    Consider this post signed

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. complete function definition i.e. atoi
    By c_lady in forum Linux Programming
    Replies: 11
    Last Post: 03-31-2010, 12:28 PM
  2. Have I understood the atoi funtion right?
    By austra in forum C Programming
    Replies: 5
    Last Post: 11-13-2009, 09:15 AM
  3. Having trouble with atoi
    By NewbGuy in forum C Programming
    Replies: 2
    Last Post: 05-22-2009, 11:55 PM
  4. help with this
    By tyrantil in forum C Programming
    Replies: 18
    Last Post: 01-30-2005, 04:53 PM
  5. string to int conversion not using atoi()
    By linucksrox in forum C Programming
    Replies: 2
    Last Post: 05-19-2004, 12:17 AM