Int Main und Void Main!Was ist der Unterschied?
-
Ja also ich versteh nicht gabz den Unterschied zwischen Int Main und Void Main!
Wer will kann das mit mir per ICQ reglen!293398369
-
Bei Funktionen (wie main()) bedeutetd der rückgabewert void, dass garnichts zurückgegeben wird. Ein int bedeuted, dass eine integer(Ganzzahl) zurückgegeben wird.
Bei main() ist der Unterschied bloß, dass man zum Schluß mit return(0); eine Integerzahl zurückgeben muss. (0 bedeuted kein Fehler)Gruß
Glamdring
-
void main() ist falsch und nicht standardkonform.
Bei standardkonformen Compilern muss man bei int main() kein return ausführen
-
int oder void? Die Frage wird hier öfter diskutiert, daher sollten wir das Original bemühen:
Auszug aus dem C++-Standard ISO 14882 (First edition, 1998-09-01):
3.6 Start and termination
3.6.1 Main functionA 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. ...
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 implementationdefined. 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[argc1] as pointers to the initial characters of nullterminated 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. ...
The function main shall not be used (3.2) within a program. The linkage (3.5) of main is implementationdefined. A program that declares main to be inline or static is illformed. The name main is not otherwise reserved. [Example: member functions, classes, and enumerations can be called main, as can entities in other namespaces.]
Calling the function void exit(int); declared in <cstdlib> (18.3) terminates the program without leaving the current block and hence without destroying any objects with automatic storage duration (12.4). If exit is called to end a program during the destruction of an object with static storage duration, the program has undefined behavior.
A return statement in main has the effect of leaving the main function (destroying any objects with automatic storage duration) and calling exit with the return value as the argument. If control reaches the end of main without encountering a return statement, the effect is that of executing return 0;____________________
Fazit: main(){...} muss einen Rückgebewert vom Typ int haben.
Ohne "return 0;" sorgt der Compiler für die Rückgabe von 0.
-
Die Antwort kann man kürzer formulieren: Das Forum besitzt eine Suchen-Funktion!
-
Ähmm...also wenn ich das richtig verstanden hab!Soll ich einfach immer int main machen und brauche am ende kein return 0?
PS:Ich hab Visual Studio 6.0
-
Etyx schrieb:
Ähmm...also wenn ich das richtig verstanden hab!Soll ich einfach immer int main machen und brauche am ende kein return 0?
PS:Ich hab Visual Studio 6.0Schreibe einfach immer int main() bzw., wenn du dem Programm Kommandos übergibst die du auch auswertest, int main(int argc, char* argv[]) und am Ende der Methode return 0; Damit bist du immer auf der sicheren Seite. void main() sind so Unarten von VC++, etc. Usern (damit hab ich mir jetzt wohl ein paar Feinde gemacht
)
Cu
André
-
Visual Studio 6.0 ist nicht mehr auf der Höhe des C++-Standards. Wichtig ist, dass Du den Standard kennst. Ohne "return 0;" geht es trotzdem; er gibt nur eine Warnung aus.
-
Ja das ist mir auch schon eingefallen okay damit wäre die Frage gelöst!
thx@all
Das Thema kann geclost werden!