problem with a simple program in C

From C++ to PHP, debugging to webhosting; help and discussion about writing your latest program to running your website. NOT for help when your PC won't work.

Announcements Posted on
Enter our travel-writing competition for the chance to win a Nikon 1 J3 camera 21-05-2013
Sign in to Reply
  1. Dr Ben's Avatar
    • Respected Member
    • Posts: 225
    problem with a simple program in C
    here my code:

    Code:
    #include <stdio.h>
    #include <limits.h>
    #include <float.h>
    
    int main()
    {
        //chars
        int maxUChar, maxSChar, minSChar;
    
        maxSChar = SCHAR_MAX;
        minSChar = SCHAR_MIN;
        maxUChar = UCHAR_MAX;
    
        printf("The range of a schar is: %d <= x <= %d\n", minSChar, maxSChar);
        printf("The range of a uchar is: 0 <= x <= %d\n", maxUChar);
        printf("\n");
    
        //ints
        int maxUInt, maxSInt, minSInt;
    
        maxUInt = UINT_MAX;
        maxSInt = INT_MAX;
        minSInt = INT_MIN;
    
        printf("The range of an int is: %d <= x <= %d\n", minSInt, maxSInt);
        printf("The range of a uint is: 0 <= x <= %d\n", maxUInt);
    
    
    }

    here's my output:

    the last line (that reads "printf("The range of a uint is: 0 <= x <= %d\n", maxUInt);") appears to print out "The range of uint is: 0 <= x <= -1".

    Why does it say -1? According to the O'Reilly C Programming Language book it should say 65536 instead of -1
  2. JGR's Avatar
    • Exalted and Worshipped Member
    • Posts: 1,239
    Re: problem with a simple program in C
    int hasn't been 16 bits wide since the DOS era.

    If you want to print an unsigned int, use the %u format code.
    You'll most likely get a result of 4294967295.

    If you absolutely must have 16 bit wide integers, use a short (and the associated format code).
  3. Dr Ben's Avatar
    • Respected Member
    • Posts: 225
    Re: problem with a simple program in C
    (Original post by JGR)
    int hasn't been 16 bits wide since the DOS era.

    If you want to print an unsigned int, use the %u format code.
    You'll most likely get a result of 4294967295.

    If you absolutely must have 16 bit wide integers, use a short (and the associated format code).
    ah ok thanks, but do you know why its saying -1? the constants USHORT_MAX, and ULONG_MAX also appear to have the values of -1
  4. JGR's Avatar
    • Exalted and Worshipped Member
    • Posts: 1,239
    Re: problem with a simple program in C
    (Original post by Dr Ben)
    ah ok thanks, but do you know why its saying -1? the constants USHORT_MAX, and ULONG_MAX also appear to have the values of -1
    Have a read of this http://en.wikipedia.org/wiki/2s_compliment
    It explains how signed integers work.
Sign in to Reply
Share this discussion:  
Useful resources
Article updates
Moderators

We have a brilliant team of more than 60 volunteers looking after discussions on The Student Room, helping to make it a fun, safe and useful place to hang out.

Reputation gems:
The Reputation gems seen here indicate how well reputed the user is, red gem indicate negative reputation and green indicates a good rep.
Post rating score:
These scores show if a post has been positively or negatively rated by our members.