Wieso darf man eine Variable vor "int main()" deklarieren, aber nicht direkt danach?



  • #include <iostream>
    
        using std::cout;
        using std::cin;
        using std::endl;
        int carrots; /* declare an integer variable */
    
        int main(void)
    
    {
    
        carrots = 25; /* assign a value to the variable */
    
        cout << "I have " << carrots << " carrots." << endl << endl; /* more than one statement per line - this is not a clear code style */
    
        carrots = carrots - 1; /* modify the variable */
    
        cout << "Crunch, crunch. Now I have ";
        cout << carrots << " carrots." << endl;
        cin.get();
        cin.get();
        cin.get();
        cin.get(); /* press 5-times enter to close the window.*/
    
        return 0;
    }
    

    Das klappt zwar... aber ist es schlecht?

    #include <iostream>
    
        using std::cout;
        using std::cin;
        using std::endl;
    
        int main(void)
        int carrots; /* declare an integer variable */
    
    {
    
        carrots = 25; /* assign a value to the variable */
    
        cout << "I have " << carrots << " carrots." << endl << endl; /* more than one statement per line - this is not a clear code style */
    
        carrots = carrots - 1; /* modify the variable */
    
        cout << "Crunch, crunch. Now I have ";
        cout << carrots << " carrots." << endl;
        cin.get();
        cin.get();
        cin.get();
        cin.get(); /* press 5-times enter to close the window.*/
    
        return 0;
    }
    

    Jedoch funktioniert das nicht mehr, wenn "int carrots;" direkt nach dem "int main()" deklariert wird. Weswegen ist das so?

    (Das mag zwar nicht die wichtigste Frage sein, aber wäre doch schön zu wissen, weswegen das so ist.)



  • Bladestorm schrieb:

    Das klappt zwar... aber ist es schlecht?

    Ja, globale Variablen sind schlecht.

    Bladestorm schrieb:

    Jedoch funktioniert das nicht mehr, wenn "int carrots;" direkt nach dem "int main()" deklariert wird. Weswegen ist das so?

    (Das mag zwar nicht die wichtigste Frage sein, aber wäre doch schön zu wissen, weswegen das so ist.)

    Weil die Sprache so definiert wurde.

    Historischer Hinweis: in K&H C (uralt) wurden dort die Funktionsparameter definiert.


  • Mod

    Dir ist schon klar, dass das int main keine Variablendeklaration ist, sondern der Anfang einer Funktionsdefinition? Anfang deshalb, weil der Rest der Funktionsdefinition, also die { } und alles dazwischen, fest dazu gehört. Und nun hast du einfach etwas dazwischen geschrieben und wunderst dich, dass das nicht funktioniert? Würde es dich auch wundern, wenn

    cout << "Hello World!";
    ->
    cout << int carrots; "Hello World"
    

    nicht funktioniert?



  • ACH SO! Danke.



  • Die Variable wird da vor allem definiert. Die Deklaration gibt es gratis dazu.



  • @ Bladestorm
    Es hilft zum Verständnis wenn du die Leerzeilen zwischen "int main()" und dem darauffolgenden "{" weglässt:

    #include <Foo.h>
    #include <Bar.h>
    
    int eineGlobaleVariable = 123;
    
    int EineFunktion(int einParameter)
    {
        // Code von "EineFunktion"
        return 123;
    }
    
    int nochEineGlobaleVariable = 234;
    
    void NochEineFunktion()
    {
        // Code von "NochEineFunktion"
    }
    
    int dieDritteGlobaleVariable = 345;
    
    int main() // Die Main-Funktion
    {
        // Code der "main" Funktion
        return 0;
    }
    
    int dieVierteGlobaleVariable = 456;
    

Anmelden zum Antworten