Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #161423

Re: #include <stdint.h>

From James Kuyper <jameskuyper@alumni.caltech.edu>
Newsgroups comp.lang.c
Subject Re: #include <stdint.h>
Date 2021-06-13 18:34 -0400
Organization A noiseless patient Spider
Message-ID <sa615b$e1r$1@dont-email.me> (permalink)
References <sa5vrs$7d3$1@dont-email.me>

Show all headers | View raw


On 6/13/21 6:08 PM, SIMON wrote:
> What changes do I need to make to get the correct values for the last 
> two items in this program:
> 
>> #include <stdio.h>
>> #include <stdint.h>
>> #include <limits.h>

#include <inttypes.h>

>> int main()
>> {
>>  printf("INT_MAX : %d\n", INT_MAX);
>>  printf("INT_MIN : %d\n", INT_MIN);
>>  printf("LONG_MAX : %ld\n", (long)LONG_MAX);
>>  printf("LONG_MIN : %ld\n", (long)LONG_MIN);
>>  printf("UINT_MAX : %u\n", (unsigned int)UINT_MAX);
>>  printf("ULONG_MAX : %lu\n", (unsigned long)ULONG_MAX);
>>  printf("USHRT_MAX : %d\n", (unsigned short)USHRT_MAX);
>>  printf("\nFrom stdint.h: \n");
>>  printf("INT8_MAX : %d\n", INT8_MAX);
>>  printf("INT16_MAX : %d\n", INT16_MAX);
>>  printf("INT32_MAX : %d\n", INT32_MAX);
>>  printf("INT64_MAX : %lld\n", INT64_MAX);
>>  printf("UINT8_MAX : %d\n", UINT16_MAX);
>>  printf("UINT16_MAX : %d\n", UINT16_MAX);
>>  printf("UINT32_MAX : %d\n", UINT32_MAX);
>>  printf("UINT64_MAX : %lld\n", UINT64_MAX);

You should not be using the "d" conversion specifier for unsigned types.
You should use "u", "o", "x" or "X" formats.
In general, you should not use explicit length specifiers (such as "h",
"hh", "l", and "ll") for the size-named types - there's no guaranteed
connection between the specifiers and the types. Instead, you should use
the print format macros from <inttypes.h>:

    printf("INT8_MAX : %" PRId8 "\n", INT8_MAX);
    printf("INT8_MAX : %" PRId16 "\n", INT16_MAX);
    printf("INT8_MAX : %" PRId32 "\n", INT32_MAX);
    printf("INT8_MAX : %" PRId64 "\n", INT64_MAX);

Assuming that you would actually have chosen the "u" conversion
specifier, you should use the following for the unsigned types. If you
would prefer, for instance, the "x" conversion specifier, change "u" to
"x" in the following macro names:
	
    printf("UINT8_MAX : %" PRIu8 "\n", UINT8_MAX);
    printf("UINT8_MAX : %" PRIu16 "\n", UINT16_MAX);
    printf("UINT8_MAX : %" PRIu32 "\n", UINT32_MAX);
    printf("UINT8_MAX : %" PRIu64 "\n", UINT64_MAX);

>> }
> 
> I am using Windows 10 64 bit machine and I have tried GCC64 bit and 
> Visual Studio 2019 64 bit. The last two results are just ( -1 ). this is clearly over/underflow somehow so what changes are necessary.
> 
> I was in the opinion that stdint.h is cross-compiler/cross- platform 
> compliant.

It is, when used correctly - which means, among other things, using the
print macros rather than explicit length specifiers in printf/scanf
family format strings. The exact-sized types are optional, but  the
corresponding macros must be #defined and if (and only if) the
corresponding types are supported.
For maximum portability, you should avoid the exact-sized types and use
only int_fastN_t and int_leastN_t types, and only for the values of N
for which support for those types is mandatory: 8, 16, 32, and 64. In
practice, most people can ignore this advice.


Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

#include <stdint.h> SIMON <invalid@invalid.invalid> - 2021-06-13 23:08 +0100
  Re: #include <stdint.h> Bart <bc@freeuk.com> - 2021-06-13 23:27 +0100
    Re: #include <stdint.h> Bart <bc@freeuk.com> - 2021-06-13 23:57 +0100
      Re: #include <stdint.h> Philipp Klaus Krause <pkk@spth.de> - 2021-06-14 09:03 +0200
        Re: #include <stdint.h> Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-06-14 01:23 -0700
          Re: #include <stdint.h> Philipp Klaus Krause <pkk@spth.de> - 2021-06-14 12:31 +0200
        Re: #include <stdint.h> Bart <bc@freeuk.com> - 2021-06-14 11:54 +0100
  Re: #include <stdint.h> James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-06-13 18:34 -0400
  Re: #include <stdint.h> Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-06-13 23:34 +0100
  Re: #include <stdint.h> Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-06-13 17:48 -0700
    Re: #include <stdint.h> Philipp Klaus Krause <pkk@spth.de> - 2021-06-14 09:09 +0200
      Re: #include <stdint.h> Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-06-14 01:24 -0700
        Re: #include <stdint.h> Philipp Klaus Krause <pkk@spth.de> - 2021-06-14 12:30 +0200
    Re: #include <stdint.h> Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-06-24 10:31 -0700
      Re: #include <stdint.h> Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-06-24 11:28 -0700
        Re: #include <stdint.h> Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-07-11 02:49 -0700
          Re: #include <stdint.h> Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-07-11 15:30 -0700
            Re: #include <stdint.h> Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-07-16 03:33 -0700
              Re: #include <stdint.h> Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-07-16 12:31 -0700
                Re: #include <stdint.h> Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-09-30 07:07 -0700
                Re: #include <stdint.h> Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-09-30 08:42 -0700
                Re: #include <stdint.h> Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-10-06 03:55 -0700
                Re: #include <stdint.h> Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-10-06 07:06 -0700
                Re: #include <stdint.h> James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-10-06 11:47 -0400
        Re: #include <stdint.h> Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-10-06 09:35 -0700
          Re: #include <stdint.h> Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-10-06 10:06 -0700
          Re: #include <stdint.h> James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-10-06 17:40 -0400
            Re: #include <stdint.h> Branimir Maksimovic <branimir.maksimovic@icloud.com> - 2021-10-06 22:14 +0000
              Re: #include <stdint.h> James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-10-06 20:06 -0400
                Re: #include <stdint.h> Branimir Maksimovic <branimir.maksimovic@icloud.com> - 2021-10-07 02:15 +0000
      Re: #include <stdint.h> Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-06-24 22:34 +0100
        Re: #include <stdint.h> Öö Tiib <ootiib@hot.ee> - 2021-06-24 15:01 -0700
          Re: #include <stdint.h> Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-06-24 15:26 -0700
        Re: #include <stdint.h> Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-07-11 02:14 -0700

csiph-web