Re: OT: Requesting C advice

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, 2007-05-24 at 17:07 -0500, Mike McCarty wrote:
> Les wrote:
> > On Thu, 2007-05-24 at 11:38 +0100, Alan Cox wrote:
> > 
> >>>The DEC KAL10 stored 5 characters in a 36 bit word with one bit
> >>>left over for anything you wanted to do with it! It used 7 bit ASCII,
> >>>of course.
> >>
> >>Or 6 BCD characters or 4 8 (as 9) bit ASCII values. Ditto the Honeywell
> >>L66. Character width as a compile option.
> >>
> >>Be very glad that today you don't have to fight that kind of thing. Don't
> >>however assume in portable code that int is 32bits, long is 32bits,
> >>pointers are 32bits and so on. C99 has proper types when you need to be
> >>specific.
> >>
> >>Also be aware that some Linux ports have char as unsigned by default and
> >>others as signed. 
> > 
> > Hi, Alan,
> >     I haven't seen a signed char since the 80's.  I did run into it once
> > on I think a ColecoVision package (don't ask!).
> 
> $ cat char.c
> #include <stdio.h>
> 
> int     main(void) {
>      char    Chr;
> 
>      Chr = -1;
>      printf("%d\n",Chr);
>      return 0;
> }
> 
> $ gcc -o char char.c
> $ ./char
> -1
> $ uname -a
> Linux Presario-1 2.6.10-1.771_FC2 #1 Mon Mar 28 00:50:14 EST 2005 i686 
> i686 i386 GNU/Linux
> 
> So, on FC2, char is signed by default. I suspect that to be true
> for all the Fedora Core releases.
> 
> >     That would really mess up any algorithm parsing into memory, and
> > would make dealing with solving some kinds of memory issues very
> > difficult, I would think, although
> 
> I suspect you are conflating "signed char" with "a character in the
> execution environment which, when its code is stored into a signed char,
> results in a negative value."
> 
> Mike
> -- 
> p="p=%c%s%c;main(){printf(p,34,p,34);}";main(){printf(p,34,p,34);}
> Oppose globalization and One World Governments like the UN.
> This message made from 100% recycled bits.
> You have found the bank of Larn.
> I can explain it for you, but I can't understand it for you.
> I speak only for myself, and I am unanimous in that!
> 
Hi, Mike,
    when you passed the char to the printf using %d, it was promoted to
an int which is signed.  That promotion will automatically extend the
MSB, which in this case was a one (-1 decimal as a char is 0xff, and
extended is 0xffffffff expressed as an int, which prints as -1.  

    To find the true state of the char, you have to get very creative.
For example:

        unsigned int x;
        char y=-1;
        char *z;

        main()
    {
            z=&x+3;
            *z=y;
          printf ("%0x\n",x);  /* if all went well, and if a signed
value was assigned to the char, you should see 0xff */
          /* now to prove the situation you saw */
         printf ("%0x\n", y);   /* note that the msb propagated and you
now see 0xffffffff */
        /* what happened is that regardless of Y, the sign is propagated
when the sign is extended */
        /* now to show the effect another way */
        {
            unsigned char b=0x10;  /* I have forced b to be unsigned */
            printf ("%0x\n",b);   /* if the compiler did not handle the
unsigned as truely unsigned, the bit was extended and you get 0xffffff80
*/
            printf ("%0x\n",(unsigend int) b);  /* now if everything
worked as it should you should see 0x00000080 */
            printf ("%d\n", b);   /* what do you see here? */
            printf ("%d\n", (unsigned int) b);  /* and now you should
see the number 128 */
           /* isn't printf fun, also bits/bytes/ints and signs */
    }

    Note that I did not try these.  I don't have my environment set up
yet.

Regards,
Les H


[Index of Archives]     [Current Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]     [Fedora Docs]

  Powered by Linux