main() - Function should return a value?? [C]

I'm getting a "Function should return a value" warning in the end of main(). I'm weirded out. Is the main() function supposed to return a value? (like, "return 0;" or just a "return" statement, maybe?) As far as I know, functions (not the void ones) are the ones required to return a value. Correct me if I'm wrong here, and please point out my mistake as well. I remove a couple of lines from the source code below, if you don't mind...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
...
main() {
	int x, opt;
	clrscr();
	makeNull();
	
	while(opt != 4){
		opt = menu();

		clrscr();
		
		switch(opt){
			case 1:
				printf("Insert Data: ");
				scanf("%d", &x);
				insertData(x);
				break;
			case 2:
				printf("Delete Data: ");
				scanf("%d", &x);
				deleteData(x);
				break;
			case 3:
				displayData();
				break;
			case 4:
				printf("Exiting application...");
				getch();
				exit(EXIT_FAILURE);
				break;
			default:
				printf("Invalid option.");
				getch();
				break;
		}
	}
}
...
Last edited on
Your main function doesn't specify a return type.

1
2
3
4
int main( int argc, char* argv[] )
{
   // A C++ main function...
}
Last edited on
Okay, so I added a "return;" statement to the end of my main() and that fixed it. I just wanna clarify if that return statement was a requirement for the termination of the program?
@iHutch105 Sorry, I forgot to mention that I was working on this in C. My bad!
The return can be implicit but the warnings will usually be compiler-dependant.

Though void returning main functions will work, the C++ standard requires that main be an integer returning function (the parameters are optional by the standards but necessary if you ask me).

EDIT: Wrote this as you wrote your response. I'm not clued up on the C standard, I'm afraid.

EDIT 2: The C standard is the same as the C++. See section 5.1.2.2.1
http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1570.pdf
Last edited on
As per suggestion, I added a return type to main and change my return statement to return 0. Thanks!

1
2
3
4
int main() {
	...
	return 0;
}
Topic archived. No new replies allowed.