How to Fix the 'main' Must Return 'int' Error in C Programming

  • Thread starter Thread starter transgalactic
  • Start date Start date
  • Tags Tags
    Bug
Click For Summary

Discussion Overview

The discussion revolves around the error message "'main' must return 'int'" encountered in C programming when using a void return type for the main function. Participants explore the implications of the C standard regarding the return type of main, the differences between C and C++, and the potential reasons for the error.

Discussion Character

  • Debate/contested
  • Technical explanation
  • Exploratory

Main Points Raised

  • One participant questions how to resolve the error message related to the main function's return type.
  • Another suggests changing the return type of main to int and adding a return 0 statement at the end.
  • Some participants express confusion about the necessity of a return type for main, with one noting that their teacher believes the code is correct.
  • It is mentioned that C99 mandates main() to return int, and that reaching the end of main without a return statement implicitly returns 0.
  • Participants discuss the historical context of C standards, noting that older versions allowed main to be defined without an explicit return type.
  • One participant shares a link to a document claiming that void main() is acceptable in C, but acknowledges that their bug was due to using a C++ compiler.
  • Another participant argues that while some compilers may allow void main() as an extension, it is not guaranteed to be safe according to the standard.

Areas of Agreement / Disagreement

Participants do not reach a consensus on whether void main() is acceptable in C. There are competing views regarding the requirements of the C standard and the behavior of different compilers.

Contextual Notes

There are references to historical standards and compiler behaviors that may not align with current interpretations of the C standard. Some participants note that different compilers may handle the void main() definition differently.

transgalactic
Messages
1,386
Reaction score
0
how to solve this bug??

when i run this code
instead of getting some expected out put i get

line 17: `main' must return `int'

but main is a void function

it can't return int
?
Code:
#include <stdio.h>
int a(int n,int count){
      int i;
      for(i=0;i<n;i++)
           count =a(i,count);
           return count+1;
}

int b(int n,int count) {
  int i;
  count =a(n,count);
     for(i=0;i<n;i++)
     count =b(i,count);
     return count;
  }

  void main (){
    int i;
    for (i=0;i<4;i++)
      printf("%d",a(i,0));
      printf("\n%d\n",a(i,10));

      for (i=0;i<4;i++)
        printf("%d",b(i,0));
        printf("\n%d\n",b(i,10));

  }
 
Technology news on Phys.org


Why don't just make it return int and add return 0 at the main end?
 


i thought by definition mains had to return something.

Don't you need to declare those functions too?
 


Like rootX said your best option would be to use int main() instead of void main() and simply have a return 0; at the end of the main function.
 
Last edited:


but main is a void function

it can't return int
?

No it isn't. In C, and in C++, main() must return int, and 0 is used to indicate successful termination. Part of the confusion arise from the special status granted to main() whereby if the program arrives at the closing }, and there's no explicit return statement, it return 0 implicitly.
 


I think it is good form to always have the main function return a value, but I believe it is implied in ANSI C whereas ANSI C++ requires it explicitly (or is it the other way around?).

With issues like this it is not uncommon for one compiler to accept it and another to return an error.
 


Both (according to their respective latest standard version) implies it.

ISO/IEC 9899:1999 (C99) 5.1.2.2.3.1 said:
...; reaching the } that terminates the main function returns a value of 0. ...

ISO/IEC 14882:2003 (C++03) 3.6.1.5 said:
If control reaches the end of main without encountering a return statement, the effect is that of executing
Code:
return 0;
 


Well, it's not really just C99. C has always required main() to return int. Confusion arise as old C allow one to write
Code:
main(void)
i.e. with no return type. At the time, a function declaration with no explicit return type return an int implicitly.
 
  • #10


thanks
 
  • #11


i talked to my teacher and he is 100% percent sure that this code works

and he doesn't know what i am talking about

i don't know why he thinks its ok
because abviosly main has to be int with return 0; in the end
 
  • #12
  • #13
after reading this document:
http://homepages.tesco.net/J.deBoynePollard/FGA/legality-of-void-main.html

i understood that void main() is perfectly ok in C

and that my bug happened because i use C++ compiler

C++ allows only int main()
 
  • #14


No no no.

One, most of the compilers that exist code to C90, which doesn't specify the implementation-defined bit that article you linked to say.
C90 said:
5.1.2.2 Hosted environment
A hosted environment need not be provided. but shall conform to the following specifications
if present
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 can be defined with no parameters
Code:
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)
Code:
int main(int argc, char *argv[]) { /*. . . */ }
If they are defined. the parameters to the main function shall obey the following constraints
...
but some provide void main() as a compiler extension anyway.

In C99, you already saw
5.1.2.2.1 Program startup
1 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:
Code:
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):
Code:
int main(int argc, char *argv[]) { /* ... */ }
or equivalent;9) or in some other implementation-defined manner.
That mean, yes, the compiler is allowed to let you write "void main()", but they can do that anyway as an compiler extension. If you want to write "void main()", and limit yourself to the compilers that happens to let you do it as an extension to what's guarantee to be safe by the standard, feel free.
 
  • #15


thanks
 

Similar threads

  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 23 ·
Replies
23
Views
2K
Replies
20
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
5
Views
2K
  • · Replies 36 ·
2
Replies
36
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 39 ·
2
Replies
39
Views
5K