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


Groups > comp.lang.c > #146055

Re: Question about use (and non-use) of "extern"

From Tim Rentsch <tr.17687@z991.linuxsc.com>
Newsgroups comp.lang.c
Subject Re: Question about use (and non-use) of "extern"
Date 2019-09-06 02:25 -0700
Organization A noiseless patient Spider
Message-ID <86v9u5wzcv.fsf@mailhub.linuxsc.com> (permalink)
References <qjgric$8u2$1@news.xmission.com> <qk49be$ac$1@dont-email.me> <86tv9ty1t4.fsf@mailhub.linuxsc.com> <qklqqd$u5t$1@dont-email.me> <lnblw1w8nl.fsf@kst-u.example.com>

Show all headers | View raw


Keith Thompson <kst-u@mib.org> writes:

> David Brown <david.brown@hesbynett.no> writes:
> [...]
>
>> If you have "int x;" in a translation unit (without any "static"
>> involved), that counts as an external definition of "x" with external
>> linkage.  (6.9.2p2 and 6.9.2p1).  If this identifier is used in an
>> expression (other than an unevaluated expression, such as a sizeof),
>> there must be exactly one definition of it (6.9p5).  These are "shall"
>> rules - they are as strong as the standards get.

David Brown: never in doubt, often in error.

> The C standard uses "shall" in two very different ways.
>
> A violation of a "shall" in a constraint (all constraints are
> explicitly marked as such) requires a diagnostic, just like a syntax
> error.  *That's* as close as the C standard gets to saying something
> is illegal/invalid/disallowed (aside from #error).
>
> A violation of a "shall" outside a constraint, like the one in
> 6.9p5, cause the code to have undefined behavior, with no diagnostic
> required.  In some cases, such rules can leave room for common
> extensions.

There is one, and only one, code situation that the ISO C
standard disallows:  accepting a translation unit that has an
unskipped #error directive.  (I have a vague memory of there
being something similar to this related to #pragma, but looking
at the Standard I couldn't find anything.  If someone finds
something I would appreciate hearing about it.)

After that, the next level of severity is situations that require
a diagnostic:  syntax errors and violating a 'shall' given as a
Constraint.  In practical terms this category may be subdivided
into two cases:  constructions that never have practical utility
(and we may reasonably expect that they are flagged as fatal
errors in most if not all implementations);  and constructions
that have a commonly held "misinterpretation" and which are often
used deliberately either in a nearly conforming mode or in a
conforming mode with a willingness to tolerate the mandated
warning.  An example of the latter case is the ?: operator,
available as an extension in both gcc and clang (and undoubtedly
others) where it does not give even a warning unless -pedantic is
used.

There are also a few other specific cases where diagnostics are
either recommended or required.  (I mention these only for the
sake of completeness.)

Situations that require a diagnostic are allowed in the sense
that they may be compiled and run if an implementation chooses to
do so, but the implementation must at least give notice that a
rule has been violated.

Nothing else is deemed "illegal" or "invalid" by the C standard.
Different constructions give implementations varying degrees of
freedom as to how they may be interpreted, including complete
freedom to do anything at all which is the case for multiply
defined global variables.  Even these extreme cases (the C
standard calls them "undefined behavior") are not in any sense
illegal or invalid.  Indeed, most if not all have been used
deliberately to accomplish something useful in a particular
environment even though they cannot be relied on in all (or even
most) environments.

Short summary:  if you want to write C code that works no matter
which platform it will be compiled and run on, there are a lot
of things to stay away from.  If the goal is more limited and
more modest, in many cases various "forbidden" constructions
will be just fine.

(Incidentally, I agree with what Keith was saying.  It seemed
worthwhile to approach the same subject area from a different
perspective.)

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


Thread

Question about use (and non-use) of "extern" gazelle@shell.xmission.com (Kenny McCormack) - 2019-08-20 13:11 +0000
  Re: Question about use (and non-use) of "extern" David Brown <david.brown@hesbynett.no> - 2019-08-20 16:09 +0200
  Re: Question about use (and non-use) of "extern" Bart <bc@freeuk.com> - 2019-08-20 16:41 +0100
    Re: Question about use (and non-use) of "extern" Bart <bc@freeuk.com> - 2019-08-20 16:47 +0100
    Re: Question about use (and non-use) of "extern" David Brown <david.brown@hesbynett.no> - 2019-08-20 19:27 +0200
      Re: Question about use (and non-use) of "extern" Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2019-08-20 13:54 -0400
        Re: Question about use (and non-use) of "extern" Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-08-21 02:09 +0100
        Re: Question about use (and non-use) of "extern" David Brown <david.brown@hesbynett.no> - 2019-08-21 10:54 +0200
      Re: Question about use (and non-use) of "extern" Bart <bc@freeuk.com> - 2019-08-21 00:41 +0100
        Re: Question about use (and non-use) of "extern" David Brown <david.brown@hesbynett.no> - 2019-08-21 10:58 +0200
  Re: Question about use (and non-use) of "extern" richard@cogsci.ed.ac.uk (Richard Tobin) - 2019-08-20 16:10 +0000
    Re: Question about use (and non-use) of "extern" Siri Cruise <chine.bleu@yahoo.com> - 2019-08-20 16:23 -0700
  Re: Question about use (and non-use) of "extern" already5chosen@yahoo.com - 2019-08-21 06:50 -0700
    Re: Question about use (and non-use) of "extern" gazelle@shell.xmission.com (Kenny McCormack) - 2019-08-22 20:20 +0000
      Re: Question about use (and non-use) of "extern" David Brown <david.brown@hesbynett.no> - 2019-08-23 00:25 +0200
      Re: Question about use (and non-use) of "extern" Bart <bc@freeuk.com> - 2019-08-23 01:35 +0100
        Re: Question about use (and non-use) of "extern" gazelle@shell.xmission.com (Kenny McCormack) - 2019-08-23 03:40 +0000
          Re: Question about use (and non-use) of "extern" Bart <bc@freeuk.com> - 2019-08-23 11:09 +0100
        Re: Question about use (and non-use) of "extern" David Brown <david.brown@hesbynett.no> - 2019-08-23 08:16 +0200
      Re: Question about use (and non-use) of "extern" scott@slp53.sl.home (Scott Lurndal) - 2019-08-23 15:02 +0000
        Re: Question about use (and non-use) of "extern" Bart <bc@freeuk.com> - 2019-08-23 17:06 +0100
          Re: Question about use (and non-use) of "extern" gazelle@shell.xmission.com (Kenny McCormack) - 2019-08-23 18:40 +0000
          Re: Question about use (and non-use) of "extern" scott@slp53.sl.home (Scott Lurndal) - 2019-08-23 20:38 +0000
            Re: Question about use (and non-use) of "extern" Richard Damon <Richard@Damon-Family.org> - 2019-08-23 17:55 -0400
              Richard Damon weighs in... (Was: Question about use (and non-use) of "extern") gazelle@shell.xmission.com (Kenny McCormack) - 2019-08-23 23:50 +0000
  Re: Question about use (and non-use) of "extern" Andrey Tarasevich <andreytarasevich@hotmail.com> - 2019-08-27 15:03 -0700
    Re: Question about use (and non-use) of "extern" Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-09-03 05:57 -0700
      Re: Question about use (and non-use) of "extern" David Brown <david.brown@hesbynett.no> - 2019-09-03 15:45 +0200
        Re: Question about use (and non-use) of "extern" Keith Thompson <kst-u@mib.org> - 2019-09-03 11:13 -0700
          Re: Question about use (and non-use) of "extern" David Brown <david.brown@hesbynett.no> - 2019-09-04 12:11 +0200
            Re: Question about use (and non-use) of "extern" James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-09-04 08:18 -0400
          Re: Question about use (and non-use) of "extern" Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-09-06 02:25 -0700
            Re: Question about use (and non-use) of "extern" David Brown <david.brown@hesbynett.no> - 2019-09-06 11:45 +0200
          Re: Question about use (and non-use) of "extern" Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-09-06 21:15 +0100
        Re: Question about use (and non-use) of "extern" James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-09-03 21:10 -0400
          Re: Question about use (and non-use) of "extern" David Brown <david.brown@hesbynett.no> - 2019-09-04 12:21 +0200
      Re: Question about use (and non-use) of "extern" Andrey Tarasevich <andreytarasevich@hotmail.com> - 2019-09-04 12:04 -0700
        Re: Question about use (and non-use) of "extern" James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-09-04 15:29 -0400
        Re: Question about use (and non-use) of "extern" Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-09-06 02:46 -0700
  Re: Question about use (and non-use) of "extern" Bonita Montero <Bonita.Montero@gmail.com> - 2019-09-05 10:01 +0200
    Re: Question about use (and non-use) of "extern" Andrey Tarasevich <andreytarasevich@hotmail.com> - 2019-09-05 10:43 -0700
      Re: Question about use (and non-use) of "extern" Bonita Montero <Bonita.Montero@gmail.com> - 2019-09-06 20:24 +0200

csiph-web