I’m on week 2 and saw this a lot, I’ve been searching this up, and it’s been giving me a headache, so thought I’d ask here...
What does each part mean (int, main, void) mean? And Why do we need the line?
What’s the difference between int main() vs int main(void) - and the implications in the terminal results?
What if we didn’t have it? What would the terminal results show if it was missing?
Thank youuu 🙏
Videos
I'm currently taking a systems programming class that's required by my college. I do have some experience coding in C before taking this course.
Our professor had first coded in C with the main function as void main() saying (something like) how using void doesn't return anything and the computer doesn't need to store memory for that return value. I'm used to writing int main() as I've done before and so that's what I used in my first assignment.
Well upon checking my grade for this assignment, I was told that since I used int as the main function's return type then I need to have a return statement at the end of the function. To my knowledge, I thought that the program would automatically return 0 at the end so I didn't put it. And my code still does compile without it so I don't see how I NEED to have it there.
Would it be bad practice that I didn't have a return statement? Can anyone can give more insight into the topic of int vs void main() and exit codes and whatnot?
In C++, there is no difference.
In C, the difference is questionable. Some love to argue that the latter version (the one without void) is technically just a common implementation extension and not guaranteed to work by the standard because of the wording in the standard. However, the standard clearly states that in a function definition an empty set of parameters has a well-defined behaviour: that the function does not take any parameters. Thus such a definition for main matches the following description in the standard:
It [main] shall be defined with a return type of int and with no parameters.
There is, however, a noticeable difference between the two: namely, the version without void fails to provide a correct prototype for the function:
// this is OK.
int main()
{
if (0) main(42);
}
// this requires a diagnostic to be shown during compiling
int main(void)
{
if (0) main(42);
}
Oh, and just to be complete: the void has the following meaning in all function declarators:
(6.7.6.3p10) The special case of an unnamed parameter of type void as the only item in the list specifies that the function has no parameters.
First of all, there is a difference of what is allowed for hosted systems and freestanding systems, as shown here.
For hosted systems, 5.1.2.2.1 Program startup applies:
The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:
int main(void)
... (more text follows regarding argv/argc etc styles).
The interesting part is "with no parameters". int main() and int main (void) are currently equivalent, since they are both function declarators and have no parameters. The following applies (6.7.6.3 ):
10 The special case of an unnamed parameter of type void as the only item in the list specifies that the function has no parameters.
/--/
14 An identifier list declares only the identifiers of the parameters of the function. An empty list in a function declarator that is part of a definition of that function specifies that the function has no parameters. The empty list in a function declarator that is not part of a definition of that function specifies that no information about the number or types of the parameters is supplied.145)
Emphasis mine, the bold text is what applies to int main(). There is also note 145) at the end of the text, which says "See ‘‘future language directions’’ (6.11.6)":
6.11.6 Function declarators
The use of function declarators with empty parentheses (not prototype-format parameter type declarators) is an obsolescent feature.
And here is the difference. Being a function declarator, int main() is bad style because of the above, since it is not guaranteed to work in the next version of the C standard. It is flagged as an obsolescent feature in C11.
You should therefore always use int main (void) on a hosted system and never int main(), even if the two forms are, for now, equivalent.
In C++ both forms are completely equivalent, but there int main() is the preferred style for subjective, cosmetic reasons (Bjarne Stroustrup says so... which is probably quite a bad rationale for explaining why you do something in a particular way).
The overwhelming majority of the time, one of int main(void) or int main(int argc, char* argv[]) is what you need to use. In particular, if you're writing a program that's going to be compiled by any major compiler for running on a personal computer, with the full set of the C standard libraries, then you almost certainly need to be returning an int from main.
(I would also avoid using an empty argument list, see "Why don't we use (void) in main?")
The C99 standard does allow for other implementation-defined signatures, and you can use these if you've read the manual for your compiler and it says you can.
(5.1.2.2.1) It shall be defined with a return type of int and with no parameters ... or with two parameters ... or in some other implementation-defined manner
Personally I would avoid them even if they are allowed (if possible), because it's one more thing to worry about if you ever need to port to another system.
See the comments below "Why don't we use (void) in main?" for some interesting discussion on this.
If your book says void main() it is very very out of date.
Unless you are in a very unlikely system where you have a freestanding main - see Why is the type of the main function in C and c++ left to the user to define?
The difference is one is the correct way to define main, and the other is not.
And yes, it does matter. Either
int main(int argc, char** argv)
or
int main()
are the proper definition of your main per the C++ spec.
void main(int argc, char** argv)
is not and was, IIRC, a perversity that came with older Microsoft's C++ compilers.
https://isocpp.org/wiki/faq/newbie#main-returns-int
Bjarne Stroustrup made this quite clear:
The definition
void main()is not and never has been C++, nor has it even been C.
See reference.