Connecting Tech Pros Worldwide Help | Site Map

External Static

Newbie
 
Join Date: Aug 2009
Posts: 15
#1: Aug 11 '09
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.
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 828
#2: Aug 11 '09

re: External Static


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.)
Newbie
 
Join Date: Aug 2009
Posts: 15
#3: Aug 12 '09

re: External Static


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
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 828
#4: Aug 12 '09

re: External Static


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).
Newbie
 
Join Date: Aug 2009
Posts: 15
#5: Aug 12 '09

re: External Static


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
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,363
#6: Aug 12 '09

re: External Static


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.
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 828
#7: Aug 13 '09

re: External Static


Quote:

Originally Posted by puneetsardana88 View Post

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.)
Newbie
 
Join Date: Aug 2009
Posts: 15
#8: Aug 14 '09

re: External Static


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