declarations and definitions

Discussion in 'C Programming' started by panku, Feb 1, 2011.

  1. panku

    panku Guest

    what is diff the declaration and definition?
    void main()
    {
    extern int i;
    i=90; //Error: i is undefined
    }

    as extern int i is there it is giving error and also when we write
    extern int i=90 then also it gives error ? why???????????????????????
    panku, Feb 1, 2011
    #1
    1. Advertising

  2. In article <>,
    panku <> wrote:
    >what is diff the declaration and definition?
    >void main()
    >{
    > extern int i;
    > i=90; //Error: i is undefined
    >}
    >
    >as extern int i is there it is giving error and also when we write
    >extern int i=90 then also it gives error ? why???????????????????????


    Put this line before the "void main()" line:

    #define extern

    That should fix it.

    --
    One of the best lines I've heard lately:

    Obama could cure cancer tomorrow, and the Republicans would be
    complaining that he had ruined the pharmaceutical business.

    (Heard on Stephanie Miller = but the sad thing is that there is an awful lot
    of direct truth in it. We've constructed an economy in which eliminating
    cancer would be a horrible disaster. There are many other such examples.)
    Kenny McCormack, Feb 1, 2011
    #2
    1. Advertising

  3. panku <> wrote:
    > what is diff the declaration and definition?
    > void main()


    Make that

    int main( void )

    since main() is supposed to return an int.

    > {
    > extern int i;
    > i=90; //Error: i is undefined
    > }


    > as extern int i is there it is giving error and also when we write
    > extern int i=90 then also it gives error ? why???????????????????????


    What you have with

    extern int i;

    isn't a defininition but just a declaration. With a decla-
    ration you merely tell the compiler that something (an ob-
    ject or a function) exists somewhere and that it shouldn't
    worry about that it doesn't know where that thing actually
    is. So, in plain words, it says: "Somewhere but not here
    an int variable named 'i' is defined and I would like you
    to use that when I write 'i' in the current context."

    But to get that to work you must also supply a definition of
    what you promised the compiler to exist. Otherwise the linker
    will catch up with you and tell you that it's not defined (via
    the dreaded "undefined reference" linker error message). One
    way to do so would be to change your program to

    int main( void ) {
    extern int i;
    i = 90;
    return 0;
    }
    int i;

    Now you have the required definition of 'i' in the last
    line - it makes the compiler create an object of type int
    and name 'i' that the merely declared 'i' in main() can
    be associated with. Another way would be to have 'i' de-
    fined in another source file and then link them together.

    It's rather similar to function declarations: you proba-
    bly wouldn't be too surprised if the linker complained
    about

    extern int foo( double );

    int main( void ) {
    foo( 3.14 );
    return 0;
    }

    Here you also just tell the compiler that some function
    foo() exists somewhere else but there's no definition that
    would tell what foo() is actually doing. Obviously, without
    that the program can't be created and you get a linker error
    about an undefined reference to 'foo'. (Note that the 'extern'
    qualifier is redundant for function declarations, so you will
    not see it used that often with function declarations.)

    Another point: since

    external int i;

    does not define a variable but merely declares it you also
    can't initialize it. Initialization can only happen at the
    place where something is defined. Consider what should hap-
    pen if it would be possible and you would do

    int main( void ) {
    extern int i = 90;
    return 0;
    }
    int i = -42;

    You'd have two contradictory initializations for the same
    variable! What should the poor compiler do with that?

    But when you instead have

    int main( void ) {
    extern int i;
    i = 90;
    return 0;
    }
    int i = -42;

    things are fine again - 'i' will be initialized to -42
    while it's created and then later in main() its value
    gets changed to 90.
    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
    Jens Thoms Toerring, Feb 1, 2011
    #3
  4. panku <> writes:
    > what is diff the declaration and definition?
    > void main()


    This should be "int main(void)". If your C book tells you that
    "void main()" is correct, get a better book.

    > {
    > extern int i;


    This declares that there's an object of type int named "i".
    It doesn't create such an object.

    > i=90; //Error: i is undefined


    The above statement is legal *if* you've defined i somewhere (and if
    it's defined in a translation unit that's linked into your program).
    You should have gotten an error message from the linker, not from
    the compiler.

    > }
    >
    > as extern int i is there it is giving error and also when we write
    > extern int i=90 then also it gives error ? why???????????????????????


    That's a different error, one that should be reported by the compiler.
    You can't initialize an object unless you're creating it. The "extern"
    says you're *not* creating it.

    BTW, it would be helpful if you'd post the actual error messages. The
    problem was obvious enough in this case, but in general more information
    is better.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Feb 1, 2011
    #4
  5. panku

    osmium Guest

    "Kenneth Brody" wrote:

    > On 2/1/2011 4:33 AM, Kenny McCormack wrote:
    >> In
    >> article<>,
    >> panku<> wrote:
    >>> what is diff the declaration and definition?
    >>> void main()
    >>> {
    >>> extern int i;
    >>> i=90; //Error: i is undefined
    >>> }
    >>>
    >>> as extern int i is there it is giving error and also when we write
    >>> extern int i=90 then also it gives error ? why???????????????????????

    >>
    >> Put this line before the "void main()" line:
    >>
    >> #define extern
    >>
    >> That should fix it.

    >
    > If you use the word "fix" in the same sense as a veterinarian uses it.


    LOL!
    osmium, Feb 1, 2011
    #5
  6. panku

    Hans Vlems Guest

    On 1 feb, 20:40, Kenneth Brody <> wrote:
    > On 2/1/2011 4:33 AM, Kenny McCormack wrote:
    >
    >
    >
    >
    >
    > > In article<>,
    > > panku<>  wrote:
    > >> what is diff the declaration and definition?
    > >> void main()
    > >> {
    > >>   extern int i;
    > >>   i=90;                  //Error: i is undefined
    > >> }

    >
    > >> as extern int i is there it is giving error and also when we write
    > >> extern int i=90 then also it gives error ? why???????????????????????

    >
    > > Put this line before the "void main()" line:

    >
    > > #define extern

    >
    > > That should fix it.

    >
    > If you use the word "fix" in the same sense as a veterinarian uses it.
    >
    > --
    > Kenneth Brody- Tekst uit oorspronkelijk bericht niet weergeven -
    >
    > - Tekst uit oorspronkelijk bericht weergeven -


    More like the way contract killers use it
    Hans Vlems, Feb 3, 2011
    #6
  7. On Tue, 01 Feb 2011 08:19:25 -0800, Keith Thompson <>
    wrote:

    > panku <> writes:


    > > {
    > > extern int i;

    >
    > This declares that there's an object of type int named "i".
    > It doesn't create such an object.
    >
    > > i=90; //Error: i is undefined

    >
    > The above statement is legal *if* you've defined i somewhere (and if
    > it's defined in a translation unit that's linked into your program).
    > You should have gotten an error message from the linker, not from
    > the compiler.
    >
    > > }
    > >
    > > as extern int i is there it is giving error and also when we write
    > > extern int i=90 then also it gives error ? why???????????????????????

    >
    > That's a different error, one that should be reported by the compiler.
    > You can't initialize an object unless you're creating it. The "extern"
    > says you're *not* creating it.
    >

    Nit: Only an error for declaration at block scope, as it was here.
    (Or precisely a constraint violation and required diagnostic.)

    At file scope the initializer effectively overrides 'extern' and the
    declaration is a definition. This inconsistency can be confusing and
    arguably better style is to avoid it, but it is allowed.
    David Thompson, Feb 11, 2011
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rikard Land
    Replies:
    0
    Views:
    336
    Rikard Land
    Oct 19, 2004
  2. Steven T. Hatton

    Declarations and Definitions in One Header

    Steven T. Hatton, Apr 25, 2004, in forum: C++
    Replies:
    4
    Views:
    319
    Steven T. Hatton
    Apr 25, 2004
  3. Chris Gordon-Smith

    Declarations and Definitions: Grammar

    Chris Gordon-Smith, Oct 3, 2004, in forum: C++
    Replies:
    2
    Views:
    360
    Gary Labowitz
    Oct 3, 2004
  4. aaragon
    Replies:
    14
    Views:
    399
  5. Jess
    Replies:
    15
    Views:
    487
Loading...

Share This Page