extern declaration

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rahul

    #1

    extern declaration

    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...
  • Richard Heathfield

    #2
    Re: extern declaration

    Rahul said:
    Hi Everyone,
    >
    I wanted to know if the following declaration is valid?
    >
    extern int i = 10;
    Nothing wrong with that. Note that it is not only a declaration but also
    (because you have initialised it explicitly) a definition.
    #include <cstdio>
    No such header.
    int main()
    That's fine, but int main(void) is finer.
    {
    printf("%d\n",i );
    Having (correctly) promised to return an int from main, why not return an
    int from main?

    return 0;
    }
    >
    I get the following warning,
    >
    warning: `i' initialized and declared `extern'
    That looks correct to me.
    I expected an error,
    C doesn't distinguish between "errors" and "warnings". They are all
    diagnostic messages.
    as extern by itself indicates that the variable
    is defined in some other compilation unit...
    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

    Comment

    • Magic.Yang

      #3
      Re: extern declaration

      On 4ÔÂ2ÈÕ, ÏÂÎç9ʱ53·Ö, Richard Heathfield <r...@see..sig. invalidwrote:
      Rahul said:
      >
      Hi Everyone,
      >
      I wanted to know if the following declaration is valid?
      >
      extern int i = 10;
      >
      Nothing wrong with that. Note that it is not only a declaration but also
      (because you have initialised it explicitly) a definition.
      >
      #include <cstdio>
      >
      No such header.
      >
      int main()
      >
      That's fine, but int main(void) is finer.
      >
      {
      printf("%d\n",i );
      >
      Having (correctly) promised to return an int from main, why not return an
      int from main?
      >
      return 0;
      >
      }
      >
      I get the following warning,
      >
      warning: `i' initialized and declared `extern'
      >
      That looks correct to me.
      >
      I expected an error,
      >
      C doesn't distinguish between "errors" and "warnings". They are all
      diagnostic messages.
      >
      as extern by itself indicates that the variable
      is defined in some other compilation unit...
      >
      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 );



      }

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


      Comment

      • Richard Heathfield

        #4
        Re: extern declaration

        Magic.Yang said:

        <snip>
        >
        You can use extern like this
        -----------------------------------------------
        int i = 10;
        >
        >
        #include <cstdio>
        C has no such header.
        int main()
        {
        extern int i;
        printf("%d\n",i );
        Yes, but you don't have to.
        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.

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

        Comment

        • Kenneth Brody

          #5
          Re: extern declaration

          Richard Heathfield wrote:
          >
          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:ThisIsA SpamTrap@gmail. com>

          Comment

          • Andrey Tarasevich

            #6
            Re: extern declaration

            Magic.Yang wrote:
            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.

            --
            Best regards,
            Andrey Tarasevich

            Comment

            Working...