473,396 Members | 1,877 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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.
Aug 11 '09 #1
7 4700
donbock
2,426 Expert 2GB
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.)
Aug 11 '09 #2
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
Aug 12 '09 #3
donbock
2,426 Expert 2GB
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).
Aug 12 '09 #4
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
Aug 12 '09 #5
weaknessforcats
9,208 Expert Mod 8TB
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.
Aug 12 '09 #6
donbock
2,426 Expert 2GB
@puneetsardana88
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.)
Aug 13 '09 #7
Ok thank you sir for your reply...Let me go through the thread properly and wil let you if I have any confusion....
Aug 14 '09 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Ida | last post by:
Hi, I am trying to build an dll with Microsoft Visual C++ but during the linking phase I get linking errors. Script.obj : error LNK2019: unresolved external symbol __imp__PyString_AsString...
47
by: Richard Hayden | last post by:
Hi, I have the following code: /******************************** file1.c #include <iostream> extern void dummy(); inline int testfunc() {
4
by: Serge | last post by:
Hi, I have no problem creating a static member variable with integers, etc but when I try the same with a vector then I always get linker errors that the static member variable is unknown...
2
by: Lu | last post by:
Hello, I am wondering how to protect a global variable in a header file from external access. So I googled and found: "The keyword 'static' has two different uses, depending on whether it is...
4
by: Peter Ammon | last post by:
I would like to share a variable between two functions defined in two separate source files; no other functions will need the global variable so I'd prefer to not give it file scope. Thus, I want...
0
by: DDE | last post by:
Hi all, I have an application (number of web Services) in which I define different application variables, like application etc. Besides that, I have a couple of assemblies containing classes and...
3
by: al.cpwn | last post by:
do static and inline functions or members have internal linkage? I have been reading this newsgroup on google and found conflicting ideas. Can someone please help me understand why in some places...
3
by: Ed Dana | last post by:
Greetings, all. :) I'm familiar with other object oriented programming tools, such as Java & REBOL. In an effort to become more familiar with C++, I decided to write an object oriented BlackJack...
6
by: andrey.vul | last post by:
The error is LNK2001: unresolved external symbol "protected: static bool solver<typename, typename, int, int, int, int, int>::solution" (<vc++ mangled name>) Code (sudoku.cpp): // Sudoku.cpp :...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.