Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #161426
| From | Keith Thompson <Keith.S.Thompson+u@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: #include <stdint.h> |
| Date | 2021-06-13 17:48 -0700 |
| Organization | None to speak of |
| Message-ID | <87wnqxxoe3.fsf@nosuchdomain.example.com> (permalink) |
| References | <sa5vrs$7d3$1@dont-email.me> |
SIMON <invalid@invalid.invalid> writes:
> 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>
>>
>> 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);
>> }
>
> 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 -- but none of the exact-width types are guaranteed to exist
(though the 8, 16, 32, and 64 bit ones almost certainly will on typical
systems).
<inttypes.h> provides macros that expand to appropriate format strings.
<inttypes.h> includes <stdint.h>, so you don't need to include both,
though you can.
Using the macros:
#include <stdio.h>
// #include <stdint.h>
#include <inttypes.h>
#include <limits.h>
int main(void) {
printf("INT_MAX : %d\n", INT_MAX);
printf("INT_MIN : %d\n", INT_MIN);
printf("LONG_MAX : %ld\n", LONG_MAX);
printf("LONG_MIN : %ld\n", LONG_MIN);
printf("UINT_MAX : %u\n", UINT_MAX);
printf("ULONG_MAX : %lu\n", ULONG_MAX);
printf("USHRT_MAX : %u\n", (unsigned)USHRT_MAX);
printf("\nFrom stdint.h: \n");
printf("INT8_MAX : %" PRId8 "\n", INT8_MAX);
printf("INT16_MAX : %" PRId16 "\n", INT16_MAX);
printf("INT32_MAX : %" PRId32 "\n", INT32_MAX);
printf("INT64_MAX : %" PRId64 "\n", INT64_MAX);
printf("UINT8_MAX : %" PRIu8 "\n", UINT16_MAX);
printf("UINT16_MAX : %" PRIu16 "\n", UINT16_MAX);
printf("UINT32_MAX : %" PRIu32 "\n", UINT32_MAX);
printf("UINT64_MAX : %" PRIu64 "\n", UINT64_MAX);
}
Most of the casts in your original program are unnecessary, since the
expressions are already of the type you were casting them to. But
unsigned short could promote either to signed int or to unsigned int,
depending on their ranges, so converting to unsigned and using "%u" is
appropriate. In general, when passed to a variadic function like
printf, an argument of an integer type narrower than int is promoted to
int if type's range fits in int, to unsigned int otherwise.
An alternative, if you don't want to remember or look up all those
macros, is to convert to a type that's known to be wide enough.
long long is guaranteed to be at least 64 bits (and is almost always
exactly 64 bits), so this might be simpler:
#include <stdio.h>
#include <stdint.h>
#include <limits.h>
int main(void) {
printf("INT_MAX : %d\n", INT_MAX);
printf("INT_MIN : %d\n", INT_MIN);
printf("LONG_MAX : %ld\n", LONG_MAX);
printf("LONG_MIN : %ld\n", LONG_MIN);
printf("UINT_MAX : %u\n", UINT_MAX);
printf("ULONG_MAX : %lu\n", ULONG_MAX);
printf("USHRT_MAX : %u\n", (unsigned)USHRT_MAX);
printf("\nFrom stdint.h : \n");
printf("INT8_MAX : %lld\n", (long long)INT8_MAX);
printf("INT16_MAX : %lld\n", (long long)INT16_MAX);
printf("INT32_MAX : %lld\n", (long long)INT32_MAX);
printf("INT64_MAX : %lld\n", (long long)INT64_MAX);
printf("UINT8_MAX : %llu\n", (unsigned long long)UINT16_MAX);
printf("UINT16_MAX : %llu\n", (unsigned long long)UINT16_MAX);
printf("UINT32_MAX : %llu\n", (unsigned long long)UINT32_MAX);
printf("UINT64_MAX : %llu\n", (unsigned long long)UINT64_MAX);
}
There's some performance cost relative to the type-specific format
macros, but printf itself is far more expensive than a few integer type
conversions.
If you have an integer type that might possibly be wider than long
long (which is unlikely in practice), you can convert to intmax_t
or uintmax_t and use "%jd" or "%ju". That might be overkill if you
don't really need 100% guarantee portability.
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips Healthcare
void Void(void) { Void(); } /* The recursive call of the void */
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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