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.
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:
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:
C function with no parameters behavior
(2 answers)
Closed 8 years ago.
Today I have spend considerable time finding a "bug" that can be demonstrated by this simple code:
main.c
#include "func.h"
#include <stdio.h>
void main(){printf("func: %f", getX());}
func.c
#include "func.h"
static float x[2] = {1.0f, 2.0f};
float getX(int n){return x[n];}
func.h
float getX();
and it compiles & links (VS2010 with /W3) without any warning.
Sample output from the run is
func: 1.000000
Can someone explain me how it can work if function declaration and definition do not match and why it is not worth to output any warning?
Thanks
In C:
float getX();
is not the same as:
float getX(void);
In the first case you are specifying a function that returns float but takes unspecified parameters - in the second case you are specifying a function which returns float but which takes no parameters.
So a definition of the form float getX(int n) is compatible with the first case, but not the second, which explains why you do not seen an error/warning.
If you change to the correct prototype (the second version) then you should see the required error/warning.
Note that this is different behaviour than for C++, where the two forms are equivalent.
As for the output you are seeing when you run the program, this is just a random consequence of undefined behaviour - literally anything could happen if you call a function with incorrect parameters.
Take home message: in C you should always use the second form when declaring a prototype that takes no parameters - unfortunately you'll find that it's very common for people to omit the void (probably because of bad habits acquired from writing C++ code, or just because it requires fewer keystrokes).
This declaration means that getX accepts any number of parameters:
float getX();
To declare a function that takes no parameters, use:
float getX(void); // This will result in a compiler error for func.c
This question already has answers here:
Weird compilation error in Visual Studio 2008
(7 answers)
Closed 8 years ago.
I have a simple c program (It's a base program without any GUI) ,
on linux this code compiles and runs without any issue with gcc compiler, now I have to compile the same code on windows with Visual Studio 2013, with both visual c and intel compilers, I have selected new console application program following this guide
but get tons of these error messages
error : declaration may not appear after executable statement in block myfile.c
at all the line with variables declarations, (also on a simple int i; )
the only libraries that I use are these
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <xmmintrin.h>
#include <math.h>
#include <string.h>
Any suggestion?
It seems that your program has Mixed Declarations and Code. VS doesn't support mixed type declaration (or you can say it doesn't support C99 completely).
In mixed declarations and code, for example, you could do:
int i;
...
i++;
int j = i + 2;
Each identifier is visible from where it is declared until the end of the enclosing block.
You need to move all the declaration to the beginning just like in C89. And you should note that VS is not a C compiler.
The problem is with code such as this:
void foo()
{
bar(); /* statement */
int i; /* variable declaration */
}
That is not C90 valid, because there declarations must be at the beginning of the block, just after the opening brace ({).
In C99 and later it is correct, however, so GCC accepts it, (C90 is 15 years old).
Unfortunately MS-VC does not, and will not, support C99, so your code will not compile there.
You can fix it manually, by moving the declaration to the top of the block (1), add a lot of braces (2), try to compile it as C++, of change the compiler... that's up to you!
Example 1:
void foo()
{
int i;
bar();
/* beware! if i was initialized, add the initialization here, not there */
}
Example 2 (hidden braces):
void foo()
{
bar();
{int i;
}} /*all the closing braces should be at the end of the function */
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.