Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #167663
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Beginner....Decimal/Octal converter help |
| Date | 2022-09-13 12:48 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <8735cvtryu.fsf@bsb.me.uk> (permalink) |
| References | (12 earlier) <tfnh5k$29dcu$1@dont-email.me> <87mtb4wq4f.fsf@bsb.me.uk> <tfnnob$1d8r$1@gioia.aioe.org> <875yhswfiv.fsf@bsb.me.uk> <tfpm5j$2idls$1@dont-email.me> |
David Brown <david.brown@hesbynett.no> writes:
> On 12/09/2022 21:37, Ben Bacarisse wrote:
>> Bart <bc@freeuk.com> writes:
>>
>>> On 12/09/2022 16:48, Ben Bacarisse wrote:
>>>> David Brown <david.brown@hesbynett.no> writes:
>>>
>>>> In the vast majority of cases, when I see intN_t I feel that
>>>> int_leastN_t or int_fastN_t would be better, because none of extra
>>>> guarantees of intN_t are needed. I usually just think "ah, this person
>>>> does not know about the alternatives".
>>>>
>>>>> I think to some extent it comes down to a subjective opinion of what
>>>>> we are willing to be wrong about and what we prefer to be correct
>>>>> about in how we code. There are always compromises.
>>>> What are some good examples if the use of the intN_t types?
>>>
>>> Of fixed width integer types in general?
>> No. Cases where intN_t is better than ether int_lastN_t or int_fastN_t,
>> except (and I've made this exception clear from the start) matching
>> externally imposed size requirements.
>
> My argument is not so much that "intN_t" types are really /better/
> than "int_leastN_t" or "int_fastN_t", but that the "least" and "fast"
> versions are not noticeably better. In most situations, /none/ of
> these types express /exactly/ what you want, with no more and no less.
> Generally, they all specify features that don't matter at the time.
> So the simpler and clearer choice then wins overall. (We may have
> different ideas about what "better" means.)
It's a relative term. When I see "int32_t" I imagine the programmer
considers that type to be better than the alternatives. So I wonder why
the programmer thinks that limiting range is important. Must this
object map to some hardware feature? If not, I am curious about how the
code would fail if the fastest type that can handle 32 bits were used
instead.
When I see int_fast32_t I have fewer questions. Even fewer if I see
long int (guaranteed to be at least 32 bits) and likely to be the
fastest such type.
> Code does not exist in a vacuum (except for snippets posted in some
> kind of discussion forum, which is why examples here would likely be
> pointless).
Why does a real world example become pointless when posted here?
> Perhaps if a large proportion of C programmers had been re-educated
> when C99 came out, and encouraged in new directions, then the "fast"
> and "least" types could have become popular - and then they would have
> been the "better" choice much more often. But that didn't happen.
But I think people /do/ use the intN_t types when matching external data
is not involved. (If they don't, I've misunderstood, and this whole
thread is pointless!) But if they do use them, how did they learn about
them without learning about the alternatives? I suspect they saw them
used for one thing (maybe in an API struct) and then used them for other
tasks without much thought.
> 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 <stdint.h>
> #include <inttypes.h>
> #include <stdio.h>
>
> #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);
<cut>
> So far, so good. What about the "least" types? We can't add them to
> the list as the _Generic would be ambiguous. Fortunately, in the real
> world (excluding DSP's) the "least" types are always identical to the
> fixed-size types, so the _Generic works unchanged.
>
> void printtest2(uint_least8_t x, uint_least16_t y,
> uint_least32_t z, uint_least64_t w) {
> print_var(x);
> print_var(y);
> print_var(z);
> print_var(w);
> }
>
> But when we get to the "fast" types, the fun starts :
>
> void printtest3(uint_fast8_t x, uint_fast16_t y,
> uint_fast32_t z, uint_fast64_t w) {
> print_var(x);
> print_var(y);
> print_var(z);
> print_var(w);
> }
>
> For one thing, the person expecting to see a 16-bit "y" sees 16 hex
> digits, not 4.
Which is presumably what the author intended, no? I can't see what
point the code has other than to reveal the underlying width.
> But try compiling this for 32-bit ARM (here's a godbolt link:
> <https://godbolt.org/z/TP3sMnTsG> ). It turns out that on that
> platform, "uint32_t" is "unsigned long int", while "uint_fast32_t" is
> "unsigned int". So now our nice neat _Generic needs to have an extra
> line :
>
> uint_fast32_t : "0x%08" PRIxFAST32, \
>
> and that also matches "uint_fast16_t".
>
> But now try compiling this for 64-bit x86. Here, "uint_fast16_t",
> "uint_fast32_t" and "uint_fast64_t" are all "unsigned long long int" -
> colliding with "uint64_t".
>
> If you stick to a simple set of fixed-size types, the _Generic
> statements are clear, follow a nice pattern, and give the user the
> expected results. Mixing the "fast" types spoils all that.
It looks like you wrote a rather limited macro in order to make the case
that a limited number of types should be used? I must be missing the
point you are making. To help move things along, here's what I'd write:
#define print_var(x) \
printf("Var "#x" = 0x%0*"PRIxMAX"\n", (int)(sizeof x * 2), (intmax_t)x);
What am I missing about the intent of your macro?
> Certainly _Generic is a bit niche, and not commonly used - I'd believe
> you if you said that many more people know about "uint_fast16_t" than
> _Generic. But it is an indication of how a simpler and more limited
> selection of types can lead to simpler and neater code, and fewer
> unexpected effects.
I not getting the point. Programmers should avoid using all the type
available so that _Generic is more usable? Surely not. I am missing
something.
>>> * Specifying narrow storage types for efficient memory use of arrays
>>> and structs
>> So why not int_leastN_t?
>
> The question is rather, /why/ int_leastN_t?
No! The question is why int_leastN_t rather the X, and I am currently
focusing on the case where X is intN_t. There will be different answers
for other Xs.
> It doesn't give you a
> smaller type, it just gives the reader bigger uncertainty.
When compared to intN_t it tells the reader that this is one of the
cases Bart was talking about -- it's the space that's important. intN_t
does not say that. intN_t says we need N and no more than N bits for
some reason the reader will be left to speculate about. I would assume
that this struct or array was to be matched against some external data
format.
Seeing int_least32_t I would assume the array will be large and space
matters. Why would you /not/ want to tell the reader this?
--
Ben.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Beginner....Decimal/Octal converter help ManyBeers <markzuffi@yahoo.com> - 2022-09-08 11:34 -0700
Re: Beginner....Decimal/Octal converter help Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-09-08 11:56 -0700
Re: Beginner....Decimal/Octal converter help Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-09-08 20:19 +0100
Re: Beginner....Decimal/Octal converter help Barry Schwarz <schwarzb@delq.com> - 2022-09-08 12:34 -0700
Re: Beginner....Decimal/Octal converter help Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2022-09-08 20:38 -0600
Re: Beginner....Decimal/Octal converter help Öö Tiib <ootiib@hot.ee> - 2022-09-09 03:22 -0700
Re: Beginner....Decimal/Octal converter help gazelle@shell.xmission.com (Kenny McCormack) - 2022-09-09 13:30 +0000
Re: Beginner....Decimal/Octal converter help Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-09-09 09:16 -0700
Re: Beginner....Decimal/Octal converter help scott@slp53.sl.home (Scott Lurndal) - 2022-09-09 19:06 +0000
Re: Beginner....Decimal/Octal converter help David Brown <david.brown@hesbynett.no> - 2022-09-10 11:58 +0200
Re: Beginner....Decimal/Octal converter help Bart <bc@freeuk.com> - 2022-09-10 11:37 +0100
Re: Beginner....Decimal/Octal converter help David Brown <david.brown@hesbynett.no> - 2022-09-10 13:05 +0200
Re: Beginner....Decimal/Octal converter help scott@slp53.sl.home (Scott Lurndal) - 2022-09-10 14:42 +0000
Re: Beginner....Decimal/Octal converter help Bart <bc@freeuk.com> - 2022-09-10 16:10 +0100
Re: Beginner....Decimal/Octal converter help David Brown <david.brown@hesbynett.no> - 2022-09-10 18:14 +0200
Re: Beginner....Decimal/Octal converter help Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-09-10 12:54 +0100
Re: Beginner....Decimal/Octal converter help David Brown <david.brown@hesbynett.no> - 2022-09-10 16:22 +0200
Re: Beginner....Decimal/Octal converter help Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-09-10 19:03 +0100
Re: Beginner....Decimal/Octal converter help Bart <bc@freeuk.com> - 2022-09-10 19:30 +0100
Re: Beginner....Decimal/Octal converter help Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-09-10 19:47 +0100
Re: Beginner....Decimal/Octal converter help David Brown <david.brown@hesbynett.no> - 2022-09-11 14:07 +0200
Re: Beginner....Decimal/Octal converter help Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-09-11 16:12 +0100
Re: Beginner....Decimal/Octal converter help David Brown <david.brown@hesbynett.no> - 2022-09-11 17:58 +0200
Re: Beginner....Decimal/Octal converter help Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-09-11 21:14 +0100
Re: Beginner....Decimal/Octal converter help Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-09-11 20:01 -0700
Re: Beginner....Decimal/Octal converter help Richard Damon <Richard@Damon-Family.org> - 2022-09-11 23:32 -0400
Re: Beginner....Decimal/Octal converter help scott@slp53.sl.home (Scott Lurndal) - 2022-09-12 13:52 +0000
Re: Beginner....Decimal/Octal converter help Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-09-14 03:24 -0700
Re: Beginner....Decimal/Octal converter help Richard Damon <Richard@Damon-Family.org> - 2022-09-14 08:10 -0400
Re: Beginner....Decimal/Octal converter help Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-12-26 06:09 -0800
Re: Beginner....Decimal/Octal converter help David Brown <david.brown@hesbynett.no> - 2022-09-12 16:53 +0200
Re: Beginner....Decimal/Octal converter help Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-09-12 16:48 +0100
Re: Beginner....Decimal/Octal converter help Bart <bc@freeuk.com> - 2022-09-12 17:46 +0100
Re: Beginner....Decimal/Octal converter help Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-09-12 20:37 +0100
Re: Beginner....Decimal/Octal converter help Bart <bc@freeuk.com> - 2022-09-13 00:03 +0100
Re: Beginner....Decimal/Octal converter help David Brown <david.brown@hesbynett.no> - 2022-09-13 12:31 +0200
Re: Beginner....Decimal/Octal converter help Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-09-13 12:48 +0100
Re: Beginner....Decimal/Octal converter help Bart <bc@freeuk.com> - 2022-09-13 14:18 +0100
Re: Beginner....Decimal/Octal converter help Anton Shepelev <anton.txt@g{oogle}mail.com> - 2022-09-13 17:13 +0300
Re: Beginner....Decimal/Octal converter help Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-09-13 16:50 +0100
Re: Beginner....Decimal/Octal converter help Bart <bc@freeuk.com> - 2022-09-13 18:47 +0100
Re: Beginner....Decimal/Octal converter help Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-09-14 03:16 +0100
Re: Beginner....Decimal/Octal converter help David Brown <david.brown@hesbynett.no> - 2022-09-13 20:15 +0200
Re: Beginner....Decimal/Octal converter help Kaz Kylheku <480-992-1380@kylheku.com> - 2022-09-13 20:29 +0000
Re: Beginner....Decimal/Octal converter help Bart <bc@freeuk.com> - 2022-09-13 22:22 +0100
Re: Beginner....Decimal/Octal converter help Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-09-13 14:37 -0700
Re: Beginner....Decimal/Octal converter help Bart <bc@freeuk.com> - 2022-09-13 23:16 +0100
Re: Beginner....Decimal/Octal converter help scott@slp53.sl.home (Scott Lurndal) - 2022-09-13 22:16 +0000
Re: Beginner....Decimal/Octal converter help Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-09-13 16:15 -0700
Re: Beginner....Decimal/Octal converter help Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-09-13 16:15 -0700
Re: Beginner....Decimal/Octal converter help Bart <bc@freeuk.com> - 2022-09-14 10:58 +0100
Re: Beginner....Decimal/Octal converter help Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-09-14 15:36 +0100
Re: Beginner....Decimal/Octal converter help Richard Harnden <richard.nospam@gmail.com> - 2022-09-14 21:53 +0100
Re: Beginner....Decimal/Octal converter help Kaz Kylheku <480-992-1380@kylheku.com> - 2022-09-14 05:55 +0000
Re: Beginner....Decimal/Octal converter help David Brown <david.brown@hesbynett.no> - 2022-09-14 10:10 +0200
Re: Beginner....Decimal/Octal converter help Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-09-14 07:54 -0700
Re: Beginner....Decimal/Octal converter help David Brown <david.brown@hesbynett.no> - 2022-09-14 18:32 +0200
Re: Beginner....Decimal/Octal converter help Bart <bc@freeuk.com> - 2022-09-14 18:39 +0100
Re: Beginner....Decimal/Octal converter help Kaz Kylheku <480-992-1380@kylheku.com> - 2022-09-15 01:12 +0000
Re: Beginner....Decimal/Octal converter help David Brown <david.brown@hesbynett.no> - 2022-09-15 09:11 +0200
Re: Beginner....Decimal/Octal converter help Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-09-15 08:07 -0700
Re: Beginner....Decimal/Octal converter help Kaz Kylheku <480-992-1380@kylheku.com> - 2022-09-14 18:40 +0000
Re: Beginner....Decimal/Octal converter help Kaz Kylheku <480-992-1380@kylheku.com> - 2022-09-14 05:45 +0000
Re: Beginner....Decimal/Octal converter help Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-09-14 12:11 +0100
Re: Beginner....Decimal/Octal converter help David Brown <david.brown@hesbynett.no> - 2022-09-14 09:57 +0200
Re: Beginner....Decimal/Octal converter help Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-09-14 03:09 +0100
Re: Beginner....Decimal/Octal converter help David Brown <david.brown@hesbynett.no> - 2022-09-14 10:29 +0200
Re: Beginner....Decimal/Octal converter help Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-09-14 15:14 +0100
Re: Beginner....Decimal/Octal converter help Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-09-14 07:36 -0700
Re: Beginner....Decimal/Octal converter help Öö Tiib <ootiib@hot.ee> - 2022-10-03 01:10 -0700
Re: Beginner....Decimal/Octal converter help Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-10-03 11:36 +0100
Re: Beginner....Decimal/Octal converter help Öö Tiib <ootiib@hot.ee> - 2022-10-04 02:13 -0700
Re: Beginner....Decimal/Octal converter help Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-10-04 16:27 +0100
Re: Beginner....Decimal/Octal converter help Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-09-13 11:48 -0700
Re: Beginner....Decimal/Octal converter help David Brown <david.brown@hesbynett.no> - 2022-09-14 10:38 +0200
Re: Beginner....Decimal/Octal converter help scott@slp53.sl.home (Scott Lurndal) - 2022-09-14 14:27 +0000
Re: Beginner....Decimal/Octal converter help Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-09-14 07:58 -0700
Re: Beginner....Decimal/Octal converter help Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-09-16 13:06 -0700
Re: Beginner....Decimal/Octal converter help Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-09-16 13:16 -0700
Re: Beginner....Decimal/Octal converter help scott@slp53.sl.home (Scott Lurndal) - 2022-09-12 17:55 +0000
Re: Beginner....Decimal/Octal converter help Kaz Kylheku <480-992-1380@kylheku.com> - 2022-09-12 18:22 +0000
Re: Beginner....Decimal/Octal converter help Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-09-12 20:41 +0100
Re: Beginner....Decimal/Octal converter help Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-09-11 15:45 -0700
Re: Beginner....Decimal/Octal converter help Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-09-11 15:41 -0700
Re: Beginner....Decimal/Octal converter help David Brown <david.brown@hesbynett.no> - 2022-09-12 17:06 +0200
Re: Beginner....Decimal/Octal converter help Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-09-11 06:46 -0700
Re: Beginner....Decimal/Octal converter help Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-09-11 06:57 -0700
Re: Beginner....Decimal/Octal converter help Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-09-11 07:07 -0700
Neologism (Was: Beginner....Decimal/Octal converter help) gazelle@shell.xmission.com (Kenny McCormack) - 2022-09-11 14:18 +0000
Re: Beginner....Decimal/Octal converter help scott@slp53.sl.home (Scott Lurndal) - 2022-09-11 16:33 +0000
Re: Beginner....Decimal/Octal converter help Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-09-13 15:40 -0700
Re: Beginner....Decimal/Octal converter help scott@slp53.sl.home (Scott Lurndal) - 2022-09-09 23:08 +0000
Re: Beginner....Decimal/Octal converter help Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-09-11 06:37 -0700
Re: Beginner....Decimal/Octal converter help scott@slp53.sl.home (Scott Lurndal) - 2022-09-11 16:31 +0000
Re: Beginner....Decimal/Octal converter help Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-09-13 15:44 -0700
csiph-web