LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-26-2004, 11:12 AM   #1
Meatwad
Member
 
Registered: Dec 2002
Distribution: Debian, Libranet, Red Hat
Posts: 43

Rep: Reputation: 15
Return code from main() using gcc


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.
 
Old 01-26-2004, 11:40 AM   #2
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
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.
 
Old 01-26-2004, 12:02 PM   #3
cjcuk
Member
 
Registered: Dec 2003
Distribution: Openwall, ~LFS
Posts: 128

Rep: Reputation: 15
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?
 
Old 01-26-2004, 01:49 PM   #4
Meatwad
Member
 
Registered: Dec 2002
Distribution: Debian, Libranet, Red Hat
Posts: 43

Original Poster
Rep: Reputation: 15
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.
 
Old 01-26-2004, 01:54 PM   #5
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
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.
 
Old 01-26-2004, 02:11 PM   #6
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
stdlib.h defines 0 as EXIT_SUCCESS
 
Old 01-26-2004, 03:16 PM   #7
Meatwad
Member
 
Registered: Dec 2002
Distribution: Debian, Libranet, Red Hat
Posts: 43

Original Poster
Rep: Reputation: 15
Quote:
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.

Last edited by Meatwad; 01-26-2004 at 03:18 PM.
 
Old 01-26-2004, 03:27 PM   #8
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
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.
 
Old 01-27-2004, 03:14 AM   #9
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419

Rep: Reputation: 35
When I compile both of these test programs,

C_EXAMPLE.C
Code:
#include <stdio.h>

int main()
{}
CPP_EXAMPLE.CPP
Code:
#include <iostream>

int main()
{}
compiled with:

gcc c_example.c -o c_example -Wall
g++ cpp_example.cpp -o cpp_example -Wall

and then I test them, like this:

Code:
$ ./c_example 
$ echo $?
$ 0
$
$ ./cpp_example
$ echo $?
$ 0
it seems both gave 0 as their return value. Were you doing this...
Code:
$ ./c_example && echo $?
 
Old 01-27-2004, 08:14 AM   #10
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
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.
 
Old 01-27-2004, 11:52 AM   #11
cjcuk
Member
 
Registered: Dec 2003
Distribution: Openwall, ~LFS
Posts: 128

Rep: Reputation: 15
Quote:
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.
 
Old 01-27-2004, 06:13 PM   #12
Meatwad
Member
 
Registered: Dec 2002
Distribution: Debian, Libranet, Red Hat
Posts: 43

Original Poster
Rep: Reputation: 15
This is what I get with gcc 2.95 and 3.2:

gcc -g -Wall c_exm.c -o c_exm
c_exm.c: In function `main':
c_exm.c:4: warning: control reaches end of non-void function


Using 3.2, I get 0 return codes on both, but adding even 1 statement to the c program gives me a different return code.
 
Old 01-27-2004, 06:34 PM   #13
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419

Rep: Reputation: 35
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.
 
Old 01-27-2004, 06:36 PM   #14
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
Ok... apparently I haven't been clear....

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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
dd return code barefootdoctor Linux - General 2 10-13-2005 12:54 PM
Wine failed with return code 137 whishkah Linux - Software 2 06-13-2005 03:46 AM
return value from shell script to c code? khucinx Programming 1 05-13-2004 03:43 PM
How to change function parameter value and return back to the main shell program Bassam Linux - General 1 01-26-2004 10:02 AM
Getting return value of main() gommo Programming 2 06-16-2002 08:06 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:30 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration