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");
}
Related
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
This question already has answers here:
Is implicit function declaration legal in C89?
(2 answers)
Implicit function declarations in C
(6 answers)
What is the default C -std standard version for the current GCC (especially on Ubuntu)?
(7 answers)
Closed 3 years ago.
I was trying to simultaneously compile the following C files
file1.c
#include<stdio.h>
int main()
{
foo();
return 0;
}
file2.c
#include<stdio.h>
void foo()
{
printf("Hello");
}
I compiled the two files using the following command in linux
gcc file1.c file2.c -o file
It compiled successfully without any warnings and on running it gave the output as 'Hello'
Shouldn't file1.c require a prototype like void foo(). Is there anything in the C standard regarding this ?
Before C99, C had a thing called implicit declaration that allowed you to do that.
If you didn't specify a declaration for foo and you called foo, it was implicitly declared as int foo();.
This, however, was removed from C99 and subsequent standards.
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.
This question already has answers here:
Not including stdlib.h does not produce any compiler error!
(5 answers)
Closed 9 years ago.
I am using the atoi() function which I know is a part of stdlib.h.
Then why is the following code working correctly when I have not included the required header file?
#define _CRT_SECURE_NO_wARNINGS
#include <stdio.h>
int main()
{
char y[10] = "1234";
int z = atoi(y);
printf("%d\n", z);
return 0;
}
If the compiler detects a function in use, not being prototyped, it assumes int as return value. Lucky atoi() returns ìnt, so as the Standard Library is linked by default the symbol atoi() is resolved by the linker successfully.
If you'd had made your compiler log warnings (options -Wall -Wextras -pedantic for gcc) it would have notified you about the missing prototype for atoi().
Btw: It should be
int main(void)
at least.
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.