how can we use static and extern?and where can we use this?
Answers were Sorted based on User's Feedback
Answer / pramod
static storage class is used for the following situations
1) the variable is defined as static within a function and
we can call the function several times, but we want that the
function should initialize the variable only once that is
during the first call to that function.So the variable will
stay alive in different calls to this function
2)In C++ , if we want a common variable between all the
objects( like a counter for how many objects have been
created) then static is allocated as it is not attached to
any object but class
extern storage is for global variables
1)if we want one varibale to be available to all the
functions in our program, make the varibale as global
variable( extern storage), it will be accessed by all the
functions in that file.But avoid declaring variable as
global for security reasons( can be accessed from any other
file in the PATH) and memory reasons (global variables are
deallocated only when the entire program terminates).
2)if we declare a variable in a file as extern , then
compiler will assume that this variable is not defined in
the present file but in some other file.So while compiling
there won't bw any error but error will come at the linking
if the extern variable is not present in the refernced files.
There is one special case
static global variable
we want a variable to be global , means accessible to all
the functions in that file and want that the variable should
not be accessible to any other file, just to make it static
global variable.So the visibility of that variable becomes
file specific only.
Is This Answer Correct ? | 3 Yes | 1 No |
the above answer is write.thanks to vignesh.
Is This Answer Correct ? | 1 Yes | 0 No |
static and extern are some types of storage classes in C...
there are four types of storage classes in C:
1) automatic (auto) storage class
2) static storage class
3) register storage class
4) extern storage class
static is a one which will be read only once by the compiler
(ie) it will ignore an another pass.... or it will be
initialized only once....... the scope of this class is
within the block only....
extern is a global declaration , but most of the compilers
dont prefer it to be used within a program.......
thank u
Is This Answer Correct ? | 3 Yes | 3 No |
what is ans for this scanf(%%d",c);
What is nested structure in c?
How can you restore a redirected standard stream?
Write a program in C to convert date displayed in gregorian to julian date
Average of a couple 10 years ago was 25. The average remains same after having a child and twins after 3 years. What is the present age of the first child
An entire structure variable can be assigned to another structure variable if __________
3 Answers Sasken, TCS, Tech Mahindra, Wipro,
A global variable when referred to in another file is declared as this a) local variable b) external variable c) constant d) pointers
How can I ensure that integer arithmetic doesnt overflow?
What is the difference b/w Structure & Union?
character array A[12] can hold
what type of errors are checked during compilation
Tell me what is null pointer in c?