Beiträge von strik im Thema „C: Emulation von Klassen“

    Der Witz ist nun, daß man das relativ ähnlich auch in C schreiben kann. Hab' hier ein Beispiel gepostet.

    Hm.... Ich habe mir das Dokument mal angeschaut:

    The datatypes and names of the arguments that are passed to a function are written in round brackets after the name of the function, here "main()".

    As in this case no arguments are passed to "main", the brackets are empty.

    Das stimmt für C++, aber nicht für C. Um zu sagen, dass main() keine Argumente bekommt, muss man in C immer noch das (void) schreiben, weil es sonst nur "unspezifizierte" Argumente sind.

    Bitte melde dich an, um diesen Link zu sehen. are always ignored by the C-compiler.

    Ähm... Nö:

    Code
    while (x++ < 100)  {} // compiliert
    whi le (x++ < 100)  {} // compiliert nicht

    Also nicht "always".

    Anderes, wohl relevanteres Beispiel:

    Code
    int b = 123;
    int * p = &b;
    int a = 456;
    
    a = a / *p; /* a wird durch b geteilt */
    a = a/*p; /* hier wird das "p;" auskommentiert; das ist wohl nicht ganz das gleiche */

    "int" usually means "signed long int".

    Nein! "int" ist "signed int". Das kann - je nach Compiler - identisch zu "signed short int" oder "signed long int" sein, muss aber nicht!

    Entsprechend muss bei "int" als printf()-Spezifier "%d" stehen; bei "long int" muss es "%ld" sein. Wenn der Compiler nun so ist, dann long int == int ist, dann mag das funktionieren, du begibst dich aber auf Glatteis.

    Bei "char" fehlt darüber hinaus, dass "char", "signed char" und "unsigned char" in C drei verschiedene Datentype sind, weil "char" ohne Signedness-Specifier anders gehandhabt wird als die anderen int-Typen, die ohne Specifier immer "signed" sind.

    Zitat von https://hlubenow.lima-city.de/c.html#13

    Conditions for the preprocessor can be programmed with:

    Code
    #if
    #if defined
    #ifdef
    ...

    Halte ich so ungünstig. Bitte melde dich an, um diesen Link zu sehen. und Bitte melde dich an, um diesen Link zu sehen. sind Präprozessor-Anweisungen. Das "defined" hinter "Bitte melde dich an, um diesen Link zu sehen." ist aber Teil eines Ausdrucks, der komplizierter sein kann. Du schmeisst hier Birnen und Äpfel in einen Topf (zumal nach dem defined noch Klammern kommen müssen, die du noch wegläßt).

    Zitat von https://hlubenow.lima-city.de/c.html#13

    #include-Statements

    There is another kind of preprocessor directive, that uses the keyword "Bitte melde dich an, um diesen Link zu sehen.". The "Bitte melde dich an, um diesen Link zu sehen." directive is followed by the name of a text-file, either in "< ... >" or in quotation marks (" ... "). When the preprocessor reaches that line, it searches for the file of the given filename. When the file is found, the preprocessor replaces the line of the directive with the whole content of that file. That way, (the header files of) libraries are imported into the source code.

    • If the filename is passed within quotation marks, the same directory as the source code is searched for the file.
    • If the filename is passed within "< ... >", several directories are searched for the file. The default directories to be searched by gcc are compiled into the program "cpp" and can be listed with the command "cpp -v". Usually, "/usr/include" and "/usr/local/include" are searched. Other directories can be passed to gcc using the "-I"-command.

    Der Standard ist da etwas generischer als es hier dargestellt ist. Im Übrigen sucht der C-Compiler auch bei Anführungszeichen in den Verzeichnissen, die er bei "<...>" durchsucht, falls die Datei lokal nicht existiert.

    Zitat von https://hlubenow.lima-city.de/c.html#14

    Variables can be declared as "const". That means, they can't be altered afterwards. Therefore, the initialization has to be in the same line as the definition:

    Code
    #include <stdio.h>
    
    int main() {
        const int a = 10;
        /* a = 15; Wouldn't work */
        printf("%d\n", a);
        return 0;
    }

    Variables declared that way are stored in a read-only-area of the program's memory.

    Nein, das kann const nicht leisten. Siehe zum Beispiel hier Bitte melde dich an, um diesen Link zu sehen. und viele andere Stellen.

    Zitat von https://hlubenow.lima-city.de/c.html#17

    gcc allows to combine all three: "int a = 10;". But other compilers (CC65. Maybe Borland, if I remember correctly) demand, that all declarations have to be written at the beginning of a function, and the initializations have to follow separately after all declarations:

    Code
    void main() {
        int a;
        int b;
        float c;
        char d[50];
        a = 5;
        b = 10;
    }

    Das ist so nicht richtig. Auch cc65 und BC erlauben es, Variablen bei der Definition direkt zu initialisieren.

    Was sie nicht erlauben, zumindest nich per Default, dass man zuerst Anweisungen schreibt und dann Variablen definiert. Das liegt daran, dass das erst mit C99 (also in der Sprachverson von 1999) eingeführt wurde, um mit C++ gleichzuziehen, und die Compiler zumindest in großen Teilen diesen Sprachstandard noch nicht können. Wenn du den gcc in den c89 oder c90 - Modus zurückschaltest, dann kann er das auch nicht.


    Darüber hinaus finde ich deine Beispiele ungünstig für C:

    • for-Loop von 1 bis 10 (statt von 0 bis 9)
    • break and continue: In einer Schleife von 1 bis 1000, wo bei 3 eine Sonderbehandlung passiert und bei 10 über "break" abgebrochen wird.

    Ich habe mir den Text nur auszugsweise angeschaut, da schlummern bestimmt noch ein Haufen weiterer Probleme.