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


Groups > comp.lang.c > #395812

Re: signed vs unsigned and gcc -Wsign-conversion

From Tim Rentsch <tr.17687@z991.linuxsc.com>
Newsgroups comp.lang.c
Subject Re: signed vs unsigned and gcc -Wsign-conversion
Date 2025-12-15 00:25 -0800
Organization A noiseless patient Spider
Message-ID <86bjk0up7m.fsf@linuxsc.com> (permalink)
References <10d5j0v$3kdmk$1@dont-email.me>

Show all headers | View raw


pozz <pozzugno@gmail.com> writes:

> After many years programming in C language, I'm always unsure if it is
> safer to use signed int or unsigned int.
>
> Of course there are situations where signed or unsigned is clearly
> better.  For example, if the values could assume negative values,
> signed int is the only solution.  If you are manipulating single bits
> (&, |, ^, <<, >>), unsigned ints are your friends.
>
> What about other situations?  For example, what do you use for the "i"
> loop variable?

I used unsigned types unless there is a compelling reason to use
signed types.  I used unsigned types for counts, array index values,
sizes, lengths, extents, limits of the above, bits and masks.  The
most common reason for using signed types is for compatibility with
some system interface (most commonly, signed int).

There are cases where using an unsigned type rather than a signed
type requires more thought and care.  To me that need is a net
positive rather than a negative.

> I recently activated gcc -Wsign-conversion option on a codebase and
> received a lot of warnings.  I started to fix them, usually expliciting
> casting.  Is it the way or is it better to avoid the warning from the
> beginning, choosing the right signed or unsigned type?

My experience with such warnings is they generate too many false
positives.  I might turn on -Wsign-conversion every now and then
as a sanity check, but not all the time.  The "cure" of changing
the code so the warnings go away is worse than the disease.

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


Thread

signed vs unsigned and gcc -Wsign-conversion pozz <pozzugno@gmail.com> - 2025-10-20 17:03 +0200
  Re: signed vs unsigned and gcc -Wsign-conversion Bonita Montero <Bonita.Montero@gmail.com> - 2025-10-20 17:38 +0200
  Re: signed vs unsigned and gcc -Wsign-conversion Michael S <already5chosen@yahoo.com> - 2025-10-20 19:43 +0300
    Re: signed vs unsigned and gcc -Wsign-conversion Bonita Montero <Bonita.Montero@gmail.com> - 2025-10-20 19:07 +0200
    Re: signed vs unsigned and gcc -Wsign-conversion scott@slp53.sl.home (Scott Lurndal) - 2025-10-20 18:01 +0000
      Re: signed vs unsigned and gcc -Wsign-conversion Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-10-21 04:27 +0200
      Re: signed vs unsigned and gcc -Wsign-conversion David Brown <david.brown@hesbynett.no> - 2025-10-21 09:13 +0200
    Re: signed vs unsigned and gcc -Wsign-conversion BGB <cr88192@gmail.com> - 2025-10-20 17:44 -0500
    Re: signed vs unsigned and gcc -Wsign-conversion Lawrence D’Oliveiro <ldo@nz.invalid> - 2025-10-20 23:36 +0000
      Re: signed vs unsigned and gcc -Wsign-conversion Kaz Kylheku <643-408-1753@kylheku.com> - 2025-10-20 23:52 +0000
      Re: signed vs unsigned and gcc -Wsign-conversion Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-10-20 16:58 -0700
  Re: signed vs unsigned and gcc -Wsign-conversion David Brown <david.brown@hesbynett.no> - 2025-10-20 20:03 +0200
    Re: signed vs unsigned and gcc -Wsign-conversion Kaz Kylheku <643-408-1753@kylheku.com> - 2025-10-20 20:09 +0000
      Re: signed vs unsigned and gcc -Wsign-conversion rbowman <bowman@montana.com> - 2025-10-21 01:43 +0000
      Re: signed vs unsigned and gcc -Wsign-conversion David Brown <david.brown@hesbynett.no> - 2025-10-21 12:42 +0200
        Re: signed vs unsigned and gcc -Wsign-conversion James Kuyper <jameskuyper@alumni.caltech.edu> - 2025-10-21 14:44 -0400
          Re: signed vs unsigned and gcc -Wsign-conversion David Brown <david.brown@hesbynett.no> - 2025-10-21 22:56 +0200
  Re: signed vs unsigned and gcc -Wsign-conversion Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-10-20 14:48 -0700
    Re: signed vs unsigned and gcc -Wsign-conversion Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-10-20 17:13 -0700
      Re: signed vs unsigned and gcc -Wsign-conversion Kaz Kylheku <643-408-1753@kylheku.com> - 2025-10-21 01:45 +0000
        Re: signed vs unsigned and gcc -Wsign-conversion antispam@fricas.org (Waldek Hebisch) - 2025-10-21 03:52 +0000
  Re: signed vs unsigned and gcc -Wsign-conversion Lawrence D’Oliveiro <ldo@nz.invalid> - 2025-10-20 23:35 +0000
    Re: signed vs unsigned and gcc -Wsign-conversion Lawrence D’Oliveiro <ldo@nz.invalid> - 2025-10-20 23:38 +0000
      Re: signed vs unsigned and gcc -Wsign-conversion Bonita Montero <Bonita.Montero@gmail.com> - 2025-10-21 09:57 +0200
        Re: signed vs unsigned and gcc -Wsign-conversion Lawrence D’Oliveiro <ldo@nz.invalid> - 2025-10-21 19:45 +0000
  Re: signed vs unsigned and gcc -Wsign-conversion antispam@fricas.org (Waldek Hebisch) - 2025-10-21 04:42 +0000
  Re: signed vs unsigned and gcc -Wsign-conversion Tim Rentsch <tr.17687@z991.linuxsc.com> - 2025-12-15 00:25 -0800

csiph-web