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.
Related
This question already has answers here:
Why does the main function work with no return value?
(2 answers)
What should main() return in C and C++?
(19 answers)
Closed 2 years ago.
Read all this to understand my problem: In my collage test there was a question like this:
What is the error in this code:
#include<stdio.h>
int main(){
printf("Hello World");
}
Answer of above code: compiling Error
But This is what I got when I try it practically:
As per books we have to return 0; when we use int main()
can any one give perfect or understandable Explanation.
Absence of return statement will never raise compiling error. Especially for main function, that handles differently. If it has type int but have doesn't return anything, a program acts like main returns 0.
Theoretically, it can be compiler-dependent, but in you case, it's likely an error in test.
You should return 0 but not doing that is not a compiling error.
UPD: as #EricPostpischil mentioned in comments, compilers don't raise an error because they have a mode set by default that allows to compile non-standard code, so question in your test is incomplete. It has to specify compiler you are using etc.
This question already has answers here:
How to replace C standard library function ?
(3 answers)
Closed 2 years ago.
By experimenting with this simple code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double sin (double i){
printf("Yes!\n");
return 0.;
}
int main() {
printf("Do I run my version of sin()?: %lf\n", sin(1));
return 0;
}
I expected to find the function sin "shadowed" by my version, as for instance described in this thread (Overriding C library functions, calling original).
Surprisingly (to my viewpoint), when I compiled, gcc example.c, (same effects with gcc example.c -lm) I just obtained the mathematical value of sin. May I ask the reason of that? What should I change to use my definition?
This question aims to a better understanding of the language; I am not trying to solve any specific practical problem.
sin is a C standard library function that compilers may substitute with a built-in function.
With gcc it is __builtin_sin. Compile with -fno-builtin-sin to be able to provide your own definition of sin.
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.