Thread: can i store 300 in the char variable?

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    64

    can i store 300 in the char variable?

    can i store 300 in the character variable?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,416
    Only on some machines, where CHAR_BIT is more than the usual 8.

    Or you can cheat by using a wchar_t.
    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.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    52
    Simple answer: No. Why? A char is 8 bits and 2^8 = 256. When you use signed, the maximum is more decreased because you can have negative numbers.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Libpgeak View Post
    Simple answer: No. Why? A char is 8 bits and 2^8 = 256. When you use signed, the maximum is more decreased because you can have negative numbers.
    Correct answer, as hinted by Salem: it depends.

    A char is not guaranteed to be 8 bits. The maximum value an unsigned char can hold is only guaranteed - by the C standard - to be at least 255. That does not limit it to being 256. Similarly, the maximum value of a signed char is guaranteed to be at least 127.

    It is also implementation-defined whether a char is signed or unsigned.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 09-04-2011, 09:29 PM
  2. Store pointee to an int variable
    By limp in forum C Programming
    Replies: 7
    Last Post: 05-25-2011, 03:17 PM
  3. which variable can store words?
    By Hunterofman in forum C++ Programming
    Replies: 8
    Last Post: 04-28-2008, 05:59 PM
  4. Replies: 1
    Last Post: 08-03-2005, 04:14 AM
  5. Char variable can't store numbers?
    By Kespoosh in forum C++ Programming
    Replies: 11
    Last Post: 03-15-2003, 07:24 PM