int main() oder void main()?



  • Guten Abend,

    da ich leider im Forum durch die Suche keine passenden Treffer erhalten habe und mich meine Quellen ein bißchen im Regen stehen lassen, wende ich mich nun vertrauensvoll an das C Forum - ihr müsst es ja wissen. 😉

    Ich habe das Problem, dass ich bisher alle C Programme mit "int main().." geschrieben habe, da ich irgendwo mal gelesen habe, dass der Rückegabewert von main() vom Typ int sein muss. Vor 2 Tagen ist mir nun ein C-Buch eines Freundes in die Hände gefallen, wo permanent nur mit "main()" bzw mit "void main(void)" gearbeitet wird. Letzte Woche hat dann mein Programmierprofessor (das berühmte hello world Programm) ebenfalls mit "void main()..." eingeleitet... huch?

    Nun weiß ich nicht was richtig ist und weshalb. Bei Wikibooks habe ich gefunden, wo "meine" Variante als die richtige (laut C99 Standart) betitelt wird. Den C99 Standart habe ich leider nirgends schriftlicht gefunden - laut pronix.de soll es ihn online nicht mehr geben.. steh ich also wieder am Anfang..

    Da steht nun für mich mein Prof/Buch gegen meine bisherige Gewohnheit und Wikibooks. Was ist denn nun richtig und weshalb? Ich danke für alle mir verständlichen Aufklärungsversuche. 😉



  • Prof und Buch haben keine Ahnung. Du hast recht.



  • es geht beides.



  • ANSI C 99, ISO/IEC 9899:1999 schrieb:

    5.1.2.2.1 Program startup

    1
    The function called at program startup is named main. The implementation declares no
    prototype for this function. It shall be defined with a return type of int and with no
    parameters:
    int main(void) { / ... / }
    or with two parameters (referred to here as argc and argv, though any names may be
    used, as they are local to the function in which they are declared):
    *int main(int argc, char *argv[]) { /* ... / }
    or equivalent;9) or in some other implementation-defined manner.

    2
    If they are declared, the parameters to the main function shall obey the following
    constraints:

    — The value of argc shall be nonnegative.

    argv[argc] shall be a null pointer.

    — If the value of argc is greater than zero, the array members argv[0] through
    argv[argc-1] inclusive shall contain pointers to strings, which are given
    implementation-defined values by the host environment prior to program startup. The
    intent is to supply to the program information determined prior to program startup
    from elsewhere in the hosted environment. If the host environment is not capable of
    supplying strings with letters in both uppercase and lowercase, the implementation
    shall ensure that the strings are received in lowercase.

    — If the value of argc is greater than zero, the string pointed to by argv[0]
    represents the program name; argv[0][0] shall be the null character if the
    program name is not available from the host environment. If the value of argc is
    greater than one, the strings pointed to by argv[1] through argv[argc-1]
    represent the program parameters.

    — The parameters argc and argv and the strings pointed to by the argv array shall
    be modifiable by the program, and retain their last-stored values between program
    startup and program termination.

    1. Thus, int can be replaced by a typedef name defined as int, or the type of argv can be written as
      char ** argv, and so on.

    der wind hat es mir zugefluestert 😉

    alles geht in speziellen oder embedded compilern. ob es dann aber auch wo anders laufen wird, weiss keiner.



  • Bei "embedded systems" reicht ein einfaches

    void main(void)
    {
        while(1)
        {
            ...
        }
    }
    

    da du dieses Programm nie verlässt und auch keine Rückgabe brauchst.
    😃



  • Und standardkonform gibt main int zurück.

    http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1124.pdf
    Seite 24 vom pdf (bzw Dokument Seite 12), program startup, falls es dich interessiert.



  • Wenn dann bitte auch vollständig lesen. Dann merkt man nämlich auch, dass void main(void) auch standardkonform sein kann.



  • du meinst wohl "5.1.2.2.3 Program termination"?

    If the return type of the main function is a type compatible with int, a return from the
    initial call to the main function is equivalent to calling the exit function with the value
    returned by the main function as its argument;10) reaching the } that terminates the
    main function returns a value of 0. If the return type is not compatible with int, the
    termination status returned to the host environment is unspecified.



  • Nö. Ich meine:

    5.1.2.1 Freestanding environment
    In a freestanding environment (in which C program execution may take place without any benefit of an operating system), the name and type of the function called at program startup are implementation-defined. Any library facilities available to a freestanding program, other than the minimal set required by clause 4, are implementation-defined.
    The effect of program termination in a freestanding environment is implementation -defined.



  • TactX schrieb:

    ...
    In a freestanding environment (in which C program execution may take place without any benefit of an operating system), the name and type of the function called at program startup are implementation-defined.
    ...

    unter m$-windoofs heisst sie auch manchmal 'wmain' oder '_tmain' 😉



  • Naja, Microsoft scheisst ja eh schon seit einer Weile auf C(99).


Anmelden zum Antworten