3

Given the following c code

static int x = 0;

int what_is_this(void) {
    static int y = 5;
    x = x + y;
    y = y + 1;
    return y;
}

int main(void) {
    int v = what_is_this();
    printf("%d\n", v);
    return v;
}

With respect to the linker, is what_is_this a global symbol?

Is x a local symbol?

Is v not registered as a symbol?

WJL
  • 91
  • 4

1 Answers1

2

As per here: https://people.cs.pitt.edu/~xianeizhang/notes/Linking.html#symbol

  • global: global symbols that are defined by module m and that can be referenced by other modules. Global linker symbols correspond to nonstatic funcs and global vars that are defined without the static attribute.
  • external: global symbols that are referenced by module m but defined by some other module. Such symbols are called externals and correspond to funcs and vars that are defined in other modules.
  • local (static): local symbols that are defined and referenced exclusively by module m. Some local linker symbols correspond funcs and global vars that are defined with the static attribute. These symbols are visible anywhere within module m, but cannot be referenced by other modules.

So in simple terms global symbols are non-static,non-extern functions, and non-static, non-extern vars, external symbol are declared with extern, and local symbols are static.

With respect to the linker, is what_is_this a global symbol?

Not extern, not static, so yes

Is x a local symbol?

Declared with a static, so yes

Is v not registered as a symbol?

The linker never sees local variable declarations.(outside of maybe static variables declared in a function.)

PiRocks
  • 1,708
  • 2
  • 18
  • 29
  • Regarding the last question, so `v` is not registered as a symbol, since the linker never sees local variable declarations. Is this correct? – WJL Apr 24 '20 at 20:19