extern declaration

R

Rahul

Hi Everyone,

I wanted to know if the following declaration is valid?

extern int i = 10;

#include <cstdio>

int main()
{
printf("%d\n",i);
}

I get the following warning,

warning: `i' initialized and declared `extern'

I expected an error, as extern by itself indicates that the variable
is defined in some other compilation unit...
 
M

Magic.Yang

Rahul said:




Nothing wrong with that. Note that it is not only a declaration but also
(because you have initialised it explicitly) a definition.


No such header.


That's fine, but int main(void) is finer.


Having (correctly) promised to return an int from main, why not return an
int from main?

return 0;




That looks correct to me.


C doesn't distinguish between "errors" and "warnings". They are all
diagnostic messages.


Can you back up this claim with reference to the Standard?

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

You can use extern like this
-----------------------------------------------
int i = 10;


#include <cstdio>


int main()
{
extern int i;
printf("%d\n",i);



}
 
K

Kenneth Brody

Richard said:
Magic.Yang said: [...]
An external variables can only be carried out once initialized, and
must be carried out in the definition of variables
Extern char permis = 'Y'; / * wrong * /

Please *demonstrate* that this is wrong, with reference to the C Standard.
Simply claiming that it's wrong isn't good enough.

Since he won't be able to do so, let me point to 6.9.2p4, which
gives as examples of "extern object definitions":

int i1 = 1; // definition, external linkage
static int i2 = 2; // definition, internal linkage
extern int i3 = 3; // definition, external linkage
int i4; // tentative definition, external linkage
static int i5; // tentative definition, internal linkage

Note the definition of "i3".

<mode pedant="on">
Well, his statement is correct in that his example is "wrong", but
this is due to the capital "E" in "Extern".
</mode>

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
A

Andrey Tarasevich

Rahul said:
I wanted to know if the following declaration is valid?

extern int i = 10;

Yes, it is valid.
I expected an error, as extern by itself indicates that the variable
is defined in some other compilation unit...

'extern' does not indicate anything like that. 'extern' explicitly
specifies that the entity being declared has external linkage. That's
all it does.
 
A

Andrey Tarasevich

Magic.Yang said:
An external variables can only be carried out once initialized, and
must be carried out in the definition of variables

I don't exactly understand what you mean by "carrying out" a variable.
Extern char permis = 'Y'; / * wrong * /

There's nothing wrong with the above declaration.
 

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

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top