Regarding extern

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • prashant.khade1623@gmail.com

    Regarding extern

    HI

    I wrote a simple program to expect some error, but it is printing the
    value.


    main()
    {
    extern int i;

    int i=10;
    printf("%d\n",i );
    }


    I am getting answer 10. But i think it should not be the case.

    Thanks

  • Richard Heathfield

    #2
    Re: Regarding extern

    prashant.khade1 623@gmail.com said:
    HI
    >
    I wrote a simple program to expect some error, but it is printing the
    value.
    >
    >
    main()
    {
    extern int i;
    >
    int i=10;
    printf("%d\n",i );
    }
    >
    >
    I am getting answer 10. But i think it should not be the case.
    Given that you've failed to provide a prototype for printf, no output that
    printf provides can be considered incorrect. C headers are not eye candy.
    Add #include <stdio.hto your code, above main.

    What do you think *should* be the case? What were you expecting to be
    printed, and why? And what diagnostic messages does your compiler give you
    when you invoke it in conforming mode?

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

    • Harald van =?UTF-8?b?RMSzaw==?=

      #3
      Re: Regarding extern

      On Fri, 11 Apr 2008 23:49:37 -0700, prashant.khade1 623@gmail.com wrote:
      HI
      >
      I wrote a simple program to expect some error, but it is printing the
      value.
      >
      >
      main()
      {
      extern int i;
      >
      int i=10;
      printf("%d\n",i );
      }
      >
      >
      I am getting answer 10. But i think it should not be the case.
      The program is indeed broken (also for other reasons than you mentioned).
      It is not allowed to declare two different objects with the same name in
      the same scope. I have tried various compilers, and almost all (not all)
      give a hard error for your code.

      That said, compilers _are_ allowed to compile any code, no matter how
      broken, so long as they complain that the code is broken. Which compiler
      are you using, and have you checked the documentation to see how to
      enable all standard errors or warnings?
      Thanks

      Comment

      • Pradeep

        #4
        Re: Regarding extern

        Hi,
        Here in the program
        main()
        {
        extern int i; -->Global 'i'
        int i=10; --->local 'i'
        printf("%d\n",i );-->complier always refers to the nearest block
        variable 'i'(i.e., local variable 'i' which is value 10
        }
        so the o/p is always '10'

        On Apr 12, 11:56 am, Harald van D©¦k <true...@gmail. comwrote:
        On Fri, 11 Apr 2008 23:49:37 -0700, prashant.khade1 ...@gmail.com wrote:
        HI
        >
        I wrote a simple program to expect some error, but it is printing the
        value.
        >
        main()
        {
        extern int i;
        >
        int i=10;
        printf("%d\n",i );
        }
        >
        I am getting answer 10. But i think it should not be the case.
        >
        The program is indeed broken (also for other reasons than you mentioned).
        It is not allowed to declare two different objects with the same name in
        the same scope. I have tried various compilers, and almost all (not all)
        give a hard error for your code.
        >
        That said, compilers _are_ allowed to compile any code, no matter how
        broken, so long as they complain that the code is broken. Which compiler
        are you using, and have you checked the documentation to see how to
        enable all standard errors or warnings?
        >
        Thanks

        Comment

        • Harald van =?UTF-8?b?RMSzaw==?=

          #5
          Re: Regarding extern

          On Sat, 12 Apr 2008 02:24:13 -0700, Pradeep wrote:
          Hi,
          Here in the program
          main()
          {
          extern int i; -->Global 'i'
          int i=10; --->local 'i'
          printf("%d\n",i );-->complier always refers to the nearest block variable
          'i'(i.e., local variable 'i' which is value 10 }
          so the o/p is always '10'
          No, the first declaration of i is not a "global" declaration. It is a
          block scope declaration of a variable. It will have to be defined
          somewhere else with file scope, but that's irrelevant.

          Consider this program:

          int main(void) {
          extern int i;
          extern int f(void);

          return f();
          }

          int f(void) {
          return i;
          }

          int i;

          This is invalid. Inside of f, the previous declaration of i is not
          visible. Even though it refers to an object with file scope, the
          declaration itself has a different scope, which ends at the closing brace
          of main.

          Here, as in the original code, there are compilers that accept it. That's
          fine, so long as they complain about it. The code is broken. The original
          code is also broken.

          Comment

          • Pradeep

            #6
            Re: Regarding extern

            On Apr 12, 4:27 pm, Harald van D©¦k <true...@gmail. comwrote:
            On Sat, 12 Apr 2008 02:24:13 -0700, Pradeep wrote:
            Hi,
            Here in the program
            main()
            {
            extern int i; -->Global 'i'
            int i=10; --->local 'i'
            printf("%d\n",i );-->complier always refers to the nearest block variable
            'i'(i.e., local variable 'i' which is value 10 }
            so the o/p is always '10'
            >
            No, the first declaration of i is not a "global" declaration. It is a
            block scope declaration of a variable. It will have to be defined
            somewhere else with file scope, but that's irrelevant.
            >
            Consider this program:
            >
            int main(void) {
            extern int i;
            extern int f(void);
            >
            return f();
            >
            }
            >
            int f(void) {
            return i;
            >
            }
            >
            int i;
            >
            This is invalid. Inside of f, the previous declaration of i is not
            visible. Even though it refers to an object with file scope, the
            declaration itself has a different scope, which ends at the closing brace
            of main.
            >
            Here, as in the original code, there are compilers that accept it. That's
            fine, so long as they complain about it. The code is broken. The original
            code is also broken.
            Yes, you are correct.
            my assemption was wrong. i think if we check the assembly code of the
            above program we can conclude some extent.

            Comment

            Working...