# main function in C



## PC person (Feb 2, 2006)

I'm right now reading C for dummies (the big large book) and have a question about using int main.

The way it describes using this function this way is confusing, it says by using int main, you are asking the main to return an integer value for you at the end, of which you would use return(0) when you end the function.

Just why is it necessary to return the integer zero at the end? I really have no desire to do so, and don't see why I have to. If I wanted a simple program to do nothing but input on the screen printf(hello world/n) why would I need to start the main function with int main and return (0) (at the end) My guess is that it's a way of returning control to the operating system, or some way of ending your program. I just wish they had worded it differently.


----------



## shuuhen (Sep 4, 2004)

While some compilers allow void main(), it can create problems and is generally considered bad practice (I think it's also standard to use int main()). There may be more, but this is what I understand of this issue:

Consider three programs:

```
int main() {
  return 0;
}
```
 This first program returns a value of 0 to the operating system, which means your program ran successfully. This is important for any programs/scripts requiring success or failure.

```
void main() {
}
```
 This second program does not return a success value to the operating system. Scripts will not be able to test for success or failure. Anything else requiring success or failure will most likely fail as well. Also, some compilers do not allow void main(), which means someone will need to go through your code later if it's compiled on a compiler that does not allow it.

```
int main() {
}
```
The issue in the final program is the return 0; at the end of an otherwise valid int main() function. I've read that return 0; is assumed for int main(), but in my experience that isn't true at all. On my Mac OS X machine I would get an exit status of 1 and on my Gentoo Linux box I would get an exit status of 176. Fairly unpredictable, eh?


----------



## MattBro (Nov 11, 2006)

The baseline is: If you don't need the return value, then don't use it.

The return value is passed to the host process (if they choose to), hence that which created your process (program), in most cases this will be the operating system. All operating system's I know of ignore the return value, as it has no way to interpret it (some say it's a general "fail" or "success", this is a myth)

No problems will occur either way you do it. But, a return is always given back to the host process, even if you declare the function as returning nothing (void).

If the return is void, the actual return value will be the contents of the EAX register at time of return, which could be anything.

So in short, unless you require the return value for some reason, there's no need to supply a return value.


----------



## shuuhen (Sep 4, 2004)

MattBro said:


> The baseline is: If you don't need the return value, then don't use it.
> 
> The return value is passed to the host process (if they choose to), hence that which created your process (program), in most cases this will be the operating system. *All operating system's I know of ignore the return value, as it has no way to interpret it (some say it's a general "fail" or "success", this is a myth)*
> 
> ...



Uh, I seriously disagree. In all Unix-like operating systems I've used (many Linux distros, Mac OS X, FreeBSD) 0 means success and non-zero values mean failure. This should hold true for other Unix-like operating systems. The shell _will_ store the return value of the command. Many scripts would not be possible if people didn't follow this standard. The ANSI standard says not to use void main() for a reason.

Try compiling the second and third programs I listed in my first post in this thread using

```
gcc -Wall [I]prog[/I].c
```
Baseline: Declare main() as int and return a value of 0 at the end.


----------



## MattBro (Nov 11, 2006)

Again, main's return value is _optional_ and only required if it's needed.


main() returning a value is not a standard, it's a suggestion. Same applies to the other baseless 'standards' out there such as "using GOTO is a sin against the computer"


----------



## shuuhen (Sep 4, 2004)

MattBro said:


> Again, main's return value is _optional_ and only required if it's needed.
> 
> 
> *main() returning a value is not a standard*, it's a suggestion. Same applies to the other baseless 'standards' out there such as "using GOTO is a sin against the computer"


Maybe my second post was a little unclear and/or written too quickly. Using 'return 0;' is more of a guideline, but I was referring to how using 'int main()' instead of 'void main()' is part of the ANSI/ISO standard. What I was trying to point out is that not declaring a specific return value in an 'int main()' function can produce behavior similar to using 'void main()' which is bad practice.

And the GOTO thing is considered bad practice because it generally makes the logic of a program much harder to follow.


----------



## MattBro (Nov 11, 2006)

Just because something is a standard does not mean it must be followed, informal or not. Since the thread starter is new to the language, I think his life should be made as easy as possible up until the point some irrelevent feature such as main's return interpretation is actually important.

If you're well adjusted to assembly, GOTO makes perfect sense


----------



## shuuhen (Sep 4, 2004)

MattBro said:


> Just because something is a standard does not mean it must be followed, informal or not. Since the thread starter is new to the language, I think his life should be made as *easy as possible* up until the point some irrelevent feature such as main's return interpretation is actually important.


Wouldn't that mean learning Java? C is a better language to learn programming skills, but it's definitely not the easiest language. A part of practicing programming should involve getting better at writing good code, instead of what works on just one compiler or what's easy.



MattBro said:


> If you're well adjusted to *assembly*, GOTO makes perfect sense


Have fun with that. PIC18 assembly drove me insane (not that I had to do huge amounts of it). :3-nuts:


@ the original poster: Either my first post or MattBro's first post should make enough sense to you to pick one or the other to follow. If not, hopefully this extra discussion of the topic will help you decide.


----------



## MattBro (Nov 11, 2006)

Oh hell no I wouldn't suggest Java. I was just commenting that if he's just beginning to learn the language it would be better not to weigh him down with irrelevent (at this point) features.

40-50% of most of my programs is assembly  I'm writing an OS at the moment in pure x86 asm


----------

