alvin lee
alvin lee

Reputation: 41

return type of 'main' is not 'int'

I'm learning the C language,has written the following code:

#include <stdio.h>
void main()
{
  char ch='a';
  printf("%c\n", ch);
}

Then I use GCC to compile the code,but i get a mistake:

return type of 'main' is not 'int' [-Wmain-return-type]

I didn't use any data type int,what's wrong?

Upvotes: 1

Views: 20022

Answers (6)

Aleix Marin&#233;
Aleix Marin&#233;

Reputation: 305

Okay, so everyone is speaking about running C programs UNDER an Operating system. This is totally true:

  • From the standard point, since the main should return int.
  • From the operating systems point, since the int returning from the main is mapped to the exit code of the program.

But no one is speaking of other uncommon conditions that of course can happen when implementing a C program: For example, a C binary that is not being run under any operating system, a C binary that does not return nothing from its main because there is no one to receive or interpret the value, a C binary that actually is the Operating System or a C binary that controls an embedded system where its main function is to synchronize its components inside an infinite loop, waiting for petition (and by definition never returning). The previous examples are more or less equivalent.

In those cases, even if the compiler complains it makes sense to define your main like:

void main()
{
...
}

You have to take warnings seriously, but it is more important to understand what are you actually doing. That is the main caveats of C: With great power comes great responsibility.

If you are under those circumstances, the definition of main does not matter very much since there is no one to call it nor no one to receive its exit value.

Anyway, there is no real concern about the main signature in those circumstances, and optimization does not seem like an improved characteristic of those C programs if they are defined as void main() or as int main(int argc, char* argv[]).

So, conclusion:

  • When expecting to be run under an operating system (I think this would be true most of the time): Use the standard definition of main int main(int argc, char* argv[]). The warning of gcc was considered for these circumstances.
  • When you do not have an operating system: Do as you please, it does not matter, except for the fact of having an annoying warning that does not make sense in those exact circumstances.

Upvotes: 1

Iharob Al Asimi
Iharob Al Asimi

Reputation: 53006

As the compiler is telling you, main() must return int

int main(void)
{
    return 0;
}

the return value being the exit code of the program.

Upvotes: 6

X_Trust
X_Trust

Reputation: 817

Get into the habit of using:

int main( int argc, char* argv[] ){

  // your code

  return 0;
}

argc is the number of arguments being passed to the program.

argv is the array of arguments in string from.

Upvotes: 1

ForceBru
ForceBru

Reputation: 44838

In this case you need to use

#include <...>
int main(){
//code
return 0; //or return any_integer;
}

If your program returns nothing the system will never know was the run successful or there were some errors.

Upvotes: 2

ouah
ouah

Reputation: 145829

Use:

int main(void) { /* ... */ }

for the definition of main function with no argument. Note that int main() { /* ... */ } is also valid but the former definition is preferred.

Upvotes: 1

Sunil Bojanapally
Sunil Bojanapally

Reputation: 12658

The error mean, the main() should return int. Make main() as int type and return an valid exit condition integer, usually termination is return 0. Please refer to this previous SO answer

Upvotes: 0

Related Questions