Thread: Data Type Range

  1. #1
    Registered User
    Join Date
    Nov 2012
    Location
    Louisville, KY
    Posts
    12

    Question Data Type Range

    I have seen that unsigned int ranges from 0 to 4,294,967,295. I cannot print this to the user without a warning though. I'm just trying to understand the limits of variables, so this is just for my reference.

    Here is the code:

    Code:
    #include <stdio.h>
    
    int main(){
        unsigned int a = 4294967295;
        // 4 Bytes [Signed: -2147483648 to 2137483647, Unsigned: 0 to 4294967295]
        printf("%u", a);
        return 0;
    }
    Basically, I want the warning to go away. I read about using limit.h, but that is not what I'm trying to do. I just want to know the maximum value that data types will hold.

    Warning I get:

    Code:
    warning: this decimal constant is unsigned only in ISO C90 [enabled by default]
    Thanks for your time.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    4294967295; doesn't fit into the range of an int -> warning
    try
    Code:
     unsigned int a = 4294967295u;
    Kurt

  3. #3
    Registered User
    Join Date
    Nov 2012
    Location
    Louisville, KY
    Posts
    12
    Quote Originally Posted by ZuK View Post
    4294967295; doesn't fit into the range of an int -> warning
    try
    Code:
     unsigned int a = 4294967295u;
    Kurt
    Thank you ZuK. That did work. I have another question in case you or someone else comes here. Would the following sample code be valid?

    Code:
    int a = -2147483648u;
    printf("int [Minimum]: %d", a);
    It does print it to the screen correctly, but is that the best way to do that?

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    505
    Quote Originally Posted by Khaltazar View Post
    Thank you ZuK. That did work. I have another question in case you or someone else comes here. Would the following sample code be valid?

    Code:
    int a = -2147483648u;
    printf("int [Minimum]: %d", a);
    It does print it to the screen correctly, but is that the best way to do that?
    You don't need the u. The value will fit into a signed int (just).

    But normally it's better to use the defines in limits.h, or use hexadecimal. Most people don't know the decimal values off pat, and it's hard to see what you are doing.

    Another question is, do you really want the biggest 32 bit value, or the biggest value the computer will hold in an int? Whilst these are usually the same, the way you use the values is likely to be different.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  5. #5
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    I have seen that unsigned int ranges from 0 to 4,294,967,295.

    It may have that range, but it's guaranteed to have a much smaller one.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Khaltazar View Post
    Code:
    int a = -2147483648u;
    printf("int [Minimum]: %d", a);
    It looks like you tried that because nobody actually said that what the U suffix does is it makes the constant unsigned, irrespective of that value being assigned to the variable.

    As it makes no sense to have a negative unsigned value, simply do not do that.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-04-2010, 01:34 AM
  2. How to compute the variable type range
    By webofunni in forum C Programming
    Replies: 1
    Last Post: 08-04-2009, 01:28 AM
  3. Replies: 2
    Last Post: 01-31-2008, 09:52 AM
  4. how to define a range of type in C++ or C?
    By zaracattle in forum C++ Programming
    Replies: 24
    Last Post: 03-23-2007, 03:31 PM
  5. Replies: 5
    Last Post: 08-09-2004, 08:25 PM