473,473 Members | 2,031 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

EXTERN Qualifier

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
7 3467

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
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

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
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
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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: A | last post by:
Hi, Consider this code: //Header File - Foo.h int i = 0; // non-static global variable class Foo{ ...
4
by: Mahesh Tomar | last post by:
Dear Readers, I am porting my existing C code to C++. In my existing code there are numerous functions that has been defined with CONST qualifier. For eg. foo(const DATA_TYPE *x); DATA_TYPE is...
18
by: tweak | last post by:
What's the best way to use extern when using multiplefiles that is easiest to maintain? Is it best to declare: extern int a; in a header file and include the header file in all files except...
6
by: shalu | last post by:
My question is about extern variables. I have a structure in a .h file and its made extern as follows, extern STATS_STRUCT Status; Actually in the .c file, the size of the array is defined....
7
by: Charles Sullivan | last post by:
On my system (using gcc) I found I need to use the "volatile" qualifier to limit compiler optimization. I notice that this qualifier is described in K&R 2nd edition (1988) but not in K&R 1st...
4
by: gaurav.dube | last post by:
I am facing a problem with warning removal I have got a header file it contains extern declarations of lot of variables (say 100) This header file is included in hundreds of .c files. Some of...
5
by: jchludzinski | last post by:
I have 3 files (see below: a.h, w.c, ww.c). I would like to use a single x (declared somewhere) which would global to both compilation units: w.c & ww.c. No matter where I place the "extern"...
4
by: Raman | last post by:
Hi All, The declaration of pthread_mutex_init in pthread.h is as in pthread.h Some one please explain. ( what is "__resrict", whay "__" before "mutex" and what is __THROW. extern int...
2
by: venkat | last post by:
Hi, i came across restrict qualifier while looking the code. I haven't able to understand what does this do?. Can some one help me how does this makes the things restrict to an specified...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.