By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,988 Members | 1,341 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,988 IT Pros & Developers. It's quick & easy.

EXTERN Qualifier

P: n/a
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
*** Posted via a free Usenet account from http://www.teranews.com ***
Apr 14 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a

MikeF wrote:
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

Apr 14 '06 #2

P: n/a
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>

Apr 14 '06 #3

P: n/a

On 14-Apr-2006, "Vladimir S. Oka" <no****@btopenworld.com> wrote:
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
*** Posted via a free Usenet account from http://www.teranews.com ***
Apr 14 '06 #4

P: n/a
Mi***@invalid.addr.com wrote:
On 14-Apr-2006, "Vladimir S. Oka" <no****@btopenworld.com> wrote:
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.


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.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
Apr 14 '06 #5

P: n/a
On Fri, 14 Apr 2006 17:44:44 +0100, "Vladimir S. Oka"
<no****@btopenworld.com> wrote in comp.lang.c:
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`.


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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Apr 15 '06 #6

P: n/a
Jack Klein opined:
On Fri, 14 Apr 2006 17:44:44 +0100, "Vladimir S. Oka"
<no****@btopenworld.com> wrote in comp.lang.c:
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`.


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>

Apr 15 '06 #7

P: n/a

On 14-Apr-2006, Jack Klein <ja*******@spamcop.net> wrote:
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
*** Posted via a free Usenet account from http://www.teranews.com ***
Apr 15 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.