Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static vs global

Tags:

c

static

global

If I have a C file like below, what is the difference between i and j?

#include <stdio.h> #include <stdlib.h>  static int i; int j;  int main () {     //Some implementation } 
like image 586
Vijay Avatar asked Feb 16 '10 09:02

Vijay


People also ask

What is static and global function?

Terminology. class (keyword): static functions that are dynamically dispatched and can be overridden by a subclasses. global functions: functions defined at global scope, that is they can be called from anywhere.

Is static the same as global Java?

A global variable is one declared at the start of the code and is accessible to all parts of the program. Since Java is object-oriented, everything is part of a class. ... A static variable can be declared, which can be available to all instances of a class.

What are the differences between global auto and static variables?

Automatic variables create a new each time when program's execution enters in the function and destroys when leaves. Static variable create once, when program's execution enters in the function first time, destroys when program's execution finishes, they do not again.

Does static mean global in Java?

Static Variables: When a variable is declared as static, then a single copy of the variable is created and shared among all objects at a class level. Static variables are, essentially, global variables. All instances of the class share the same static variable.


1 Answers

i has internal linkage so you can't use the name i in other source files (strictly translation units) to refer to the same object.

j has external linkage so you can use j to refer to this object if you declare it extern in another translation unit.

like image 55
CB Bailey Avatar answered Oct 03 '22 10:10

CB Bailey