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


Groups > comp.lang.c > #169273

Re: FAQ list update: Q1.1: choosing types

From Tim Rentsch <tr.17687@z991.linuxsc.com>
Newsgroups comp.lang.c
Subject Re: FAQ list update: Q1.1: choosing types
Date 2023-02-15 02:54 -0800
Organization A noiseless patient Spider
Message-ID <865yc3ci6a.fsf@linuxsc.com> (permalink)
References <2023Feb13.1015.scs.0001@quinine2.local>

Show all headers | View raw


scs@eskimo.com (Steve Summit) writes:

> In its very first question, ["How should I decide which integer
> type to use?"] the FAQ list currently says:
>
>   [long answer]
>
> Besides cleaning up the wording (this answer, like much of my
> writing, is afflicted with Too Many Parentheticals :-) ),

First, as you indirectly point out, this question is actually two
questions, namely, which representation should be chosen, and
which name should be used (e.g., using typedef).  It's important
IMO to identify this dichotomy right up front, and not just leave
it as an afterthought.

Second, for the representation part of the question, the easy and
safe answer is simply to use long long in cases where a signed
type is needed, and unsigned long long in cases where an unsigned
type is needed.  That choice works pretty well in most "normal"
environments or situations.

Third, in many cases the choice is determined by whatever outside
functions or libraries are being used.  For example, when using
stdio.h functions, characters are returned as 'int', and 'int' is
what should be used in conjunction with those functions.  (It may
be worth considering whether to combine this point with the next
one.)

Fourth, after that there are any number of various situations or
environments that deserve being addressed, which I think should
be done by spinning them off into subquestions.  (Note that doing
this should ameliorate the problem of Too Many Parentheticals.)

Fifth, there are several schools of thought about whether to
prefer signed types or unsigned types in various common code
patterns, as for example indexing, and that issue should be
mentioned and addressed explicitly.  Obviously this aspect can
and probably should be a separate question.

> I am
> strongly considering making one or two additional, potentially
> controversial points:
>
>     If you want to make the considered assumption that your
>     code will only ever run on "modern" hardware, you can say
>     that plain int can hold up to +-2147483647 (that is, has
>     32 bits).

If you think this point is important to mention, it should be
brought up not at the top level but under one of the followon
questions under "Fourth" above.

>     Some coding guidelines, notably MISRA, deprecate the
>     "abstract" types short, int, and long, and mandate always
>     using exact-size types like int16_t and int32_t.

Clearly a separate question, and maybe even a separate category
of questions.  How to make good choices for integer types is
both an important area of discussion and a large area of
discussion.  Such important subtopics should be addressed
individually and not just mentioned in passing.  Of course,
it is perfectly reasonable (and IMO desirable) to give a
"see also" reference for these sorts of concerns.

> To the first point, believe me, I do know what the Standard
> guarantees about type int, and I remember well the pejorative
> phrase "All the world's a VAX" as promulgated in, among other
> things, Henry Spencer's 10 Commandments.  But I also know that
> the codebase I work on every day in my day job makes this
> assumption, and I'm pretty sure I'm not alone.

I concur, with the understanding that this point is a subpoint
in a separate question rather than being brought up in the
initial question.

> To the second point, while I personally disagree up, down, and
> sideways with the dictum "don't use plain int", I can't ignore the
> fact that many practicing C programmers do advocate and follow it,
> and many projects faithfully honor MISRA.

A question like "Is it okay just to use plain int?" is a good
question, but it should be a subsidiary question.

Regarding MISRA, does the FAQ have a separate section for style
guides and coding standards?  Presuming it does (and IMO it
should), any answers that refer to MISRA should be put there.

> Opinions (in support or denigration of either point) welcome.

I hope my comments have provided some value to your efforts.

You are welcome to use any of my writing above freely, with the
understanding that I reserve all original rights to myself, as
for example reuse, claim of original authorship, etc.

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


Thread

FAQ list update: Q1.1: choosing types scs@eskimo.com (Steve Summit) - 2023-02-13 15:18 +0000
  Re: FAQ list update: Q1.1: choosing types Anton Shepelev <anton.txt@g{oogle}mail.com> - 2023-02-13 18:27 +0300
  Re: FAQ list update: Q1.1: choosing types scott@slp53.sl.home (Scott Lurndal) - 2023-02-13 16:27 +0000
    Re: FAQ list update: Q1.1: choosing types Oğuz <oguzismailuysal@gmail.com> - 2023-02-14 08:55 +0300
  Re: FAQ list update: Q1.1: choosing types Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2023-02-13 18:07 +0000
  Re: FAQ list update: Q1.1: choosing types Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2023-02-13 10:48 -0800
  Re: FAQ list update: Q1.1: choosing types Kaz Kylheku <864-117-4973@kylheku.com> - 2023-02-14 03:10 +0000
    Re: FAQ list update: Q1.1: choosing types Blue-Maned_Hawk <bluemanedhawk@gmail.com> - 2023-02-14 17:33 -0500
  Re: FAQ list update: Q1.1: choosing types Öö Tiib <ootiib@hot.ee> - 2023-02-13 23:24 -0800
  Re: FAQ list update: Q1.1: choosing types bart c <bart4858@gmail.com> - 2023-02-14 06:22 -0800
    Re: FAQ list update: Q1.1: choosing types Blue-Maned_Hawk <bluemanedhawk@gmail.com> - 2023-02-14 17:48 -0500
  Re: FAQ list update: Q1.1: choosing types John Dill <jadill33@gmail.com> - 2023-02-14 08:17 -0800
    Re: FAQ list update: Q1.1: choosing types Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-02-14 12:34 -0800
      Re: FAQ list update: Q1.1: choosing types John Dill <jadill33@gmail.com> - 2023-02-15 10:04 -0800
        Re: FAQ list update: Q1.1: choosing types Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-02-15 10:28 -0800
          Re: FAQ list update: Q1.1: choosing types Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2023-02-15 10:57 -0800
            Re: FAQ list update: Q1.1: choosing types Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-02-15 11:17 -0800
              Re: FAQ list update: Q1.1: choosing types Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-02-15 11:32 -0800
          Re: FAQ list update: Q1.1: choosing types John Dill <jadill33@gmail.com> - 2023-02-15 11:01 -0800
            Re: FAQ list update: Q1.1: choosing types Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-02-15 11:28 -0800
              Re: FAQ list update: Q1.1: choosing types John Dill <jadill33@gmail.com> - 2023-02-15 14:00 -0800
  Re: FAQ list update: Q1.1: choosing types Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-02-15 02:54 -0800
  Re: FAQ list update: Q1.1: choosing types Anton Shepelev <anton.txt@g{oogle}mail.com> - 2023-02-15 14:43 +0300
  Re: FAQ list update: Q1.1: choosing types David Brown <david.brown@hesbynett.no> - 2023-02-15 13:55 +0100
    Re: FAQ list update: Q1.1: choosing types Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2023-02-15 05:31 -0800

csiph-web