Printing datatypes problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mikeyit87
    New Member
    • Aug 2015
    • 12

    Printing datatypes problem

    My program is just printing ranges but the unsigned integer is not working. The max is suppose to be 65535. And also integer is messed up. And also long is. I'm new to programming and books can be vague.

    C:\Documents and Settings\Mikes\ Desktop\Cprog>g cc Datatypes.c -o Datatypes.exe

    C:\Documents and Settings\Mikes\ Desktop\Cprog>D atatypes
    Character min: -128

    Character max: 127

    Integer min: -2147483648

    Integer max: 2147483647

    Long min: -2147483648

    Long max: -2147483648

    Short min: -32768

    Short max: 32767

    Signed Character min: -128

    Signed Character max: 127

    Unsigned Character max: 255

    Unsigned Integer max: 4294967295

    Unsigned Long max: 4294967295

    Unsigned Short max: 65535


    C:\Documents and Settings\Mikes\ Desktop\Cprog>
    Code:
    #include<stdio.h>
    #include<limits.h>
    #include<float.h>
    main()
    {
    /* A program to determine the range of data types */
    printf("Character min: %d\n\n", CHAR_MIN); 
    printf("Character max: %d\n\n", CHAR_MAX); 
    printf("Integer min: %d\n\n", INT_MIN); 
    printf("Integer max: %d\n\n", INT_MAX);
    printf("Long min: %d\n\n", LONG_MIN);
    printf("Long max: %d\n\n", INT_MIN);
    printf("Short min: %d\n\n", SHRT_MIN);
    printf("Short max: %d\n\n", SHRT_MAX);
    printf("Signed Character min: %d\n\n", SCHAR_MIN);
    printf("Signed Character max: %d\n\n", SCHAR_MAX);
    printf("Unsigned Character max: %d\n\n", UCHAR_MAX);
    printf("Unsigned Integer max: %u\n\n", UINT_MAX);
    printf("Unsigned Long max: %lu\n\n", ULONG_MAX);
    printf("Unsigned Short max: %d\n\n", USHRT_MAX);}
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Some of the format specifiers you use are not correct. For example, %c is for char and %d is for signed integer. Go through your code and be sure you use the correct specifier.

    Also, the size of types varies by compiler implementation. You should do a sizeof(type) to see how big the type is. Often the int and the long int are the same size.

    Be sure char is signed. It's not required to be signed. The only sure signed char is: signed char. There should be compiler info or settings for this. If a char is 8 bits the max value is 255. A max value of 127 is for an 8-bit char with a 0 for the sign bit. So this applies only to signed char. A max value of 128 is binary 1000 0000. That left bit 1 is the sign bit. It's not part of the data. A 1 in the sign bit means a negative number. An unsigned 8-bit char has no sign bit so it's max positive value is 1111 1111 which is 255.

    Negative values are often stored in 2's-complement format so just looking at the binary does not tell you the value. This is why the printf format specifiers are so important. They will convert to decimal so you can read the data value.

    Comment

    • mikeyit87
      New Member
      • Aug 2015
      • 12

      #3
      Idk their much bigger than indicated in the book, but the book is old. I went to wikipedia https://en.wikipedia.org/wiki/C_data_types to correct each one and size is not changing so that must be the size. Int is 16 to 32 bits and not necessarily the same size as long but in the case it looks like they are. Thanks for the input.

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        I don't see anything wrong with the values you listed in the original posting.

        The C Standard allows the compiler writer to set these limits as they wish ... as long as their choices fit within the broad outline mandated in the Standard. You wouldn't expect two different compilers to report the same limit values.

        Comment

        • mikeyit87
          New Member
          • Aug 2015
          • 12

          #5
          The whole point of this exercise was to use to start using diffrent header files for the first time. In the book INT is +/- 32767, UINT is 65535. Which when your a beginner and you get numbers not intended you begin to think your doing something wrong. When I fixed everything, INT was still 32 bits and UINT was same size as ULONG which as you can see from the numbers. They're 16 bit in the book that's all.

          Comment

          Working...