What is the impact of using the void keyword before the main function in C?

Click For Summary

Discussion Overview

The discussion centers on the implications of using the void keyword before the main function in C programming. Participants explore the syntax, standards compliance, and potential consequences of defining main with a return type of void.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • Some participants assert that using void before the main function means it returns no value, which is generally valid syntax but not appropriate for main.
  • One participant emphasizes that the main function must return an integer to comply with the standard, indicating success or failure.
  • Another participant argues that defining main as void can lead to undefined behavior, as it may return garbage values to the calling environment.
  • It is noted that the C++ standard requires main to return int, while the C standard allows compilers to permit other return types, though this is considered nonportable.

Areas of Agreement / Disagreement

Participants generally agree that the main function should return an integer, but there is contention regarding the permissibility of using void as a return type in C, with some arguing it is nonportable.

Contextual Notes

The discussion highlights the potential for undefined behavior when using void for main, but does not resolve the nuances of compiler-specific behavior or portability issues.

kthouz
Messages
188
Reaction score
0
What is the effect of putting void before main function i mean?

void main()
{
}
void main(void)
{
}
 
Technology news on Phys.org
What you wrote means the function returns no value. This is valid syntax in general, but not for the main function. Many functions don't have a return value. For example, a function that receives a structure pointer as an argument and updates the contents of the structure might well have no return value.

The main function must return an integer (an int) to be compliant with the standard. The returned value indicates success (zero) or failure of some sort (non-zero).
 
DH is completely right: void main() is not okay, 'not nowhow, not noway' as the Lion said to Dorothy.

If you leave it as void main(), when the program returns to the shell or whoever called it, the return value can be any integer, ie. whatever garbage is left in stack space. If it returns a zero, it is probably pure luck - programming by coincidence.
 
main() should be defined as 'int main()' or in many cases 'int main(int argc, char * argv[])'.
 
The C++ standard requires main to return int.

The C standard permits compilers to allow other return types -- but such code is, in principle, nonportable.
 

Similar threads

Replies
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 36 ·
2
Replies
36
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 34 ·
2
Replies
34
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K