Thread: Why char is unsigned?

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    5

    Lightbulb Why char is unsigned?

    Hello ,

    I read that range of the signed char in C is from -128 to 127 and for unsigned char it is 0 to 255.

    How can char be signed / unsigned. Can we define negative char???

    Thanks

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Try it! Print the value, see what happens.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by fenilshah03 View Post
    Hello ,

    I read that range of the signed char in C is from -128 to 127 and for unsigned char it is 0 to 255.

    How can char be signed / unsigned. Can we define negative char???

    Thanks
    A char is just a 1-byte data type.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    5
    To Subsonics,

    which value are you telling me to print?

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    A negative char...

    Code:
    #include <stdio.h>
    
    int main(void)
    {
      char ch = -10;
      printf("%d\n", ch);
      return 0;
    }
    If you understand what you're doing, you're not learning anything.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    A simple char by itself may be signed or unsigned.

    If you want to make sure, you need to qualify
    signed char foo = -10;
    unsigned char bar = 200;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    5
    Quote Originally Posted by itsme86 View Post
    A negative char...

    Code:
    #include <stdio.h>
    
    int main(void)
    {
      char ch = -10;
      printf("%d\n", ch);
      return 0;
    }
    ok
    Now i can understand that char can have integer value stored in it..

    thanx

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by fenilshah03 View Post
    ok
    Now i can understand that char can have integer value stored in it..

    thanx
    As the others have been explaining the C type char is simply a 1 byte (usually 8bits) integer.

    We use it for strings because most ascii text is byte sized.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory Allocation Problems
    By amac in forum C Programming
    Replies: 9
    Last Post: 12-10-2009, 06:49 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  4. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  5. problem with reading and writing
    By yahn in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2006, 04:38 PM

Tags for this Thread