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


Groups > comp.lang.c > #161488

Re: #include <stdint.h>

From Keith Thompson <Keith.S.Thompson+u@gmail.com>
Newsgroups comp.lang.c
Subject Re: #include <stdint.h>
Date 2021-06-24 15:26 -0700
Organization None to speak of
Message-ID <87r1gqvqzl.fsf@nosuchdomain.example.com> (permalink)
References <sa5vrs$7d3$1@dont-email.me> <87wnqxxoe3.fsf@nosuchdomain.example.com> <86bl7vb24m.fsf@linuxsc.com> <878s2zndzc.fsf@bsb.me.uk> <122cd8b8-5ef9-4b3d-a739-a071153cbf5cn@googlegroups.com>

Show all headers | View raw


Öö Tiib <ootiib@hot.ee> writes:
> On Friday, 25 June 2021 at 00:34:58 UTC+3, Ben Bacarisse wrote:
>> Tim Rentsch <tr.1...@z991.linuxsc.com> writes: 
>> > Keith Thompson <Keith.S.T...@gmail.com> writes: 
>> > 
>> > [... on printing various integer types ...]
>> <cut>
>> >> printf("USHRT_MAX : %u\n", (unsigned)USHRT_MAX); 
>> > 
>> > The cast in the last line is not needed.
>> <cut>
>> > The description of how type promotion works is right, however a 
>> > cast is still not needed there. The reason is that values given 
>> > as variadic arguments are allowed to be the corresponding signed 
>> > or unsigned version of the type used to get the value, and that 
>> > will work as long as the value is in the set of values common to 
>> > both types. If an unsigned short promotes to int, then its value 
>> > is guaranteed to be in the set of values representable by both 
>> > int and unsigned int, and hence using %u for an uncasted unsigned 
>> > short is always safe.
>> It's safe (in practical terms) but 7.21.6.1 paragraph 8 says of the 'u' 
>> conversion specifier: 
>> 
>> "o,u,x,X The unsigned int argument is..." 
>> 
>> and, later, paragraph 9 says 
>> 
>> "... If any argument is not the correct type for the corresponding 
>> conversion specification, the behavior is undefined." 
>> 
>> So I am left wondering how one can know that int (the promoted type when 
>> no cast is used) is "the correct type" for the 'u' conversion 
>> specification. 
>
> Yes it is strange. IIRC the constant USHRT_MAX may be of type
> signed int or unsigned int but can not be of type unsigned short.

Correct.  Which means that if you want to print the value of USHRT_MAX
safely, you can do:

    printf("USHRT_MAX = %u\n", (unsigned)USHRT_MAX);

-- 
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 | 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