?
Solved

why does main return a value?

Posted on 2003-02-22
9
Medium Priority
?
466 Views
Last Modified: 2012-05-04
HELLO EXPERTS, THIS IS A GENERAL QUESTION ABOUT C/C++
PLEASE TELL ME WHY DOES THE MAIN() RETURN A VALUE AND TO WHOM DOES IT RETURN. (TO THE OPERATING SYSTEM ?). CAN YOU PLEASE GIVE ME AN EXAMPLE WHERE THIS CAN PROVE USEFUL I MEAN WHERE AND HOW CAN WE USE THE VALUE RETURNED BY MAIN.
THANKS.
THE CURIOUS BOY.
0
Comment
Question by:thecuriousboy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 6

Expert Comment

by:gj62
ID: 7999299
main() CAN return a value - it does not have to.  You can define void main() if you don't want to return a value.

main() can return a value so that other processes that called your program can get a return value.

It would be helpful to know what platform you are on to give you an example (but batch files in DOS and most scripts on UNIX can work with return values from a program that you write...)
0
 
LVL 9

Expert Comment

by:tinchos
ID: 7999380
As gi62 told main can return a value

void main()
{}

or
int main()
{}

and it is used in order to communicate its result

generally a return value of 0 is a success and a return value != 0 is an error exit

sometimes, you can have a process waiting for some other's end to continue execution and depending on the return value do something different

generally speaking the functionality would be the same as in a function
0
 

Expert Comment

by:deichele
ID: 7999583
Other programs can call your program, and will need to know whether execution was succesful or not. A major use of return values from programs is shell programming. Many shell scripts rely on return values to determine their course(s) of action.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 8

Expert Comment

by:Exceter
ID: 7999753
>> PLEASE TELL ME WHY DOES THE MAIN() RETURN A VALUE AND TO WHOM DOES IT RETURN. (TO THE OPERATING SYSTEM ?).
>> CAN YOU PLEASE GIVE ME AN EXAMPLE WHERE THIS CAN PROVE USEFUL I MEAN WHERE AND HOW CAN WE USE THE VALUE RETURNED BY MAIN.

Main is like any other function it can return nothing or it can return a value. Why? That depends on what you are doing. Most commonly main returns an int.  1 signifying that the program terminated with an error and 0 meaning that the prgram terminated normally. To whom is this value returned? The short answer is simply whoever called it. However, this value is ignored by the OS, on a command prompt anyway, because it does not care how your program terminated. Therefore, if you are not planning on your program being called from another process it is perfectly acceptable to have main return void, or anything else for that matter as it does not make any difference.

Keep in mind that main can return ANYTHING that any other function can return. You could specify that main return void, char, int, float, double, long, pointers, etc... How you would capture these values is another question because you can only capture an int return value using popen() and pclose().

Exceter
0
 
LVL 5

Accepted Solution

by:
Kocil earned 150 total points
ID: 8000659
Make and compile this c program
-----
/* yesno.c */
main()
{
   printf("Press <Y> or any key>");
   return (getch() == 'Y' ? 0 : 1);
}
-----

Then make this test.bat file
-----
echo "Am I cool ? "
yesno.exe
if errorlevel 0 then goto lyes
echo "why not ?"
goto lend
:lyes
echo "Thank's, you too"
:lend

----
Try it and figure it out bys :)
0
 
LVL 2

Expert Comment

by:Kryp
ID: 8002413
> Many shell scripts rely on return values to determine their course(s) of action.
Yeah, I had to do this once when the programmer of the utility in question was a void main programmer.
Utter chaos in the return status - figuring out what really happened was a nightmare.

For the void main programmers who cling to "works for me"
http://users.aber.ac.uk/auj/voidmain.shtml
Not everyone uses sloppy MS compilers which don't give a damn about the status result.

Time to read the standard again
http://anubis.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/
And perhaps the FAQ
http://www.eskimo.com/~scs/C-faq/s11.html

> However, this value is ignored by the OS
WRONG!
This value MAY be ignored by the OS (there's more that one OS you know), but your program should be declared in such a way as to return an int value.  Now the OS will know that main() returns an int, and can choose to ignore it.  It's much worse for the OS to expect a return status and not get one.

It's a good job you didn't post that on comp.lang.c

> Keep in mind that main can return ANYTHING that any other function can return.
> You could specify that main return void, char, int, float, double, long, pointers, etc
Oh please - void main is simply the tip of the incorrect iceberg - here's the rest of it.

#include <stdio.h>
struct f00l {
    int dummy[10000];
};
struct f00l main() {
    struct f00l result;
    return result;
}

Compiles fine with Borland/MS compilers (not a warning in sight), but boy does the OS not like it when you run it.  At least gcc warns you that the return result isn't an int.

If you use int main, then
- the compiler will never complain
- the OS will never complain
- the language police will never complain
- programmers who make use of the exit status will never complain
- changing or upgrading your OS/Compiler will not suddenly break your code

> because you can only capture an int return value using popen() and pclose().
And there's me thinking that popen() returned a FILE* stream to allow you to communicate with the stdio streams of a sub-process....

0
 
LVL 11

Expert Comment

by:cup
ID: 8002935
Also note if you ever move to C++, main always returns int: never void unless you use the MS compiler.
0
 
LVL 8

Expert Comment

by:Exceter
ID: 8004562
>> > However, this value is ignored by the OS
>> WRONG!

As you so eloquently pointed out, the OS MAY ignore it. Therefore, my statement was not totally "wrong." I was mearly speaking from the standpoint of the OS I am familiar with. I now realize that it was an over generalization and I appologize for saying it.

>> Oh please - void main is simply the tip of the incorrect iceberg - here's the rest of it.

I never said that is was good form, I said it was legal, and I agree with you that it would be a horibble mistake to write a program that did that. :-)

>> And there's me thinking that popen() returned a FILE* stream to allow you to communicate with the stdio streams of a sub-process....

It does. I was taking about how pclose() returns the sub-process's return value when it terminates that FILE* stream. As you know, the two commands are used together. :-)

Exceter
0
 

Author Comment

by:thecuriousboy
ID: 8009795
yes, it works. thanks a lot for your answer sir.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question