Thread: Find the max limit of unsigned int.

  1. #1
    Registered User
    Join Date
    Mar 2017
    Posts
    1

    Find the max limit of unsigned int.

    Hi, I'm trying to see what is the max limit of unsigned int. I get the size of int of 4 bytes. But the UINT_MAX is giving me -1.
    What am I doing wrong?
    Code:
    #include <stdio.h>
    #include <limits.h>
    
    int main(void)
    {
        int i = sizeof(int);
        printf("The size of int is %d\n ", i);
        printf("The max limit of unsigned is %d\n", UINT_MAX):
    return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    You're not using the correct format string in printf.
    Quote Originally Posted by manpage
    Conversion specifiers
    A character that specifies the type of conversion to be applied. The conversion specifiers and their
    meanings are:

    d, i The int argument is converted to signed decimal notation. The precision, if any, gives the minimum
    number of digits that must appear; if the converted value requires fewer digits, it is padded on
    the left with zeros. The default precision is 1. When 0 is printed with an explicit precision 0,
    the output is empty.

    o, u, x, X
    The unsigned int argument is converted to unsigned octal (o), unsigned decimal (u), or unsigned
    hexadecimal (x and X) notation. The letters abcdef are used for x conversions; the letters ABCDEF
    are used for X conversions. The precision, if any, gives the minimum number of digits that must
    appear; if the converted value requires fewer digits, it is padded on the left with zeros. The
    default precision is 1. When 0 is printed with an explicit precision 0, the output is empty.
    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
    Feb 2012
    Posts
    347
    So i think he should be writing %u instead of %d. I have not tried it but i hope it is correct. It is strange that always i should have an idea of what my output should be to give the input like %d or %u etc. Why cant i have generic format specifier where i need not guess what my output be? I really don't know i am making sense.

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,127
    Quote Originally Posted by alx2progrmz View Post
    Hi, I'm trying to see what is the max limit of unsigned int. I get the size of int of 4 bytes. But the UINT_MAX is giving me -1.
    What am I doing wrong?
    Several things.

    1) Turn on and turn up your warning level to the highest setting!

    2) See my comments in the corrected code below:

    Code:
    #include <stdio.h>
    #include <limits.h>
    
    int main(void)
    {
       // sizeof() returns a size_t not int!
       size_t i = sizeof(int);
    
       // The proper format specifier for a size_t is "%zu".
       printf("The size of int is %zu\n", i);
    
       // You terminate the line with a colon NOT a semi-colon!
       printf("The max limit of unsigned is %u\n", UINT_MAX);
    
       return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How covert unsigned char[4] to unsigned integer?
    By barracuda in forum C Programming
    Replies: 110
    Last Post: 02-23-2015, 04:00 AM
  2. find MSB of unsigned char
    By cubis in forum C Programming
    Replies: 4
    Last Post: 09-25-2010, 11:07 PM
  3. Replies: 2
    Last Post: 10-06-2009, 09:37 AM
  4. Converting unsigned long array to unsigned char array
    By delvec28 in forum C Programming
    Replies: 2
    Last Post: 09-07-2009, 08:53 PM
  5. Replies: 7
    Last Post: 02-08-2008, 06:31 PM