1,927 views

2 Answers

Best answer
4 votes
4 votes

    Property                                Static                                      Global

Memory Location               Data Segment                              Data Segment 

 Default Value                             0                                              0

    Scope                           Local to function                   Accessible to every function

   Life-Time                        Till Program run                           Till Program run

Some More:

1) Every global variable can be accessed in the other C program file by using extern storage class. If you want that a particular global variable should not be accessed by any other file then you can declare that global variable as static global variable. 

2) See this one more similarity

#include<stdio.h>

int x = 10;

int y = x; //Not allowed you can not assign a variable to gloabal variable

int main(){
    ...
    static int p = x; // Its also Not allowed
    ...
    ...
}

P.S. There are more similarity than differences. The only difference is that scope thing

selected by
4 votes
4 votes

Global variables are defined outside a function, usually on top of the program. Global variables hold their values throughout the lifetime of your program and they can be accessed inside any of the functions defined for the program.

A global variable can be accessed by any function. That is, a global variable is available for use throughout your entire program after its declaration.

In statically scoped language if any variable is free it always refers to global variable.

Static automatic variables continue to exist even after the block in which they are defined terminates. Thus, the value of a static variable in a function is retained between repeated function calls to the same function. The scope of static automatic variables is identical to that of automatic variables, i.e. it is local to the block in which it is defined; however, the storage allocated becomes permanent for the duration of the program. Static variables may be initialized in their declarations; however, the initializers must be constant expressions, and initialization is done only once at compile time when memory is allocated for the static variable.

Related questions

7 votes
7 votes
3 answers
1
7 votes
7 votes
1 answer
2
vaishali jhalani asked Nov 21, 2016
3,599 views
What is the dtfference between static single assignment and 3 address code?
1 votes
1 votes
2 answers
3
tarunmaganti asked Apr 15, 2018
776 views
If there are three tables to choose from -Sailors(sid,sname); Reserves(sid,bid); Boats(bid,color)Question is to choose a sailor who reserved a red boat.My question is wha...