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

  • Thread starter Thread starter transgalactic
  • Start date Start date
  • Tags Tags
    Bug
AI Thread Summary
The discussion centers around the error message indicating that the 'main' function must return 'int' in C programming. It clarifies that the C standard requires the main function to return an integer, typically 0 for successful termination, and that using 'void main()' is not compliant with C99 and later standards. Some compilers may allow 'void main()' as an extension, leading to confusion among programmers. The conversation also highlights differences between C and C++ regarding the main function's return type, emphasizing that C++ strictly requires 'int main()'. Ultimately, adhering to the C standard by defining 'int main()' and including a return statement is recommended for compatibility and correctness.
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

Back
Top