Re: extern variable assignment



Thanks Igor,


extern int i = 3;

1. Is it correct?

Yes, though keyword 'extern' serves no useful purpose here.

If it is both a declaration and a definition, does it violates the MSDN
extern documents -- which extern is used for declaration?

http://msdn2.microsoft.com/en-us/library/0603949d.aspx

--------------------
The extern keyword declares a variable or function and specifies that it has
external linkage (its name is visible from files other than the one in which
it's defined).
--------------------


regards,
George

"Igor Tandetnik" wrote:

"George" <George@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:414B7395-BA16-4826-86AC-109D4FA40586@xxxxxxxxxxxxx
Statement like this,

extern int i = 3;

1. Is it correct?

Yes, though keyword 'extern' serves no useful purpose here.

2. Is it definition or declaration?

Both.

Any special function or benefits
we could get compared with

extern int i;

This is a declaration but not a definition.

int i = 3;

This is the same as "extern int i = 3;" if the statement appears at
namespace scope. When in block scope, this statement defines a local
variable while the original statement is illegal.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925



.



Relevant Pages

  • Re: Use of extern keyword
    ... >> i.e consider a following piece of code from MSDN explaining extern ... >> Here,if I drop the extern keyword from source file 2 and compile and ... > considered to be a tentative declaration. ... > in using global variables, it's much better practice to define the ...
    (comp.lang.c)
  • Re: keyword extern
    ... >> definition or declaration until the end of the translation unit being ... >> One never needs to use the extern keyword with a function definition, ... >> ...then here is how a C compiler understands them. ... If any code in the source file access 'x', ...
    (comp.lang.c)
  • Re: "extern" inside a block
    ... In multi source file programs, ... > I know the behaviour when extern declaration follows a non-extern ... Linkage of Identifiers ... scope in which a prior declaration of that identifier is visible [and ...
    (comp.lang.c)
  • Re: extern
    ... > (of two files with int a; ... behaves as if it had been declared extern int a = 0; ... If the declaration of an identifier for ... function named by the identifier is a definition. ...
    (comp.lang.c)
  • Re: "extern" question?
    ... don't use "extern", it works. ... object (a declaration without the extern keyword, ... extern int x; ... must be exactly one and only one external definition. ...
    (comp.lang.c)