1

I'm not really sure am I right, but i think that: in the line static int k; variable is declared and defined (k is initialized by default to 0); in the next line static int k = 31; I initialized k to 31 (why this is not redefinition?); in the next line nothing is actually changed because it's just another declaration of k - i've read somewhere that multiple declarations are allowed sometimes (if you can tell me when and where).


static int k;
static int k = 31;
static int k;
int main()
{
    static int k;
    static int k = 21;
    static int k;
    printf("%d", k);
    return 0;
}

Why I didn't get an error for the line static int k = 31; for redefinition? On the other hand, if it works properly for global variable k why I'm getting redeclaration error for the line static int k = 21; for local k. Can someone explain this so I can understand even if I'm just a beginner in C?

  • 1
    For reference to the _tentative definition_ concept there are other SO posts https://stackoverflow.com/questions/3095861/about-tentative-definition for example. – Iharob Al Asimi Jul 21 '19 at 13:53
  • 1
    _Why I didn't get an error for the line static int k = 31; for redefinition?_ Because this `static int k = 31;` is strong symbol(`static` with *initialized*) & compiler give preference to strong symbol over weak one(un-initialized). – Achal Jul 21 '19 at 13:56

0 Answers0