www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/03/01/23:11:55

From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: char to int
Date: Sat, 01 Mar 1997 22:15:41 -0800
Organization: Two pounds of chaos and a pinch of salt
Lines: 25
Message-ID: <33191B0D.4025@cs.com>
References: <Pine DOT SV4 DOT 3 DOT 94 DOT 970301143114 DOT 733A-100000 AT aludra DOT usc DOT edu>
Reply-To: fighteer AT cs DOT com
NNTP-Posting-Host: ppp206.cs.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

rellwood wrote:
> 
> Is there a function I can use to convert a char to an int?  The closest
> thing I can find is atoi(), which actually converts a string.  Is there
> anything better?

I'm not sure what you mean.  If you want an individual character, then
why not just use (c - '0'), as in the following:

int getdigit( void )
{
    int c;
    printf( "Type a digit:  " );
    fflush( stdout );
    while ( ( c = getchar( ) ) < '0' || c > '9' )
        printf( "Please type a digit.\n" );
    return c - '0';
}

-- 
---------------------------------------------------------------------
| John M. Aldrich, aka Fighteer I |        fighteer AT cs DOT com          |
| Descent 2: The Infinite Abyss - The greatest Internet game of all |
| time just got better!  This time, you're going all the way down...|
---------------------------------------------------------------------

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019