ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I've noticed that programs I compile using gcc don't return a 0 on success. Using g++ they do. I'm curious if this is the expected result or if this is a bug/undocumented feature. I tried on gcc 2.95 and 3.2.
Are these programs you wrote yourself? Or programs written by other people? There is no errno definition as far as I know for a success exit so most people just use 0 but I suppose you could use whatever you want, though it wouldn't make sense to use something defined by the errno.h file for your architecture.
What do you mean by, ``don't return a 0 on success''? Are you referring to the shell not showing a return value when queried or have you disassembled the program?
I'm not using a 'return 0' statement as the last line in my source. I'm checking the return code with echo $?. The default behavior for most processes is 0 for success and I was wondering why g++ uses this behavior but gcc doesn't.
Well it is your responsibility to set the return value. If you encounter an error you return something corresponding to your error, and if you don't you return 0. The compiler isn't just going to guess if your application preformed as you wanted it to. If you have no return statement in your main function then you should be getting a warning when you compile with the -Wall option.
Originally posted by jtshaw The compiler isn't just going to guess if your application preformed as you wanted it to. If you have no return statement in your main function then you should be getting a warning when you compile with the -Wall option.
Is that a difference between C and C++ or a difference between gcc and g++? g++ gives no warning and if the program successfully executes it always returns 0 even though I have no return statement.
Well, lets put it this way, if your function is of type int and you don't explicitly return an int then you are not programming the function correctly, regardless of the language. It might work, but it is bad form.
Originally posted by jtshaw Just so you know, the fact that C program returning 0 is probably dumb luck. C doesn't init anything to a value so you can't expect that to happen.
Yeah, it seems that GCC (well, at least 3.3.1 on IA32) seems to always chuck a `mov $0x0,%eax' into the prologue of the main function. However, 2.95.3 does not, and assume many other version may not - also, other compilers probably will not.
Well, why dont you compile your program like this:
gcc -g -S -Wall c_exm.c -o c_exm
and check out the machine code. The reason you get a different return code
by "adding even 1 statement" is that each and every line of C code uses the
machine registers, and it is their contents that determine the return value at the
end of the main procedure.
You MUST have a return statement in a function that is declared anything other then void in order for any competent programmer to consider the function correctly coded. I don't care if you are using C or C++, I don't care what the warning message is. If you are declaring functions with return values and not returning a value your implementation of that function is poor, and incorrect.
WRONG:
int main{}
{
}
RIGHT:
int main{}
{
return 0;
}
It is perfectly reasonable to place a "return EXIT_SUCCESS;" or "return 0;" as the last line in your function. It is also perfectly reasonable to define a list of error codes if different behaviors occur and return those values. It is NOT reasonable to have no return statement and expect the compiler to clean up your badly written code and make it perform in a consistent manner.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.