Thread: a couple questions about typecast

  1. #1
    Linux is where it's at movl0x1's Avatar
    Join Date
    May 2007
    Posts
    72

    a couple questions about typecast

    Hello,
    Am I correct that C automatically typecasts from int to char in the following?

    Code:
              int c = 97;
              char b;
    
              b = c;
    or do I have to explicitly do ...

    Code:
             int c = 97;
             char b;
    
             b = (char) c;
    thanks

  2. #2
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    Yes. It will store the lowest sizeof(char) bytes (1 in this case) into b including a sign bit if it is signed. In this case, since b is a signed char, it will also perserve its sign.

  3. #3
    Linux is where it's at movl0x1's Avatar
    Join Date
    May 2007
    Posts
    72
    thanks

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    since b is a signed char
    It depends on the compiler's settings... It can be unsigned as well.

    that C automatically typecasts from int to char
    Yes, but some compilers on the high warning levels can show a warning about "possible loss of data"
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Linux is where it's at movl0x1's Avatar
    Join Date
    May 2007
    Posts
    72
    So I should explicitly assign it like this?

    Code:
               b = (char) c;
    or like this if I wanted to make sure it was unsigned?
    Code:
              b = (unsigned char) c;
    thanks

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    No, no no, Some compilers specify 'char' as 'unsigned char', this is unusual because most data-types are signed by default.

    so to ensure C is unsigned,
    Code:
    unsigned char c;
    int b;    /* signed by default */
    
    b = (unsigned char) c;   /* To silence the high level warnings. */
    Last edited by zacs7; 05-14-2007 at 03:14 AM.

  7. #7
    Linux is where it's at movl0x1's Avatar
    Join Date
    May 2007
    Posts
    72
    Thanks ... I'm new to typecast

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Couple of questions about XOR linked lists...
    By klawson88 in forum C Programming
    Replies: 5
    Last Post: 04-19-2009, 04:55 PM
  2. A Couple of Questions
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 12-03-2005, 06:47 PM
  3. Couple of simple directdraw questions.
    By Deo in forum Game Programming
    Replies: 3
    Last Post: 05-25-2005, 07:55 AM
  4. Studying for Final, Couple Questions...
    By stewade in forum C Programming
    Replies: 4
    Last Post: 05-10-2004, 05:02 PM
  5. A couple questions
    By punkrockguy318 in forum Tech Board
    Replies: 4
    Last Post: 01-12-2004, 10:52 PM