| | |
g++ and int main
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
I am running Ubuntu and compiling my C++ code using g++. For some reason I can't get it to compile my source code using VOID MAIN. It is always telling me that I must use INT instead. This isn't life threatening, but I was just wondering if g++ just doesn't support a main function with a return of void. Anyone run into this problem?
GM
GM
If in doubt, reach into the trash can and remove the user guide.
Yeah, the problem is you've been learning from people who never bothered to look at the standards. void main has never been a valid return type for main. Unfortunately for you, you've now got the pain of trying to unlearn something.
All compilers extend the language in some way, I guess you learnt with a compiler that blindly accepted void main without complaint. g++ is no different in that respect.
does a pretty good job of reigning it back to being just an ANSI C++ compiler, where the code has a pretty good chance of being compiled with any other ANSI C++ compiler.
As for the return result, you do can something like this in say a bash script
The shell variable $? contains the return value of the main in the program you most recently ran.
You can also get the return status in your C code if you want as well
Use the various 'W' macros to find out whether it was a normal exit (and thus a valid return status), or whether the program was killed or crashed.
Finally, SOME implementations of
All compilers extend the language in some way, I guess you learnt with a compiler that blindly accepted void main without complaint. g++ is no different in that respect.
g++ -W -Wall -ansi -pedantic prog.cppdoes a pretty good job of reigning it back to being just an ANSI C++ compiler, where the code has a pretty good chance of being compiled with any other ANSI C++ compiler.
As for the return result, you do can something like this in say a bash script
C++ Syntax (Toggle Plain Text)
./myprog if [ $? == 0 ]; then echo success fi
You can also get the return status in your C code if you want as well
C++ Syntax (Toggle Plain Text)
pid = fork(); if ( pid == 0 ) { /* run your other program */ execl("./prog", "./prog", (char*)NULL ); } else if ( pid != -1 ) { int status; wait( &status ); /* when prog exits, the return result is in status (along with some other stuff) */ }
Finally, SOME implementations of
system("./prog") also return the status, but this is far from certain. It is also used by programs when executing other programs. The return value tells the calling program whether it successfully executed its job or not. The return value is useful in a lot of situations, even in Windows programs.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
- void main vs int main (C++)
- int main() or void main() ??!! (C++)
Other Threads in the C++ Forum
- Previous Thread: problem implementing BFS using string vector
- Next Thread: Problems with Switch
Views: 1731 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






