What does this GCC warning mean? [duplicate] - c

This question already has answers here:
warning: return type defaults to ‘int’ [-Wreturn-type]
(4 answers)
Closed 3 years ago.
Here's the code, straight from 'The C programming Language, Second edition'.
#include <stdio.h>
main ()
{
printf("hello, world\n");
}
Here's the GCC error:
user#root:~/bin$ gcc helloworld.c
helloworld.c:2:1: warning: return type defaults to ‘int’ [-Wimplicit- int]
main ()^~~~
edit: I have just realized that the code did, in fact, compile. I just didn't realize it had overwritten the file for another program I compiled. I've edited the question itself, as I think that has more value. (Originally I asked why this program was not compiling)

You haven't included a return type for main. You probably want to return 0 at the end of it, as that is the standard exit code for no error. Revised your code would be
#include <stdio.h>
int main() {
printf("hello, world\n");
return 0;
}

Try to:
Use int main() since int is return type of main.
EDIT:
In C89, the default return type is int. This default was removed in
C99 and compilers are helpful reminding you that your C-style with no
int before main() is out of date.

That is not an error, it's just a warning. Your code does compile, trying running the resulting binary with ./a.out
GCC is just warning you that you didn't specify a return type for main, and that it will automatically return an integer.

Your main signauture is wrong. You should consider reading this answer to get a C11 standardized signature of the main function :
https://stackoverflow.com/a/2108208/8141369

Related

Why still compile successfully and got a.out after warning: implicit declaration of function? [duplicate]

This question already has answers here:
Implicit function declarations and linkage
(4 answers)
Closed 2 years ago.
Is it necessary to declare a variable before using it if the compilation works anyway ?
/* hello-world.c */
#include <stdio.h>
int main(void) {
printf("Hello World!\n");
printf("1 + 2 is: %d\n", sum(1, 2));
return 0;
}
/* sum.c */
int sum(int a, int b) {
return a + b;
}
I compile these code with gcc hello-world.c sum.c and clang hello-world.c sum.c,
both got a warning: implicit declaration of function 'sum' but compiled the a.out.
Is there any case proving it's absolutely necessary to declare before using in C?
(edit: Here, i mean the function prototype, if there is any confusion)
"proving" with C is done by the C Standard, not by observation of compilers. Because the standard leaves a lot of things up to compiler discretion .
This program was valid in C89 but is not valid in C99 and later (in which undeclared identifiers cannot be used in expressions).
What the compiler chooses to do with non-compliant programs is up to that compiler, except that some violations (including this one) must result in a diagnostic being printed. Perhaps your compiler opts to print the diagnostic and then go on to behave like C89 .
The classical case is incompatible implicit declaration:
// a.c
int f()
{
return g();
}
// b.c
float g() { return 3.14; }
In this case, it is undefined behavior.

gcc compiles with no error with no return type (even using -Wall) [duplicate]

This question already has answers here:
What should main() return in C and C++?
(19 answers)
Closed 6 years ago.
I'm wondering why gcc doesn't display an error while compiling, even while using the -Wall option. Also tried the -std=c89 option.
Here's the code:
#include <stdio.h>
int main(void)
{
printf("hello, world\n");
}
Sorry if this post is a duplicate but couldn't find a case where this option is being used.
Thanks!
In C++ and C99 and later, hitting the end of main() without returning a value is equivalent to returning 0. In earlier versions of the C standard it's undefined behavior which the compiler isn't obliged to issue a diagnostic for, although in practice GCC's likely doing an implicit return 0; just as it would for C++ or C99 onwards.
See What should main() return in C and C++? for a lot more detail.

K&R C book is giving me compile issues [duplicate]

This question already has answers here:
C function calls: Understanding the "implicit int" rule
(3 answers)
Closed 6 years ago.
I recently picked up K&R the second edition to learn C. However on the first program, a simple hello world. It is giving me this compile issue
hello.c:3:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
main()
^
1 warning generated.
My code is
#include <stdio.h>
main()
{
printf("Hello world\n");
}
and I'm compiling it by going into the folder where this program is located and using gcc hello.c -o hello
Ah yes, this is old. Declaring functions or variables without their types is going away.
#include <stdio.h>
int main()
{
printf("Hello world\n");
}

C Language function called with few arguments, but not error? [duplicate]

This question already has answers here:
Why does a function with no parameters (compared to the actual function definition) compile?
(11 answers)
Closed 8 years ago.
In the following C code, function printr called only with one parameter, But all compiled with no warning on GCC and VS.
I am confused why this is OK? Thanks!
#include <stdlib.h>
#include <stdio.h>
int printr(i, j){
printf("The first parameter %d\n", i);
printf("The second parameter %d\n", j);
return 0;
}
int main(){
printr(3);
return 0;
}
Gcc does warn you:
$ gcc -Wextra y.c
y.c: In function ‘printr’:
y.c:4: warning: type of ‘i’ defaults to ‘int’
y.c:4: warning: type of ‘j’ defaults to ‘int’
And once you've fixed those it will warn
y.c: In function ‘main’:
y.c:11: error: too few arguments to function ‘printr’
You defined printr() by using old-fashion function definition syntax, therefore compiler cannot do some syntactic check. You should define it like this:
int printr(int i, int j) {
By the way, with -Wextra, gcc will give warnings about your definition.

Hello.c: In function ‘main’: Hello.c:13: warning: return type of ‘main’ is not ‘int’? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What should main() return in C/C++?
Just started coding C about an hour ago, after a few months of basic java coding, and am encountering a problem compiling the basic hello world program.
Here is my code:
#include < stdio.h>
void main()
{
printf("\nHello World\n");
}
and this is what i get back when i try to compile:
Hello.c: In function ‘main’:
Hello.c:13: warning: return type of ‘main’ is not ‘int’
any help would be much apprecated, thanks!
The standard signatures for main are either
int main(void)
or
int main(int argc, char **argv)
Your compiler is simply enforcing the standard.
Note that an implementation may support void main(), but it must be explicitly documented, otherwise the behavior is undefined. Like dandan78 says, a large number of books and online references get this wrong.
it should be
int main() {}
then you should return 0 if the program is terminating correctly or any other number if there was an error. That's an Unix convention, so scripts can check if the program was terminated correctly or an error occurred.
main-function in c has to return an int:
#include < stdio.h>
int main()
{
printf("\nHello World\n");
return 0;
}
Regardless of which prototype you choose for main(), it's return value cannot be void. It has to be int. Many books and tutorials get this wrong and some compilers tend to complain while others do not.

Resources