4

In this example, what will be difference if variable string_a is declared as static variable ?

const char *pString;
void first(void)
{
    const char string_a[] = " First string ";
    pString =(char *)string_a;
}
void second(void)
{
    const char string_b[] = " Second string ";
    pString =(char *)string_b;
}
int main()
{   
    first();
    second();
    printf("%s\n", pString);
}
  • What determined a lifetime of object in C ?

  • What is difference between global and file scope of variables ?

boleto
  • 1,149
  • 1
  • 22
  • 32
  • 1
    See also http://stackoverflow.com/questions/7632120/scope-vs-life-of-variable-in-c – torek Jul 17 '13 at 11:05
  • 6
    *"Lifetime of object"*? What do you mean? C has no objects. – m0skit0 Jul 17 '13 at 11:07
  • To elaborate on m0skit0, this question is ambiguous because C has no objects. For example, when you talk about the lifetime of `string_a`, it's not clear whether you mean the lifetime of the pointer itself or the lifetime of the thing it points to. Asking vague questions tends to get you confusing answers. – David Schwartz Jul 17 '13 at 11:08
  • 3
    @m0skit0 Ever had a look at the C standard and its rationale? Look [here](http://www.open-std.org/jtc1/sc22/wg14/www/C99RationaleV5.10.pdf). – glglgl Jul 17 '13 at 11:09
  • 7
    @m0skit0: Actually, the spec (C99 at least) does deal with objects, these being defined as "region of data storage in the execution environment, the contents of which can represent values" – Hasturkun Jul 17 '13 at 11:10
  • @glglgl Oh thanks, didn't know about this definition of *object*. Pretty confusing to use the same terminology for totally different things... – m0skit0 Jul 17 '13 at 11:13
  • @m0skit0 C++ is a different language so it is free to use any terminology differently from C. However, in this case, it mostly doesn't. The definition of an object in C++ is "a region of storage", very similar to the C definition. An object in C++ can have a user-defined type (e.g. a `class`) or it can have primitive type (e.g. `float` or `int`) or it can have an array type. In the C++ world the word "object" is often used as a shorthand for "`class` object", but that is misleading. – Oktalist Jul 17 '13 at 12:53

4 Answers4

4

There are two kinds of static in play here: static in the global scope in a file, and static inside a function.

The former declares internal linkage for the object, which means that it is only accessible inside the file. These objects are created on bss before main() is entered. That memory area is always memset to all zeros before main() runs.

The default for objects created outside function scope is being global (external linkage), meaning they can be accessed from other compilation units using the extern keyword.

static inside a function means that the object exists from the first time the function is called until the program ends.

Illustration:

int external_linkage;
static int internal_linkage;

void foo()
{
    static int static_in_function;
}

All three variables are guaranteed to have a value of 0 when the program runs, unlike stack and heap variables.

Lstor
  • 2,265
  • 17
  • 25
2

Static variables have file scope with internal linkage. This means that these variables cannot be accessed from other translation units.

Global variables also have file scope but with external linkage. This means that these variables can be accessed from other translation units as well.

string_a is a local variable defined inside a function. If it is made as static, it will come into existence once the function is called, and will exist until the program ends (whereas non-static local variables stop existing as soon as the function ends).

Nithin Bhaskar
  • 686
  • 1
  • 5
  • 9
0

Global means you can access that variable in another file by using extern keyword. File scope means, that variable is not visible to other files. In c, By default every global variable have global scope. If anyone wants to make global variable invisible to other files they define global variable with static keyword. static keyword converts global scope to file scope.

Chinna
  • 3,930
  • 4
  • 25
  • 55
-1

Static variables in functions are confined to scope. This means that these variables cannot be accessed from other functions but unlike local variable it's not destroyed after scope and will remain till end of program

Global variables have file scope but those are accessible from other files as well provided
extern keyword is used. static in global scope in File confines variable to file only.

Shumail
  • 3,103
  • 4
  • 28
  • 35
  • 2
    C does indeed have something called objects, only it's something different from the *object* in OOP. An *object* in C is defined as **region of data storage in the execution environment, the contents of which can represent values**. – Lstor Jul 17 '13 at 11:15
  • Edited my answer. I didn't know. – Shumail Jul 17 '13 at 11:16