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


Groups > comp.lang.c > #67002

Re: Untyped Variables

From James Kuyper <jameskuyper@verizon.net>
Newsgroups comp.lang.c
Subject Re: Untyped Variables
Date 2015-08-08 11:09 -0400
Organization Self
Message-ID <55C61BBF.8090302@verizon.net> (permalink)
References <83a58dd3-53a6-422a-8502-4fef870baf70@googlegroups.com>

Show all headers | View raw


On 08/07/2015 09:19 PM, David Kleinecke wrote:
> In another newsgroup it was stated that C will accept the declaration
>    a;
> and create an integer variable.
> 
> As I read the standard (I used 1990) this is not possible in the syntax.
> By 6.5 a declaration must start with "declaration specifiers" so a bare
> "a;" is a syntax error. So far so good.
> 
> The reason I am bringing this up is that
>    auto a;
>    constant a;

I presume you mean 'const' rather than 'constant'?

>       etc. 
> are not syntactic errors. I feel sure the standard addresses these
> structures (and outlaws them - as undefined perhaps) but I can't
> find where it does. Perhaps someone will direct me to the
> appropriate place in the standard.

The feature that those declarations rely upon is "implicit int", and it
is a feature only of your preferred version of the C standard,
C89(==C90). I've never owned a copy of that version of the standard, so
I can't tell you precisely how this feature is defined, but I'll do the
best that I can, by comparing with the current version of the standard.

In C2011, the standard imposes the following constraint (6.7.2p2):
> At least one type specifier shall be given in the declaration specifiers in each declaration,
> and in the specifier-qualifier list in each struct declaration and type name. Each list of
> type specifiers shall be one of the following multisets (delimited by commas, when there
> is more than one multiset per item); the type specifiers may occur in any order, possibly
> intermixed with the other declaration specifiers.
> — void
> — char
> — signed char
> — unsigned char
> — short, signed short, short int, or signed short int
> — unsigned short, or unsigned short int
> — int, signed, or signed int
> — unsigned, or unsigned int
> — long, signed long, long int, or signed long int
> — unsigned long, or unsigned long int
> — long long, signed long long, long long int, or
> signed long long int
> — unsigned long long, or unsigned long long int
> — float
> — double
> — long double
> — _Bool
> — float _Complex
> — double _Complex
> — long double _Complex
> — atomic type specifier
> — struct or union specifier
> — enum specifier
> — typedef name


Note: 'long long', _Bool and _Complex were introduced in C99, and
support for _Complex is optional. "atomic types" other than sig_atomic_t
were not added until C2011, where it became urgent in combination with
C2011's support for multi-threaded programs.

If you examine that list, you'll find every one of those multisets that
applied in C90 and doesn't involve a user-defined identifier includes at
least one of the following five keywords: void, char, int, float,
double. My understanding of the implicit int feature is that, in C90,

a) There was no wording equivalent to the first sentence of 6.7.2p2.

b) Any declaration that doesn't include one of those five keywords was
implicitly treated as though it included 'int'. There is a residue of
this rule in more recent standards, in the fact that, for every
multi-set that contains both 'int' and at least one other
type-specifier, there is another multi-set with the same meaning that
differs only by the removal of 'int'.

c) Any use of a previously undeclared identifier was treated, if
possible, as if it were the name of a function with external linkage
returning 'int'. If arguments were given for the function, it was
treated as though the corresponding declaration had parameters of the
same type.

Thus
	auto a;
	const b;
	c;

Were equivalent to
	auto int a;
	const int b;
	int c();
	c;	// expression statement consisting of the name of a
 		// function, implicitly converted to a pointer to the
		// function type. Useless, but legal.

The implicit-int feature reflects the fact that C was developed from
earlier languages in which there was only one integer type. Since C very
quickly acquired a number of other types, this feature has been the
cause of innumerable errors, and is now considered to be a mistake. When
I first learned C in 1979, I was taught how to understand the
implicit-int rule, but I was also taught that it was a bad idea: I
should avoid writing code that relied upon it, and if I was in a
position to do so, I should correct existing code that relied upon it.
Despite the fact that awareness of the problem existed that far back, it
was not until C99 that implicit int was removed from the standard.

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


Thread

Untyped Variables David Kleinecke <dkleinecke@gmail.com> - 2015-08-07 18:19 -0700
  Re: Untyped Variables Lőrinczy Zsigmond <nospam@for.me> - 2015-08-08 06:53 +0200
  Re: Untyped Variables James Kuyper <jameskuyper@verizon.net> - 2015-08-08 11:09 -0400
    Re: Untyped Variables Keith Thompson <kst-u@mib.org> - 2015-08-08 12:09 -0700
      Re: Untyped Variables James Kuyper <jameskuyper@verizon.net> - 2015-08-08 14:24 -0400
    Re: Untyped Variables Keith Thompson <kst-u@mib.org> - 2015-08-08 12:54 -0700
      Re: Untyped Variables David Kleinecke <dkleinecke@gmail.com> - 2015-08-08 16:06 -0700
        Re: Untyped Variables Keith Thompson <kst-u@mib.org> - 2015-08-08 17:29 -0700
          Re: Untyped Variables David Kleinecke <dkleinecke@gmail.com> - 2015-08-09 17:52 -0700
            Re: Untyped Variables Ian Collins <ian-news@hotmail.com> - 2015-08-10 12:57 +1200
            Re: Untyped Variables Keith Thompson <kst-u@mib.org> - 2015-08-10 08:41 -0700
              Re: Untyped Variables David Kleinecke <dkleinecke@gmail.com> - 2015-08-10 17:06 -0700
        Re: Untyped Variables Richard Damon <Richard@Damon-Family.org> - 2015-08-08 19:44 -0400
        Re: Untyped Variables James Kuyper <jameskuyper@verizon.net> - 2015-08-08 20:34 -0400
          Re: Untyped Variables glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-08-09 02:03 +0000
            Re: Untyped Variables Richard Damon <Richard@Damon-Family.org> - 2015-08-08 22:52 -0400
              Re: Untyped Variables glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-08-09 04:57 +0000
                Re: Untyped Variables Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2015-08-09 09:58 -0600
                Re: Untyped Variables Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-08-10 03:54 -0700
                Re: Untyped Variables Richard Damon <Richard@Damon-Family.org> - 2015-08-09 16:59 -0400
          Re: Untyped Variables David Kleinecke <dkleinecke@gmail.com> - 2015-08-09 18:02 -0700
            Re: Untyped Variables James Kuyper <jameskuyper@verizon.net> - 2015-08-10 11:01 -0400
              Re: Untyped Variables James Kuyper <jameskuyper@verizon.net> - 2015-08-10 13:20 -0400
              Re: Untyped Variables David Kleinecke <dkleinecke@gmail.com> - 2015-08-10 16:54 -0700
                Re: Untyped Variables James Kuyper <jameskuyper@verizon.net> - 2015-08-11 00:20 -0400
            Re: Untyped Variables Keith Thompson <kst-u@mib.org> - 2015-08-10 08:11 -0700
              Re: Untyped Variables David Kleinecke <dkleinecke@gmail.com> - 2015-08-10 17:15 -0700
                Re: Untyped Variables James Kuyper <jameskuyper@verizon.net> - 2015-08-11 00:30 -0400
                Re: Untyped Variables David Kleinecke <dkleinecke@gmail.com> - 2015-08-11 16:50 -0700
                Re: Untyped Variables Keith Thompson <kst-u@mib.org> - 2015-08-11 17:24 -0700
                Re: Untyped Variables Richard Heathfield <rjh@cpax.org.uk> - 2015-08-12 01:37 +0100
                Re: Untyped Variables glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-08-12 04:46 +0000
                Re: Untyped Variables David Kleinecke <dkleinecke@gmail.com> - 2015-08-12 18:12 -0700
                Re: Untyped Variables Keith Thompson <kst-u@mib.org> - 2015-08-13 12:08 -0700
                Re: Untyped Variables David Kleinecke <dkleinecke@gmail.com> - 2015-08-13 16:25 -0700
                Re: Untyped Variables Keith Thompson <kst-u@mib.org> - 2015-08-14 10:36 -0700
                Re: Untyped Variables Bartc <bc@freeuk.com> - 2015-08-14 19:13 +0100
                Re: Untyped Variables Keith Thompson <kst-u@mib.org> - 2015-08-11 07:58 -0700
          Re: Untyped Variables richard@cogsci.ed.ac.uk (Richard Tobin) - 2015-08-10 13:53 +0000
            Re: Untyped Variables Keith Thompson <kst-u@mib.org> - 2015-08-10 08:29 -0700
              Re: Untyped Variables David Kleinecke <dkleinecke@gmail.com> - 2015-08-10 17:01 -0700
                Re: Untyped Variables Keith Thompson <kst-u@mib.org> - 2015-08-10 17:42 -0700
                Re: Untyped Variables glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-08-11 02:56 +0000
            Re: Untyped Variables David Kleinecke <dkleinecke@gmail.com> - 2015-08-10 16:50 -0700
              Re: Untyped Variables Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-08-11 01:39 +0100
                Re: Untyped Variables Keith Thompson <kst-u@mib.org> - 2015-08-10 19:03 -0700
                Re: Untyped Variables Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-08-11 11:21 +0100
                Re: Untyped Variables David Kleinecke <dkleinecke@gmail.com> - 2015-08-11 16:37 -0700

csiph-web