Thread: why omit unsigned int in unsuffixed decimal integer constant?

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    48

    why omit unsigned int in unsuffixed decimal integer constant?

    Hi,

    In the K&R C II,
    Code:
    The type of an integer constant depends on its form, value and suffix. 
    (See Par.A.4 for a discussion of types). If it is unsuffixed and decimal, it 
    has the first of these types in which its value can be represented: int, 
    long int, unsigned long int. If it is unsuffixed, octal or hexadecimal, it 
    has the first possible of these types: int, unsigned int, long int, 
    unsigned long int. If it is suffixed by u or U, then unsigned int, unsigned 
    long int. If it is suffixed by l or L, then long int, unsigned long int. If an 
    integer constant is suffixed by UL, it is unsigned long.
    compared to int, unsigned int can represent bigger positive numbers, just like unsigned long vs. long. If int is assigned to 8 bits, then its range for positive number is [0, +127], while unsigned int is [0, +255]. An integer constant like 200 can be represented as unsigned int, which doesn't have to be long (which maybe has bigger bits than int).
    Just like unsuffixed octal and hex integer constants, they take an order int->unsigned int->long->unsigned log. I think this is more reasonable, don't understand why unsuffixed decimal integer skip unsigned int. Could anyone please explain it to me?

    THANKS!!

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    An integer constant (unsuffixed decimal digits) is placed in a signed integral type, never in an unsigned one. The order in the 1999 C standard is int, long, long long NOT int, long, unsigned long,
    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.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    48
    Quote Originally Posted by grumpy View Post
    An integer constant (unsuffixed decimal digits) is placed in a signed integral type, never in an unsigned one. The order in the 1999 C standard is int, long, long long NOT int, long, unsigned long,
    I'm talking about C90 actually, and it does take the order int, long, unsigned long. Why never in an unsigned one? What about unsuffixed octal and hex integer constant? They can also be unsigned.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It could well be a matter of common usage: the decimal representation tends to see use with signed integer variables, so the type of the literal without suffix is only made unsigned when necessary, and then made only signed in C99. The octal and hexadecimal representations tend to see use with unsigned integer variables as they may be involved in bitwise operations, so prefering the unsigned version of an integer conversion rank to the signed version of the next rank makes sense.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Decimal to 16 bit unsigned?
    By blurx in forum C Programming
    Replies: 5
    Last Post: 10-16-2008, 01:56 PM
  2. Replies: 7
    Last Post: 06-01-2008, 07:47 AM
  3. integer constant is too large for type
    By Abda92 in forum C++ Programming
    Replies: 8
    Last Post: 02-09-2008, 11:47 AM
  4. How do I print 64 bit unsigned integer in hex?
    By electrolove in forum C Programming
    Replies: 7
    Last Post: 02-11-2003, 12:43 PM
  5. integer to constant char w/o using itoa()
    By kes103 in forum C++ Programming
    Replies: 1
    Last Post: 01-06-2003, 10:42 PM