integer to char table problems

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • AA

    integer to char table problems

    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!
  • pete

    #2
    Re: integer to char table problems

    AA wrote:[color=blue]
    >
    > 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 );
    > }[/color]

    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

    Comment

    • Derrick Coetzee

      #3
      Re: integer to char table problems

      pete wrote:[color=blue]
      > printf("int=%d char=%c\n", d, d);[/color]

      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.

      Comment

      • Spacen Jasset

        #4
        Re: integer to char table problems


        "AA" <AA@example.com > wrote in message news:Ill5d.8340 $3n.2138@okepre ad06...[color=blue]
        > 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![/color]

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



        Comment

        • Martin Ambuhl

          #5
          Re: integer to char table problems

          AA wrote:[color=blue]
          > 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?[/color]
          [...][color=blue]
          > char d;
          >
          > for (d=0; d<=255; d++)[/color]

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

          Comment

          • CBFalconer

            #6
            Re: integer to char table problems

            pete wrote:[color=blue]
            >
            > AA wrote:[color=green]
            > >
            > > 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 );
            > > }[/color]
            >
            > 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;
            > }[/color]

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

            --
            Chuck F (cbfalconer@yah oo.com) (cbfalconer@wor ldnet.att.net)
            Available for consulting/temporary embedded and systems.
            <http://cbfalconer.home .att.net> USE worldnet address!


            Comment

            • Keith Thompson

              #7
              Re: integer to char table problems

              Martin Ambuhl <mambuhl@earthl ink.net> writes:[color=blue]
              > AA wrote:[color=green]
              >> 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?[/color]
              > [...][color=green]
              >> char d;
              >> for (d=0; d<=255; d++)[/color]
              >
              > If char is unsigned and UCHAR_MAX == 255, this loop will never terminate.[/color]

              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_Keit h) kst-u@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.

              Comment

              • AA

                #8
                Re: integer to char table problems

                pete wrote:[color=blue]
                > AA wrote:
                >[color=green]
                >>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 );
                >>}[/color]
                >
                >
                > 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;
                > }
                >[/color]

                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!

                Comment

                • Michael Mair

                  #9
                  Re: integer to char table problems

                  Hi there,
                  [color=blue]
                  > This value (CHAR_MAX-1) is 127 on my machine. That's a bit confusing[/color]

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

                  [color=blue]
                  > 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?[/color]

                  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

                  Comment

                  • AA

                    #10
                    Re: integer to char table problems

                    AA wrote:[color=blue]
                    > 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![/color]

                    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.

                    Comment

                    • Brett Frankenberger

                      #11
                      Re: integer to char table problems

                      In article <AzB5d.14232$3n .7524@okepread0 6>, AA <AA@example.com > wrote:[color=blue]
                      >
                      >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.[/color]

                      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

                      Comment

                      • Derrick Coetzee

                        #12
                        Re: integer to char table problems

                        AA wrote:[color=blue]
                        > 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.[/color]

                        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.

                        Comment

                        • Keith Thompson

                          #13
                          Re: integer to char table problems

                          AA <AA@example.com > writes:
                          [...][color=blue]
                          > 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?[/color]

                          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_Keit h) kst-u@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.

                          Comment

                          • Guillaume

                            #14
                            Re: integer to char table problems

                            Keith Thompson wrote:[color=blue]
                            > As someone else pointed out, CHAR_MAX has to be at least 127, so
                            > CHAR_MAX-1 must be at least 128.[/color]

                            How could that be?

                            Comment

                            • Keith Thompson

                              #15
                              Re: integer to char table problems

                              Guillaume <"grsNOSPAM at NOTTHATmail dot com"> writes:[color=blue]
                              > Keith Thompson wrote:[color=green]
                              >> As someone else pointed out, CHAR_MAX has to be at least 127, so
                              >> CHAR_MAX-1 must be at least 128.[/color]
                              >
                              > How could that be?[/color]

                              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_Keit h) kst-u@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.

                              Comment

                              Working...