Problem with atoi()

K

Kenneth Brody

Douglas A. Gwyn said:
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: <mailto:[email protected]>
 
K

Keith Thompson

Kenneth Brody said:
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*.
 
C

Charles Richmond

tolkien said:
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] (8)

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]);
 
C

CBFalconer

André Gillibert said:
Charlie Gordon wrote:
.... snip ...

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.
 
A

Army1987

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.
 
D

David Thompson

:

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

atoi 11
Print with command-line arguments 0
a question abou "atoi" 6
problematic atoi conversion 2
Problem with codewars. 5
A Minor Problem With atoi() And Negative Numbers 12
atoi query 13
integer overflow in atoi 29

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top