scoping in c



  • Hallo,
    warum compiliert folgendes program in c? was ist hier blockscope and function scope etc?
    warum gibt folgender ausdruck keine redeclaration? char x = 'b';

    #include <stdio.h>
    int main(void)
    { 
        char x = 'm';
        printf("%c\n", x);
        {
            printf("%c\n", x);
            char x = 'b';
            printf("%c\n", x);
        }
        printf("%c\n", x);
    }
    


  • scoper schrieb:

    warum gibt folgender ausdruck keine redeclaration? char x = 'b';

    Weil das eine x im nested scope das andere schlichtweg verdeckt.



  • scoper schrieb:

    warum compiliert folgendes program in c?

    Warum sollte es nicht?

    scoper schrieb:

    was ist hier blockscope

    alles innerhalb eines Blockes

    scoper schrieb:

    and function scope etc?

    alles innerhalb des Haupblocks der Funktion

    scoper schrieb:

    warum gibt folgender ausdruck keine redeclaration? char x = 'b';

    Warum sollte es.

    C ist genormt. Und zwar mehrmals: http://de.wikipedia.org/wiki/Varianten_der_Programmiersprache_C



  • ok, was ist ein nested scope vs. block scope im code?


  • Mod

    scoper schrieb:

    ok, was ist ein nested scope vs. block scope im code?

    Ein nested scope ist ein Block in einem Block. Block scope ist das, was in einem Block ist, beziehungsweise die Eigenschaft von Bezeichnern, die in einem Block sind.

    int global;   // Globaler Scope
    
    void foo()
    {          // Ein neuer Block/Scope beginnt
      int i;   // i hat block scope
        {      // Dies ist ein nested scope
           int j;   // j hat auch block scope, aber eben ein anderer Block als i
        }
    }
    

Anmelden zum Antworten