473,406 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

integer to char table problems

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!
Nov 14 '05 #1
16 2487
AA wrote:

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;
}

--
pete
Nov 14 '05 #2
pete wrote:
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.
--
Derrick Coetzee
I grant this newsgroup posting into the public domain. I disclaim all
express or implied warranty and all liability. I am not a professional.
Nov 14 '05 #3

"AA" <AA@example.com> wrote in message news:Ill5d.8340$3n.2138@okepread06...
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;
}

Nov 14 '05 #4
AA wrote:
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.
Nov 14 '05 #5
pete wrote:

AA wrote:

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

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #6
Martin Ambuhl <ma*****@earthlink.net> writes:
AA wrote:
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.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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.
Nov 14 '05 #7
AA
pete wrote:
AA wrote:
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!
Nov 14 '05 #8
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

Nov 14 '05 #9
AA
AA wrote:
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.
Nov 14 '05 #10
In article <AzB5d.14232$3n.7524@okepread06>, AA <AA@example.com> wrote:

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
Nov 14 '05 #11
AA wrote:
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.)
--
Derrick Coetzee
I grant this newsgroup posting into the public domain. I disclaim all
express or implied warranty and all liability. I am not a professional.
Nov 14 '05 #12
AA <AA@example.com> writes:
[...]
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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.
Nov 14 '05 #13
Keith Thompson wrote:
As someone else pointed out, CHAR_MAX has to be at least 127, so
CHAR_MAX-1 must be at least 128.


How could that be?
Nov 14 '05 #14
Guillaume <"grsNOSPAM at NOTTHATmail dot com"> writes:
Keith Thompson wrote:
As someone else pointed out, CHAR_MAX has to be at least 127, so
CHAR_MAX-1 must be at least 128.


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

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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.
Nov 14 '05 #15

"AA" <AA@example.com> wrote in message
news:Muz5d.35313$aW5.10600@fed1read07...

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.
Nov 14 '05 #16
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.

--
pete
Nov 14 '05 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: shailashri_sk | last post by:
Hi, int *p; p++; here p now increments itself with the size of integer. similarly, I wanted to know, how to declare an pointer to an array ( say array of integers) where in it we do a p++ it...
5
by: Nonoize | last post by:
Hi all, Really dumb question but I don't know how to resolve it. Looked in help and evry book I have. I have a table where the primary key was set as an Integer and its reached over 140K...
11
by: Frederick Gotham | last post by:
I'd like to discuss the use of signed integers types where unsigned integers types would suffice. A common example would be: #include <cassert> #include <cstddef> int...
40
by: Robert Seacord | last post by:
The CERT/CC has released a beta version of a secure integer library for the C Programming Language. The library is available for download from the CERT/CC Secure Coding Initiative web page at:...
13
by: Freaker85 | last post by:
Hello, I am new at programming in C and I am searching a manner to parse a string into an integer. I know how to do it in Java, but that doesn't work in C ;o) I searched the internet but I...
13
by: bwaichu | last post by:
Now, I read the faq, and it suggests using sprintf. However, I want to all ways know where the integer finishes in the string. Basically, I want to: nbr | other data But the other data all...
20
by: ML | last post by:
Integers are stored in tables using only 4 bytes. Is there a way in SQL to retrieve the value as it is actually stored, not converted back into the displayed number? For example, if I have...
13
by: Mantorok | last post by:
Hi Is it possible to convert 2 char values to an integer and be able to reverse it? I'm sure there must be some algorithm to achive this. Any ideas? Thanks Kev
5
by: pradeep | last post by:
Hello friends: I know some people here don't like to answer C++ questions, but I believe this is really about the underlying C code. Anyway I have posted as well to the other group someone...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.