It will be explained later don't worry too much about it now. Answer from FZTR on reddit.com
🌐
Reddit
reddit.com › r/cs50 › what does int main(void) mean?
r/cs50 on Reddit: What does int main(void) mean?
May 13, 2022 -

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...

  1. What does each part mean (int, main, void) mean? And Why do we need the line?

  2. What’s the difference between int main() vs int main(void) - and the implications in the terminal results?

  3. What if we didn’t have it? What would the terminal results show if it was missing?

Thank youuu 🙏

🌐
GeeksforGeeks
geeksforgeeks.org › c++ › difference-int-main-int-mainvoid
Difference between "int main()" and "int main(void)" in C/C++? - GeeksforGeeks
October 19, 2023 - prog.cpp: In function ‘int main()’: prog.cpp:8:23: error: too many arguments to function ‘void fun()’ fun(10, "GfG","GQ"); ^ prog.cpp:4:6: note: declared here void fun(void) { } ^ Unlike the last example, this example fails in compilation in both C and C++. ... In C++, both fun() and fun(void) are same. So the difference is, in C, int main() can be called with any number of arguments, but int main(void) can only be called without any argument.
🌐
TutorialsPoint
tutorialspoint.com › differentiate-between-int-main-and-int-main-void-function-in-c
Differentiate between int main and int main(void) function in C
Actually, both seem similar, but int main(void) is technically better as it clearly indicates that main can only be called without any parameters.
🌐
TutorialsPoint
tutorialspoint.com › difference-between-void-main-and-int-main-in-c-cplusplus
Difference between void main and int main in C/C++
<< endl; return 0; // here returning 0 indicates successful execution } ... Hello TutorialsPoint Learner! The void main() indicates that the main() function will not return any value to the operating system. This is used when you write very simple programs which run without any errors and reach ...
🌐
Reddit
reddit.com › r/c_programming › void main() vs int main()
r/C_Programming on Reddit: void main() vs int main()
October 24, 2021 -

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?

Top answer
1 of 5
100
The standard is int main(), and the return can be omitted on C99 and later. void main() is not standard and doesn't work on all compilers.
2 of 5
41
From the C99 Standard: 5.1.2.2.1 Program startup 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) { /* ... */ } or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared): int main(int argc, char *argv[]) { /* ... */ } or equivalent; 9) or in some other implementation-defined manner. And: 5.1.2.2.3 Program termination If the return type of the main function is a type compatible with int, a return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument; 10) reaching the } that terminates the main function returns a value of 0. If the return type is not compatible with int, the termination status returned to the host environment is unspecified. But also: 5.1.2.1 Freestanding environment 1 In a freestanding environment (in which C program execution may take place without any benefit of an operating system), the name and type of the function called at program startup are implementation-defined. [...] 2 The effect of program termination in a freestanding environment is implementation- defined. So, if your are running under an operating system you must use int main(void) or int main(int argc, char *argv[]) or equivalent. You can return something or rely on returning 0 automatically when exiting main(). If you are not running under an OS, it all depends. But if there is no OS, where do you want to return something to? I don't know about pre-C99, but I'm sure the above did not change as of today. As a side note, this statement from your prof the computer doesn't need to store memory for that return value gives me a bad feeling. You better read a good book instead of listening to him if you want to learn C. This was a thing in the 80s and is totally irrelevant today (e.g. the compiler could decide to store it in a register and not to store it in memory).
Top answer
1 of 10
57

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.

2 of 10
6

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).

Find elsewhere
🌐
BYJUS
byjus.com › gate › difference-between-int-main-and-int-main-void-in-c-c-plus-plus
Difference Between “int main()” and “int main(void)” in C/C++
September 30, 2022 - Thus, the main() function is a prerequisite to every program. This function, just like the C function, consists of a definition. Here, the int main(), the void main(), and the void main() are basically the function definitions of the main().
🌐
Sololearn
sololearn.com › en › Discuss › 2329048 › what-is-the-difference-in-int-mainvoid-and-no-return-0-and-int-main-with-return-0-
What is the difference in int main(void) and no return 0; and int main() with return 0; ? | Sololearn: Learn to code for FREE!
First let me clear that “main(void)” is the same as “main()” The only difference between those two is that the “void” keyword inside main clearly specifies that main has no parameters.
🌐
HackerEarth
hackerearth.com › practice › notes › is-it-correct-to-use-void-main
Is it valid to use void main() in C and C++? | HackerEarth
The int returned by main() is a way for a program to return a value to the system that invokes it. In Linux, the process always exits with a status value that the kernel collects. On systems that doesn't provide such a facility the return value is ignored, but that doesn't make void main() legal.
🌐
Quora
quora.com › What-does-int-main-void-mean-Can-someone-explain-like-I-m-5
What does int main (void) mean? Can someone explain like I’m 5? - Quora
Answer (1 of 11): [code]int main(void) [/code]is the opening line of the definition of a function named main. In some languages (e.g., C, C++, etc.), the main function is where all the action starts when you later run your program.
🌐
Quora
quora.com › Why-do-we-use-void-main-or-what-is-its-significance-in-C-programming
Why do we use 'void main' or what is its significance in C programming? - Quora
Answer (1 of 53): Before knowing what is the significance of void main in C, let me tell you something. When man thought of building the first computer it's not that how it looks today with a screen or a CPU, it's just a large mechanical machine which is used to do maths calculations like the one...
🌐
Sololearn
sololearn.com › en › Discuss › 583759 › when-to-use-void-main-and-when-to-use-int-main-in-c-language-will-anyone-help-me-by-posting-examples-please
When to use 'void main ()' and when to use 'int main ()' in C language ? Will anyone help me by posting examples, please. | Sololearn: Learn to code for FREE!
void main() was kept around only for backwards compatibility, but you should avoid using it. ... void main is used when you want that your function should not return any value. However, if you want the function to return an int value, we use int main
🌐
Sololearn
sololearn.com › en › Discuss › 2500543 › difference-between-int-main-and-void-main
Difference between int main() and void main() | Sololearn: Learn to code for FREE!
As void means nothing or null ... int main() -->returns an Integer value. void main()--->returns nothing.. int and void are datatypes that defines the function. ... The main() function is like other functions.
🌐
Shiksha
shiksha.com › home › it & software › programming › colleges in india
Difference Between int main and void main in C/C++
July 4, 2025 - Students can get admissin into some of the top programming institutes in India based on their merit scores as well as their marks in entrance exams like JEE MAIN, GATE, etc. Amity University, MIT-WPU, Parul University, IIT Madras, IIT Kanpur, School of Engineering and Technology, Coding Ninjas, IIT Bombay, K J Somaiya School of Engineering, IIT Delhi, etc. are the best programming colleges in India. ... Programming is the process of generating a set of commands to use artificial intelligence and computer systems for performing tasks.
🌐
Testbook
testbook.com › home › key differences › difference between int main() and int main(void) in c/c++ | testbook.com
Difference Between int main() and int main(void) in C/C++ | Testbook.com
Crack Your Exam with Super Pass Live with India’s Best Teachers and Coaching ... The definition of "int main(void)" is largely the same as that of "int main()", with one key difference: "int main(void)" does not accept any arguments.
🌐
EasyExamNotes
easyexamnotes.com › void-main-in-c-programming
Void main in C Programming – EasyExamNotes.com
In C programming, the void main() function is used to define the starting point of a program.