'C' language question

'C' language question

Post by Doug Jone » Fri, 20 Aug 2004 00:02:03


I'm using LCCwin32 and Windows 'ME'

Is it correct for the compiler to not emit an error or warning
when variable names are duplicated?

as in:
snip...
static RECT XX;
snip...
static RECT XX;
snip...

Thanks for the lesson
DJ
 
 
 

'C' language question

Post by Dave Thomp » Fri, 27 Aug 2004 14:22:21

On Wed, 18 Aug 2004 10:02:03 -0500, "Doug Jones"


If those are both at file scope -- and assuming of course RECT is a
typedef, or a macro that expands to a valid type-specifier-list --
they are what the C standard classifies as 'tentative definitions' and
it is legal to have multiple of those in one translation unit as long
as they have the same (or actually, compatible) type. So an error
would definitely be wrong. Whether a warning would be desirable is up
to the implementors, in this case Jacob, and/or customers/users.

There are definitely cases where you need a tentative definition
followed by an actual definition, so I wouldn't like to see a warning
for that.

If they are at the same block scope, it's not legal C and yes should
be an error, and is on my copy at least.

- David.Thompson1 at worldnet.att.net

 
 
 

'C' language question

Post by Doug Jone » Fri, 27 Aug 2004 21:19:42

Thanks Dave
I was beginning to think that no one cared.
This nothing program compiles-links and runs without comment from LCC.
I would like a warning for these dups. Especially when the dups are
interspersed in a long long list of globals.
//-------------------------
#include <stdio.h>
static int XX;
static int XX;
static int XX;
static int XX;
int main(int argc, char *argv[])
{
XX++;
printf("%d", XX);
return 0;
}
//------------------------
Doug Jones
 
 
 

'C' language question

Post by iddw » Fri, 27 Aug 2004 22:42:24

On Thu, 26 Aug 2004 07:19:42 -0500, "Doug Jones"



Running your code through PC-lint I get (among other errors):

--- begin included file ---
static int XX;
mult.c 3 Error 31: Redefinition of symbol 'XX' compare with line 2
mult.c 2 Info 830: Location cited in prior message
_
static int XX;
mult.c 4 Error 31: Redefinition of symbol 'XX' compare with line 2
mult.c 2 Info 830: Location cited in prior message
_
static int XX;
mult.c 5 Error 31: Redefinition of symbol 'XX' compare with line 2
mult.c 2 Info 830: Location cited in prior message
--- end included file ---

Lint early. Lint often. Lint is your friend. Regards,

-=Dave
--
Change is inevitable, progress is not.
 
 
 

'C' language question

Post by Doug Jone » Fri, 27 Aug 2004 23:00:24


You're right Dave - One should Lint often - however it just seems to me
that the compiler should catch this and I was curious about the standard
reguirements of a compiler.
DJ
 
 
 

'C' language question

Post by CBFalcone » Fri, 27 Aug 2004 23:46:00


... snip ...

If you had refrained from the evil topposting (fixed) you would
have seen the quoted sentences from Dave which explain why you
should not get such.

--
Chuck F ( XXXX@XXXXX.COM ) ( XXXX@XXXXX.COM )
Available for consulting/temporary embedded and systems.
< http://www.yqcomputer.com/ > USE worldnet address!
 
 
 

'C' language question

Post by iddw » Sat, 28 Aug 2004 00:01:20

On Thu, 26 Aug 2004 09:00:24 -0500, "Doug Jones"


[...re: multiple "static int XX;" at file scope...]

Section 6.9.2 (External Object Definitions) p2 and p3 say:

"A declaration of an identifier for an object that has file scope
without an initializer, and without a storage-class specifier or
with the storage-class specifier static, constitutes a tentative
definition. If a translation unit contains one or more tentative
definitions for an identifier, and the translation unit contains
no external definition for that identifier, then the behavior is
exactly as if the translation unit contains a file scope
declaration of that identifier, with the composite type as of the
end of the translation unit, with an initializer equal to 0.

"If the declaration of an identifier for an object is a tentative
definition and has internal linkage, the declared type shall not
be an incomplete type."

"static int XX:" is a declaration of an identifier for an object that
has file scope, has no initializer, and has only the storage-class
specifier "static." Furthermore, "int" is not an incomplete type. So
it is a "tentative definition." Multiple tentative definitions are
legal. The compiler is not required to complain.

The compiler is _allowed_ to complain, but it is not allowed to refuse
to compile the code. Such diagnostics are QoI issues.

If one of the definitions disagreed with another (e.g., different type
or not static), or more than one initialized the object (even if the
initializers were identical), then diagnostics would be required, and
the compiler could refuse to compile.

Do you get a diagnostic if you change the declarations to

static int XX = 0;

?

Regards,

-=Dave
--
Change is inevitable, progress is not.
 
 
 

'C' language question

Post by Doug Jone » Sat, 28 Aug 2004 01:55:41


Picky Picky Picky.

Would have seen what? Jeesh!
DJ
 
 
 

'C' language question

Post by Doug Jone » Sat, 28 Aug 2004 02:01:14


Initializing more than one of them as in the following causes errors.
#include <stdio.h>
static int XX = 1;
static int XX = 1;
static int XX;
static int XX;
int main(int argc, char *argv[])
{
XX++;
printf("%d", XX);
return 0;
}
My curiosity is satisfied now so Thanks again;
DougJ