Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Problem with atoi()

Reply
Thread Tools

Problem with atoi()

 
 
Kenneth Brody
Guest
Posts: n/a
 
      09-10-2007
"Douglas A. Gwyn" wrote:
>
> "André Gillibert" wrote:
> > > 5.2.1p3: the 10 decimal digits 0 1 2 3 4 5 6 7 8 9

> > They're "contiguous" on ASCII or BCDIC based character sets, but on other
> > character sets, they may not be.
> > ...
> > That doesn't change the fact that ch - '0' is not guaranteed to portably
> > work.

>
> Actually the C standard does require the digit codes to be contiguous
> and ascending, so it is guaranteed to work on any conforming C
> implementation.
>
> I argued against this requirement, which infringes on the codeset
> designer's turf, but lost the argument. (I would rather specify
> macros/functions to convert between digit characters and
> corresponding numeric interpretations.)


Baudot does not have the digits as contiguous values. But, then
again, it is also a 6 bit character set, whereas C specifies a
minimum of 8.

I can imagine a hardware device which uses something like the
7-segment LED displays as the character values. (ie: which bits
need to be set on in order to display the number 4?) I guess
any conforming C compiler would need to include a translation
layer in the I/O library on such a platform.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <(E-Mail Removed)>

 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      09-10-2007
Kenneth Brody <(E-Mail Removed)> writes:
> "Douglas A. Gwyn" wrote:
>>
>> "André Gillibert" wrote:
>> > > 5.2.1p3: the 10 decimal digits 0 1 2 3 4 5 6 7 8 9
>> > They're "contiguous" on ASCII or BCDIC based character sets, but on other
>> > character sets, they may not be.
>> > ...
>> > That doesn't change the fact that ch - '0' is not guaranteed to portably
>> > work.

>>
>> Actually the C standard does require the digit codes to be contiguous
>> and ascending, so it is guaranteed to work on any conforming C
>> implementation.
>>
>> I argued against this requirement, which infringes on the codeset
>> designer's turf, but lost the argument. (I would rather specify
>> macros/functions to convert between digit characters and
>> corresponding numeric interpretations.)

>
> Baudot does not have the digits as contiguous values. But, then
> again, it is also a 6 bit character set, whereas C specifies a
> minimum of 8.


More relevantly, C requires at least 7 bits to represent the basic
character set. A Baudot-based implementation could use 6 bits of an
8-bit byte to represent the character code, just as an ASCII-based
system can use 7 bits of an 8-bit byte. The Baudot-based
implementation would be non-conforming because it can't represent
every member of the basic character set.

> I can imagine a hardware device which uses something like the
> 7-segment LED displays as the character values. (ie: which bits
> need to be set on in order to display the number 4?) I guess
> any conforming C compiler would need to include a translation
> layer in the I/O library on such a platform.


As a practical matter, you can't get a legible representation of each
of the required printable characters i a 7-segment LED display, though
you could encode each character.

When C was standardized, there happened to be no existing character
sets that (a) could support C's other requirements and (b) had
non-contiguous representations for '0'..'9', and it's highly unlikely
that there will be such a character set in the forseeable future. On
the other hand, I can see Doug's point just as a consistency argument;
it happened to be *possible* for C to require contiguity of '0'..'9',
but it wasn't *necessary*.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
 
Charles Richmond
Guest
Posts: n/a
 
      09-10-2007
tolkien wrote:
> i have this matrix:
> char matrix[3][3]={'3','4','5',
> '6','7','8',
> '1','2','3' };
>
> and i want to use one of its elements as integer .
> for example int x= matrix[1][2] (
>


If you *have* to use atoi(), declare matrix as:

int x;

char *matrix[3][3] = {"3","4","5",
"6","7","8",
"1","2","3"};


Then the following will work:

x = atoi(matrix[2][2]);


--
+----------------------------------------------------------------+
| Charles and Francis Richmond richmond at plano dot net |
+----------------------------------------------------------------+
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      09-11-2007
"André Gillibert" wrote:
> Charlie Gordon wrote:
>

.... snip ...
>>
>> Really ? I don't think so:
>>
>> 7.4.1.5p2: The isdigit function tests for any decimal-digit
>> character (as defined in 5.2.1).
>>
>> 5.2.1p3: the 10 decimal digits 0 1 2 3 4 5 6 7 8 9

>
> They're "contiguous" on ASCII or BCDIC based character sets, but
> on other character sets, they may not be.


Then they are not the 'decimal digits' referred to in the
standard. Simple. If you have some unusual chars you have to
translate them first. If you have unusual bases you have to write
the appropriate code. etc.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
Army1987
Guest
Posts: n/a
 
      09-11-2007
On Mon, 10 Sep 2007 13:28:14 -0700, Keith Thompson wrote:

> As a practical matter, you can't get a legible representation of each
> of the required printable characters i a 7-segment LED display, though
> you could encode each character.

In such an hypothetical implementation, putchar()ing any character
which can't be sensibly displayed on such a display could always
return EOF and set the ferror() flag on.
--
Army1987 (Replace "NOSPAM" with "email")
If you're sending e-mail from a Windows machine, turn off Microsoft's
stupid “Smart Quotes” feature. This is so you'll avoid sprinkling garbage
characters through your mail. -- Eric S. Raymond and Rick Moen

 
Reply With Quote
 
David Thompson
Guest
Posts: n/a
 
      09-23-2007
On Mon, 10 Sep 2007 15:15:35 -0400, Kenneth Brody
<(E-Mail Removed)> wrote:

> "Douglas A. Gwyn" wrote:


> > Actually the C standard does require the digit codes to be contiguous
> > and ascending, <snip>
> > I argued against this requirement, which infringes on the codeset
> > designer's turf, but lost the argument. <snip>

>
> Baudot does not have the digits as contiguous values. But, then
> again, it is also a 6 bit character set, whereas C specifies a
> minimum of 8.
>

s/6/5/. And Baudot/IA2 is also shift-state dependent, which doesn't
fit in well with C's model, and tradition, of 'you can access any char
to which you have a (valid) pointer'.

FWIW, TeleTypeSetter is 6-bit, also shift-state dependent (actually
multiple states in many usages), and discontiguous -- because, like
Baudot/IA2, it was designed to minimize 'marks' for average text. I
know this reduced aggregate wear on (re)perferators; I never got a
straight answer on whether it saved anything significant in
power/battery use and/or cable (and perhaps cableway) heating.

- formerly david.thompson1 || achar(64) || worldnet.att.net
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem problem problem :( Need Help Mike ASP General 2 05-11-2004 08:36 AM



Advertisments