Like Tree1Likes
  • 1 Post By oogabooga

Casting isn't portable?

This is a discussion on Casting isn't portable? within the C++ Programming forums, part of the General Programming Boards category; I ran the following code below and got 2 different outputs depending on the architecture I ran it on. Code: ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    119

    Casting isn't portable?

    I ran the following code below and got 2 different outputs depending on the architecture I ran it on.

    Code:
    int main()
    {
     char x = -1;
     int y = x;
    
     printf("0x%x\n", y);
    
     return 0;
    }
    On x86 I got 0xffffffff. Which is exactly what I'd expect. But on powerpc I got 0xff. Is casting really not portable? If so, what is the best way to deal with it?

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    char's are signed on some archs and unsigned on others.

    As an experiment try signed char and unsigned char on both archs.
    Last edited by oogabooga; 10-11-2012 at 05:47 PM.
    Salem likes this.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    119
    I'm not sure what you're getting at with that suggestion. But I did try replacing the char with a short and correctly got 0xffffffff. So I guess that means it is unsigned on the powerpc arch I'm using.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,739
    Unadorned char can be signed or unsigned the standard does not mandate which. There are compiler switches to force the use of one type though.

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Replacing the char with a short doesn't prove anything (shorts are always signed unless you specifically request an unsigned short).

    I meant to try forcing an unsigned char:
    Code:
    unsigned char x = -1;
    and then forcing a signed char:
    Code:
    signed char x = -1;
    and see the difference.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    119
    Oh. I didn't even realize there was a signed keyword. I've never seen that used anywhere.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Portable executable in C
    By inu11byte in forum C Programming
    Replies: 2
    Last Post: 08-25-2012, 11:13 AM
  2. making it portable.....?
    By ShadeS_07 in forum C Programming
    Replies: 11
    Last Post: 12-24-2008, 08:38 AM
  3. C - Portable?
    By vb.bajpai in forum C Programming
    Replies: 2
    Last Post: 07-15-2007, 09:09 AM
  4. 'portable' package
    By janklojo in forum C++ Programming
    Replies: 2
    Last Post: 12-22-2006, 12:58 PM
  5. which Portable mp3 player?
    By Raihana in forum A Brief History of Cprogramming.com
    Replies: 27
    Last Post: 01-09-2004, 06:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21