integer to char table problems

A

AA

I am trying to print a table of what integer corresponds to what
character. Can someone please tell me what is wrong with this code?

#include <stdio.h>


int main()
{

char line[9]; /* each word in the wordlist */
char d;

for (d=0; d<=255; d++)
printf("int=%d\tchar=%c\n",d,d);
}


Thanks in advance!
 
P

pete

AA said:
I am trying to print a table of what integer corresponds to what
character. Can someone please tell me what is wrong with this code?

#include <stdio.h>

int main()
{

char line[9]; /* each word in the wordlist */
char d;

for (d=0; d<=255; d++)
printf("int=%d\tchar=%c\n",d,d);
}

You don't know if type char can ever reach 255.


#include <stdio.h>
#include <limits.h>

int main(void)
{
int d;

for (d=0; d <= CHAR_MAX; d++) {
printf("int=%d char=%c\n", d, d);
}
return 0;
}
 
D

Derrick Coetzee

pete said:
printf("int=%d char=%c\n", d, d);

It may also be a little bit subtle that printf's variadic arguments are
subject to the default promotions - that is, printf doesn't care whether
you pass a char, short, or int for either a %d or %c argument; it only
affects how the value is displayed (although the loop certainly cares,
as pete points out.)

As a confusing aside, on platforms that define NULL as 0 you could also
pass NULL for a %d or %c argument, and it would cause no undefined
behaviour, being treated as the int zero.
 
S

Spacen Jasset

AA said:
I am trying to print a table of what integer corresponds to what
character. Can someone please tell me what is wrong with this code?

#include <stdio.h>


int main()
{

char line[9]; /* each word in the wordlist */
char d;

for (d=0; d<=255; d++)
printf("int=%d\tchar=%c\n",d,d);
}


Thanks in advance!

Since a char on your platform probaly only has the range 0 - 255 or for
signed char, (-128) - (127) you encounter a problem because 255 + 1 = 0 so d
will never be more than 255 and hednce the loop might not ever stop.

try:

#include <stdio.h>

int main()
{
int d;

for (d=0; d<=255; d++)
printf("int=%d\tchar=%c\n",d,d);
getchar();
return 0;
}
 
M

Martin Ambuhl

AA said:
I am trying to print a table of what integer corresponds to what
character. Can someone please tell me what is wrong with this code? [...]
char d;

for (d=0; d<=255; d++)

If char is unsigned and UCHAR_MAX == 255, this loop will never terminate.
 
C

CBFalconer

pete said:
I am trying to print a table of what integer corresponds to what
character. Can someone please tell me what is wrong with this code?

#include <stdio.h>

int main()
{

char line[9]; /* each word in the wordlist */
char d;

for (d=0; d<=255; d++)
printf("int=%d\tchar=%c\n",d,d);
}

You don't know if type char can ever reach 255.

#include <stdio.h>
#include <limits.h>

int main(void)
{
int d;

for (d=0; d <= CHAR_MAX; d++) {
printf("int=%d char=%c\n", d, d);
}
return 0;
}

In addition not all chars are printable. To fix, guard the printf
statement with "if (isprint(d))" after including <ctype.h>.
 
K

Keith Thompson

Martin Ambuhl said:
AA said:
I am trying to print a table of what integer corresponds to what
character. Can someone please tell me what is wrong with this code? [...]
char d;
for (d=0; d<=255; d++)

If char is unsigned and UCHAR_MAX == 255, this loop will never terminate.

Also, if char is signed and CHAR_MAX == 127, this loop will probably
never terminate. ("probably" because signed overflow invokes
undefined behavior, but it commonly wraps around to a negative value.)
 
A

AA

pete said:
AA said:
I am trying to print a table of what integer corresponds to what
character. Can someone please tell me what is wrong with this code?

#include <stdio.h>

int main()
{

char line[9]; /* each word in the wordlist */
char d;

for (d=0; d<=255; d++)
printf("int=%d\tchar=%c\n",d,d);
}


You don't know if type char can ever reach 255.


#include <stdio.h>
#include <limits.h>

int main(void)
{
int d;

for (d=0; d <= CHAR_MAX; d++) {
printf("int=%d char=%c\n", d, d);
}
return 0;
}

Thanks to everyone who replied. You guys are right, and in fact gcc
won't let me compile it until I change the above loop bounds to read:
for (d=0; d <= (CHAR_MAX-1); d++) {

This value (CHAR_MAX-1) is 127 on my machine. That's a bit confusing
because my Osbourne book "The Complete Reference, C 4th ed." says that
printable characters range from 0x20 to 0xFE but they clearly don't go
that high, or am I missing something?

Special thanks to the "isprint()" tip!
 
M

Michael Mair

Hi there,
This value (CHAR_MAX-1) is 127 on my machine. That's a bit confusing

CHAR_MAX *must* be at least 127, so you probably mean 126.

because my Osbourne book "The Complete Reference, C 4th ed." says that
printable characters range from 0x20 to 0xFE but they clearly don't go
that high, or am I missing something?

This is system specific information and not true in this form.
However, the printable characters may indeed have this representation.
Let's assume that CHAR_BIT, the number of bits per byte, equals 8
and that negative values are represented in two's complement, that is:
Bit 7 means that it is a negative number (every signed char c with
(unsigned char)c & 0x80 != 0 is negative) and every char c which, as
an unsigned char, would be >=128 is wrapped to (unsigned char)c -255.

That means that a part of your printable characters might be between
-128 and -1. I would try to create code that first finds out from
where to where char runs and then go either through the whole range
in one go or, in your case, first through the positive range and then
through the negative range (starting from CHAR_MIN).


HTH
Michael
 
A

AA

AA said:
I am trying to print a table of what integer corresponds to what
character. Can someone please tell me what is wrong with this code?

#include <stdio.h>


int main()
{

char line[9]; /* each word in the wordlist */
char d;

for (d=0; d<=255; d++)
printf("int=%d\tchar=%c\n",d,d);
}


Thanks in advance!

Thanks again to everyone who helped. Now I have one more question. I
am trying to produce a list of all possible combinations of 8 printable
characters. Is there a more elegant or efficient way to do this than 8
nested for loops? I feel there must be but I haven't done any real
programming for so long that nothing is coming to me.

Thanks.
 
B

Brett Frankenberger

Thanks again to everyone who helped. Now I have one more question. I
am trying to produce a list of all possible combinations of 8 printable
characters. Is there a more elegant or efficient way to do this than 8
nested for loops? I feel there must be but I haven't done any real
programming for so long that nothing is coming to me.

Even assuming a mere 64 printable characters, all combinations of 8
printable characters is 64^8, which is 2^48, which is 281474976710656,
or 281 trillion. Assuming you can print a million combinations a
second, you will need about 9 years to get them all printed out.

8 for loops is as good a way as any to do it.

-- Brett
 
D

Derrick Coetzee

AA said:
Thanks again to everyone who helped. Now I have one more question. I
am trying to produce a list of all possible combinations of 8 printable
characters. Is there a more elegant or efficient way to do this than 8
nested for loops? I feel there must be but I haven't done any real
programming for so long that nothing is coming to me.

Try recursion. Keep in mind that such a list is very, very, very long
(roughly 100 quadrillion strings; no disk could hold them all.)
 
K

Keith Thompson

AA said:
Thanks to everyone who replied. You guys are right, and in fact gcc
won't let me compile it until I change the above loop bounds to read:
for (d=0; d <= (CHAR_MAX-1); d++) {

This value (CHAR_MAX-1) is 127 on my machine. That's a bit confusing
because my Osbourne book "The Complete Reference, C 4th ed." says that
printable characters range from 0x20 to 0xFE but they clearly don't go
that high, or am I missing something?

As someone else pointed out, CHAR_MAX has to be at least 127, so
CHAR_MAX-1 must be at least 128.

The book _C: The Complete Reference_ is written by Herbert Schildt,
who is notorious for writing engaging books full of dangerous
nonsense. (See <http://www.lysator.liu.se/c/schildt.html> for a
scathing review of another of Schildt's books.) Get yourself a copy
of Kernighan & Ritchie's _The C Programming Language_, 2nd edition.
 
K

Keith Thompson

Guillaume said:
How could that be?

It can't. I goofed. Sorry.

CHAR_MAX must be at least 127, so CHAR_MAX-1 must be at least 126.
The previous article said that (CHAR_MAX-1) is 127, which is unlikely
(probably just a typo). (I think I could argue that CHAR_MAX can't
legally be 128, but if I can't reliably distinguish between '+' and
'-' I'm not going to try it.)
 
S

Spacen Jasset

AA said:
This value (CHAR_MAX-1) is 127 on my machine. That's a bit confusing
because my Osbourne book "The Complete Reference, C 4th ed." says that
printable characters range from 0x20 to 0xFE but they clearly don't go
that high, or am I missing something?

You can print some of the characters above 127 but they are no longer in the
ascii set. They are OEM or 'extended-ascii' characters and can change on
different machines.
 
P

pete

Derrick Coetzee wrote:
As a confusing aside, on platforms that define
NULL as 0 you could also pass NULL for a %d or %c argument,
and it would cause no undefined behaviour,
being treated as the int zero.

As well as (void *)0,
there are other definitions that might cause problems for %d.
NULL could also be something like 0L.
 

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

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
EmeliaBryc

Latest Threads

Top