Kurze Frage zur main Funktion



  • Ai gude,
    ich habe mal eine kurze Frage bezüglich der main-Funktion. Warum sollte ich diese eigentlich als int deklarieren und z.B. nicht als short oder gar als float?

    Das ist mir gerade durch den Kopf gegangen und ich habe keine richtige Antwort gefunden.

    Vielen Dank für eure Hilfe
    lg, freakC++


  • Mod

    Das kannst du gerne tun, aber du dann muss ein standardkonformer Compiler deinen Code nicht mehr annehmen.



  • Hallo,
    ja, mir ist schon klar, dass das eigentlich kein Compiler gerne sieht. Aber ich frage mich warum. Das das mal jemand gesagt oder gibt es wirklich einen schlagkräftigen Grund?

    Vielen Dank
    lg, freakC++



  • Weil es ja wohl genug ints gibt, um einen Befehlscode zurückzugeben. 58389572394923 verschiede gibt's eher net, 65535 reichen da wohl komplett. -.-


  • Mod

    [C++ Standard]

    3.6.1 Main function
    A program shall contain a global function called main, which is the designated start of the program. It is implementationdefined whether a program in a freestanding environment is required to define a main function. [ Note: in a freestanding environment, start-up and termination is implementation-defined; start-up contains the execution of constructors for objects of namespace scope with static storage duration; termination contains the execution of destructors for objects with static storage duration. —end note ]

    An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int, but otherwise its type is implementation-defined. All implementations shall allow both of the following definitions of main :

    int main () { / * . . . * / }
    

    and

    int main (int argc , char * argv []) { / * . . . * / }
    

    In the latter form argc shall be the number of arguments passed to the program from the environment in which the program is run. If argc is nonzero these arguments shall be supplied in argv[0] through argv[argc-1] as pointers to
    the initial characters of null-terminated multibyte strings (NTMBSs) (17.3.2.1.3.2) and argv[0] shall be the pointer to the initial character of a NTMBS that represents the name used to invoke the program or "". The value of argc shall be nonnegative. The value of argv[argc] shall be 0. [ Note: it is recommended that any further (optional) parameters be added after argv. —end note ]

    The function main shall not be used (3.2) within a program. The linkage (3.5) of main is implementation-defined. A program that declares main to be inline or static is ill-formed. The name main is not otherwise reserved. [ Example:
    member functions, classes, and enumerations can be called main, as can entities in other namespaces. —end example ]

    [/C++ Standard]

    Und der tiefere Sinn dahinter: Der Rückgabewert eines Programmes soll üblicherweise einen Fehlercode darstellen und dieser ist vom Typ int. Du kannst da natürlich auch versuchen, was anderes zurückgeben. Dann musst du aber damit rechnen, dass alle anderen Programme sich an die übliche Konvention halten und deinen Rückgabewert als int-Fehlercode interpretieren.



  • Ok, also der Standart schreibt es vor. Das wollte ich wissen!

    Vielen Dank
    lg, freakC++

    PS.: Warum wird mein "Standard" zensiert?



  • Weil du es wahrscheinlich mit 't' statt mit 'd' am Ende schreibst.


  • Mod

    freakC++ schrieb:

    PS.: Warum wird mein "Standard" zensiert?

    Weil das Forum so nett ist, die üblichsten Rechtschreibfehler zu zensieren. Wer hier öfters Texte liest, weiß das zu schätzen.

    edit: Ups, das kommt davon wenn man mitten im Satz die Aussage ändern will. Grammatikfehler korrigiert.



  • Hallo,

    haha...das wusste ich noch gar nicht!! Find' ich lustig. Jetzt müsste es nur noch die Grammatik verbessern:

    Wer du hier öfters Texte liest, weiß das zu schätzen.

    🙂

    lg, freakC++


Anmelden zum Antworten