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

  • Thread starter transgalactic
  • Start date
  • Tags
    Bug
In summary, the function main must return an int, but main is a void function, so it can't return int.
  • #1
transgalactic
1,395
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
  • #2


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


i thought by definition mains had to return something.

Don't you need to declare those functions too?
 
  • #4


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:
  • #5


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.
 
  • #6


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


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;
 
  • #9


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
 
  • #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
 

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

1. How do I fix the 'main' must return 'int' error in C programming?

To fix this error, you need to make sure that your main function has a return type of 'int'. In C programming, the main function must always return an integer value to the operating system. If your main function does not have a return type or has a different return type, you will get this error.

2. Why am I getting the 'main' must return 'int' error?

This error occurs because the main function in C programming is required to return an integer value. This value is used to indicate the status of the program to the operating system. If the main function does not have a return type or has a different return type, the compiler will throw this error.

3. Can I have a main function that does not return an integer value?

No, the main function in C programming must always return an integer value. This is a requirement of the C programming language and is necessary for the operating system to know the status of the program. If you want to use a different return type, you can use the exit() function within the main function to return an integer value.

4. How do I use the exit() function to fix the 'main' must return 'int' error?

The exit() function can be used within the main function to return an integer value. This value will be used to indicate the status of the program to the operating system. To use the exit() function, you need to include the header file in your program and call the function with the desired integer value as its argument.

5. Is the 'main' must return 'int' error specific to C programming?

Yes, this error is specific to C programming. Other programming languages may have different requirements for the main function, such as not requiring a return type or allowing different return types. It is important to understand the specific rules and requirements of the programming language you are using.

Similar threads

  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
1
Views
928
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
4
Views
782
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
5
Views
955
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
13
Views
1K
Back
Top