External Static

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • puneetsardana88
    New Member
    • Aug 2009
    • 57

    #1

    External Static

    Hi Can someone tel me about external static?Do we use it if we want that data to be private to our file.Can't we use it in other file.I have created a file

    1.h

    static int k=9;

    and

    2.c

    #include "1.h"
    #include<stdio. h>
    int main()
    {
    printf("Hello %d ",k);
    getch();
    }


    Th variable is accesible and can be modified.

    Please tel me correct use of external static.
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    The phrase external static is not from the C Standard; and so is not precisely defined. What do you mean when you say this?

    Look at Static variables having Block scope and Global variable and static global variable for advice on the static keyword.

    By the way, the typical convention is to put declarations in header (*.h) files and definitions and code in source files (*.c). This is merely a convention -- the compiler doesn't care one way or the other -- but you ought to be sure you have a good reason to swim upstream against the tide of 30+ years of C programming. I mention this because "static int k=9;" in 1.h is a definition.

    (FYI: I can't think of any way to construct a declaration for a static variable.)

    Comment

    • puneetsardana88
      New Member
      • Aug 2009
      • 57

      #3
      This is the second reply from the thread....

      They are both in memory for the entire lifetime of the program. The variable that is declared static only has scope in the file in which it is declared where as the variable declared without static can be accessed from other files using an extern declaration.

      Can you explain this with an example

      The variable that is declared static(here he mean global) only has scope in the file in which it is declared


      Thank you for your reply

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Have you had a chance yet to work with a program that was constructed from more than one *.c file? If not, then there is no meaningful distinction between internal linkage (static) or external linkage (global).

        Comment

        • puneetsardana88
          New Member
          • Aug 2009
          • 57

          #5
          Thats what I am asking to you....Sorry i am bit beginner so kindly bear me for sometime :).....Please take some of your time n explain with an example.......I would be highly thankful to you

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            No one has mentioned linkage.

            Linkage is either external or internal. If the linkage is external then that variable (or function) is accessible from another implementation file. Otherwise it is not.

            You make the linkage internal by using the static keyword.

            You make the linkage external by using the extern keyword.

            Therefore:

            extern int var;

            means there is an int variable named var defined in some other file and I want to use it.

            extern int var= 0;

            means I want to create a variable named var with external linkage and an initial value of zero.

            static int var;

            means I want to create a variable named var with internal linkage. This variable is accessible only in this file between the point of definition and the end of the current scope. If this static int is defined between braces, its scope ends with th closing brace. If this static int is defined outside any braces, its scope ends at the end of the file.

            Comment

            • donbock
              Recognized Expert Top Contributor
              • Mar 2008
              • 2427

              #7
              Originally posted by puneetsardana88
              Thats what I am asking to you....Sorry i am bit beginner so kindly bear me for sometime :).....Please take some of your time n explain with an example.......I would be highly thankful to you
              What's wrong with the example in Message #3 of the Global-variable-and-static-global-variable thread I referred you to?
              (Don't forget to read the note in Message #5 of that same thread.)

              Comment

              • puneetsardana88
                New Member
                • Aug 2009
                • 57

                #8
                Ok thank you sir for your reply...Let me go through the thread properly and wil let you if I have any confusion....

                Comment

                Working...