Path: csiph.com!news.mixmin.net!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: Tim Rentsch
Newsgroups: comp.lang.c
Subject: Re: Beginner....Decimal/Octal converter help
Date: Fri, 16 Sep 2022 13:06:56 -0700
Organization: A noiseless patient Spider
Lines: 55
Message-ID: <86a66zkrrz.fsf@linuxsc.com>
References: <8ffd982c-2e82-4caa-9256-ec17be2efe56n@googlegroups.com> <86leqsr091.fsf@linuxsc.com> <875yhv30nu.fsf@bsb.me.uk> <87zgf714zc.fsf@bsb.me.uk> <8735cy0wt4.fsf@bsb.me.uk> <87r10hzn19.fsf@bsb.me.uk> <87mtb4wq4f.fsf@bsb.me.uk> <875yhswfiv.fsf@bsb.me.uk> <87a673hzzj.fsf@nosuchdomain.example.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader01.eternal-september.org; posting-host="4a0db673561e161ba81b81715d43a3e1"; logging-data="4135413"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/cSzGsYloQJF+8mCRO0nsU0m/PVe6JpKo="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:K/yJi+QU5V++7kKCom4QE1EWa78= sha1:oSYe7l14V63A362xXbKwTVgC7OU=
Xref: csiph.com comp.lang.c:167745
Keith Thompson writes:
> David Brown writes:
> [...]
>
>> While thinking about newer C features, I have found another challenge
>> with using multiple different sometimes incompatible aliases for
>> integer types - _Generic. I don't know if you've used _Generic much,
>> but it can sometimes be useful. Here is a small example :
>>
>> #include
>> #include
>> #include
>>
>> #define print_var(_x) \
>> do { \
>> printf("Var " # _x " = "); \
>> printf( \
>> _Generic(_x, \
>> uint8_t : "0x%02" PRIx8, \
>> uint16_t : "0x%04" PRIx16, \
>> uint32_t : "0x%08" PRIx32, \
>> uint64_t : "0x%016" PRIx64), \
>> _x); \
>> printf("\n"); \
>> } while (0);
>
> I'd use the predefined types unsigned char, unsigned short, et al rather
> than the uintN_t types. For example, if uint32_t is unsigned int and
> uint64_t is unsigned long long, then passing an unsigned long argument
> is an error. Using the predefined types guarantees that you'll cover
> all the integer types.
Yes, obviously using standard integer types is better than
considering just the exact width types.
> You can use sizeof to determine the number of digits.
It seems better to use values from to determine the
number of digits.
> Except that it all breaks down if some of the types in are
> defined as extended integer types. There's no good way for a _Generic
> expression to cover all the integer types if the implementation supports
> extended integer types. [...]
There is a way to write a _Generic expression that covers all the
standard integer types; char; the real floating types; size_t;
ptrdiff_t; wchar_t; wint_t; [u]intmax_t; [u]intptr_t (if they
exist); [u]int_least{8,16,32,64}_t; [u]int_fast{8,16,32,64}_t;
and [u]int{8,16,32,64}_t (if they exist); with separate result
expressions for each type, and which will be accepted without
complaint regardless of which types may be extended integer
types. Assuming of course that someone thinks it's important to
do that.