EXTERN Qualifier

M

MikeF

Group,

I have a variable declared in 'main.c' as:

const unsigned int x = 1;

I have another module which uses this same variable, I've tried to declare
as:


extern const unsigned int x;


Is this correct????

My compiler complains about using extern with 'const'.


Thanks,
MikeF
*** ***
 
R

ranjmis

MikeF said:
Group,

I have a variable declared in 'main.c' as:

const unsigned int x = 1;

I have another module which uses this same variable, I've tried to declare
as:


extern const unsigned int x;


Is this correct????
I dont know about its specification in C standard
My compiler complains about using extern with 'const'.
but my gcc compiler doesn't complain
 
V

Vladimir S. Oka

MikeF opined:
Group,

I have a variable declared in 'main.c' as:

const unsigned int x = 1;

I have another module which uses this same variable, I've tried to
declare as:


extern const unsigned int x;


Is this correct????

My compiler complains about using extern with 'const'.

Your compiler is either broken, or you're not showing the exact code in
question. There's nothing wrong with what you describe above.

Are you sure, you're not trying to initialise the `extern`ed `x` as
well, as in:

extern unsigned const x = 1;

That /will/ make compiler complain about initialising `extern`ed
`const`.

--
"Are [Linux users] lemmings collectively jumping off of the cliff of
reliable, well-engineered commercial software?"
(By Matt Welsh)

<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
 
M

MikeF

Are you sure, you're not trying to initialise the `extern`ed `x` as
well, as in:

extern unsigned const x = 1;

That /will/ make compiler complain about initialising `extern`ed
`const`.

No, I'm sure I entered the code as originally stated. This is for an
embedded compiler, which is advertised as ANSI compliant, but embedded
compilers don't always follow the rules.

MikeF
*** ***
 
F

Flash Gordon

No, I'm sure I entered the code as originally stated. This is for an
embedded compiler, which is advertised as ANSI compliant, but embedded
compilers don't always follow the rules.

Compilers are allowed to issue diagnostics for anything they like, so
complaining about "extern unsigned const x;" does not make it
non-conforming. Less than helpful possibly, but that is another matter.
If I was you I would check through the compiler options and see if you
can disable this warning.
 
J

Jack Klein

MikeF opined:


Your compiler is either broken, or you're not showing the exact code in
question. There's nothing wrong with what you describe above.

Are you sure, you're not trying to initialise the `extern`ed `x` as
well, as in:

extern unsigned const x = 1;

That /will/ make compiler complain about initialising `extern`ed
`const`.

Not unless _your_ compiler is broken. The following program is
strictly conforming.

#include <stdio.h>

extern unsigned const x = 1;

int main(void)
{
printf("extern unsigned const x = %u\n", x);
return 0;
}

The extern keyword is not as completely redundant on a file scope
object declaration as it is on a function declaration.

In the absence of an initializer, the extern keyword keeps the
declaration from being treated as a tentative definition, which always
results in a full definition by the end of the translation unit.

On an object declaration with an initializer at file scope, the extern
keyword is completely redundant.
 
V

Vladimir S. Oka

Jack Klein opined:
Not unless _your_ compiler is broken. The following program is
strictly conforming.

Had the OP's first line been without the initialiser, you'd be right.

OP had `x` already initialised where it's declared. I offered the above
as the variation on his `extern`ed declaration in a different module.
The two modules will compile, but not link.
#include <stdio.h>

extern unsigned const x = 1;

int main(void)
{
printf("extern unsigned const x = %u\n", x);
return 0;
}

The extern keyword is not as completely redundant on a file scope
object declaration as it is on a function declaration.

In the absence of an initializer, the extern keyword keeps the
declaration from being treated as a tentative definition, which
always results in a full definition by the end of the translation
unit.

On an object declaration with an initializer at file scope, the
extern keyword is completely redundant.

--
Each kiss is as the first.
-- Miramanee, Kirk's wife, "The Paradise Syndrome",
stardate 4842.6

<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
 
M

MikeF

Not unless _your_ compiler is broken. The following program is
strictly conforming.

#include <stdio.h>

extern unsigned const x = 1;

--------------------------------------
Here was my original input:

(main.c)
const unsigned int x = 1;

initialize_vars.c (module)
extern unsigned int x;

When I compiled 'initialize_vars.c' , I received this error:
Error[46] D:\job1020\clock_generator\initialize_vars.c 39 : Expecting an =
D:\job1020\clock_generator\initialize_vars.o ===> 1 Errors, 0 Warnings.

Next I changed 'extern unsigned int x;' to
extern unsigned int x =1;

This time it compiled without errors.

I suspect the compiler does not adopt all 'ANSI' 'C'.

---MikeF
*** ***
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top