Member Avatar for Geek-Master

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

The C++ standard says main is supposed to return int, right? I think that's the case, anyway.

So why would g++ support a main function with a return type of void? That makes no sense.

Member Avatar for Geek-Master

When you do use main to return an integer, that number that is sent back to the OS, how is it used? I know it is on the lines of error handling, 0 for no-errors and not 0 for errors.

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. g++ -W -Wall -ansi -pedantic prog.cpp 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

./myprog
if [ $? == 0 ]; then
 echo success
fi

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

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) */
}

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 system("./prog") also return the status, but this is far from certain.

commented: Cookie for Salem. +16
Member Avatar for Geek-Master

So the return is mainly used for automation of individual programs in a batch or bash script. I've not focused much on C++ since I work in a Windows environment, but I can use that bit of info Salem.

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.

Void Main never has worked in the last who knows how many years! It violates C+ standards right from the start. There are a lot of text book examples using "void" forget about them!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.