-1

I'm new to programming and was using Visual Studio Code in C language to try out coding. I started with the Hello World function, but, even though the output was correct (the output was "Hello World!"), the following warning appeared. I don't know what it means: [Running] cd "c:\Users\lucas\OneDrive\Ambiente de Trabalho\" && gcc test.c -o test && "c:\Users\lucas\OneDrive\Ambiente de Trabalho\"test test.c:3:1: warning: return type defaults to 'int' [-Wimplicit-int] 3 | main(){ | ^~~~ Hello World [Done] exited with code=0 in 0.71 seconds

I used the code below to get the Hello World output:

#include<stdio.h>
main (){
    printf("Hello World!");
}

What should I do to fix this warning? Thanks for the help!

Maoaii
  • 17
  • 4

3 Answers3

1

All functions must specify a return type. Older version of C would allow the return type to be omitted, in which case the return type defaults to int.

The main function is particular must return int, so explicitly state as much. Also, you need to return a value, and by convension main should return 0 if the program was successful.

#include<stdio.h>

int main ()
{
    printf("Hello World!");
    return 0;
}
dbush
  • 205,898
  • 23
  • 218
  • 273
  • Thanks for the answer! I've tried that same exact code, including the "int" before main, but it still outputs the same warning. Edit: I tried: int main(void) and it worked fine, without any warning. Why would that happen? – Maoaii May 08 '20 at 21:22
1

You need to explicitly give main a return type:

int main( void ) 
{
  printf ("Hello World!");
}

The standard signatures for main are

int main( void )

or

int main( int argc, char **argv ) // you can use different names for argc and argv

That is, either takes 0 or 2 parameters and returns an int.

In the early days of C, if you didn't provide a return type for a function, the compiler assumed it returned int. Such implicit typing is no longer allowed, hence the warning.

John Bode
  • 119,563
  • 19
  • 122
  • 198
  • Do you need `(void)` ? – Ed Heal May 08 '20 at 20:17
  • If you're going to use prototype syntax *anywhere*, use it *everywhere*. An empty parameter list in a function *definition* will be interpreted as "takes 0 parameters", but an empty parameter list in a *declaration* will be interpreted as "takes an unspecified number of parameters". I'd rather be consistent and use the same form for both declarations and definitions. – John Bode May 08 '20 at 20:51
  • Thanks for the answer! I tried your first recommendation and it worket out great, without the warning! – Maoaii May 08 '20 at 21:25
1

Every function in C needs a return value type. With the main function, it can either be of type void or int. void means that the main function doesn't return a value. int means that the main function returns an integer. It looks like the compiler automatically filled in that part of your code with int, and it's just letting you know that you should actually be putting some return type.

So you can either do this:

#include<stdio.h>
int main (){
    printf("Hello World!");
    return 0;
}

Or this

#include<stdio.h>
void main (){
    printf("Hello World!");
}