Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 468
  • Last Modified:

why does main return a value?

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
thecuriousboy
Asked:
thecuriousboy
1 Solution
 
gj62Commented:
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
 
tinchosCommented:
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
 
deicheleCommented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ExceterCommented:
>> 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
 
KocilCommented:
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
 
KrypCommented:
> 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
 
cupCommented:
Also note if you ever move to C++, main always returns int: never void unless you use the MS compiler.
0
 
ExceterCommented:
>> > 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
 
thecuriousboyAuthor Commented:
yes, it works. thanks a lot for your answer sir.
0

Featured Post

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now