Two declarations of the same function in c

I had this question in the test, and I still do not understand the answer that gave me:

Say I wrote the following code:

#include <math.h> #include <stdio.h> float cos(float x){ return 1-x*x/4; } int main() { printf("%0f",cos(0.05f)+sin(0.05f)); } 

Suppose cos and sin declared and defined in a math library (getting and returning double ), and I'm trying to link my code to a math library.

Another assumption is that cos is defined in math.c

The question was:

"Will the code compilation / link succeed? If so, which cos function will be called?"

The answer was:

"Yes, the code will compile and my call will be called."

How can this behavior be explained? Are these not numerous definitions of the same function?

+6
source share
6 answers

Perhaps your teacher made a mistake and intended to use double cos(double x) . In this case, many C implementations will accept the program, and it will communicate and run, because the linker accepts each module from the supplied object modules, but only accepts the necessary modules from the libraries into which it is supplied. Thus, since cos already defined in the program, the linker does not take it from the math library. However, although this works in many implementations of C, it violates the rules of the C standard, which preserve library identifiers; normal programs may not define them.

Another possibility is that your teacher was not going to include math.h This would make the cos declaration not a mistake, since it would not contradict another declaration, but it would mean that the sin program must also be declared by the program, since it is used.

+7
source

This will not work.

You will see an error similar to this:

 conflicting types for 'cos' 

I got it with code blocks with a gcc compiler ...

There is another solution that you can use, please check this post: Override function call in C

+2
source

It will not compile.

I added return 0; at the end of main() to remove the second problem with -Wall -Werror . If you do this, you will see:

 $ gcc -Wall -Werror costest1.c -o costest -lm costest1.c:5:1: error: conflicting types for 'cos' 

This is not done at compile time because math.h also defines a function called cos . Please note that the prototype for cos :

 double cos(double x); 

not

 float cos(float x); 

If you did not include math.h , you could compile but get:

 $ gcc -Wall -Werror costest1.c -o costest -lm costest1.c:5:1: error: conflicting types for built-in function 'cos' [-Werror] costest1.c: In function 'main': costest1.c:13:3: error: implicit declaration of function 'sin' [-Werror=implicit-function-declaration] costest1.c:13:32: error: incompatible implicit declaration of built-in function 'sin' [-Werror] cc1: all warnings being treated as errors 

This is because cos not a normal function, but is treated as builtin . As you can see, this is defined in terms of sin . If cos was a normal function, you might see a duplicate character error.

In C, you cannot have two functions with the same name, even if they have different arguments. In C ++, you can, in that the same named methods can differ in calling parameters (but not only with return type).

+2
source

EDIT:

I assume that the question was about C ++, not C, since compiling your code as a C program will lead to conflicting error types: https://eval.in/93380 .

This behavior is caused by function overloading . The cos() function has the assembly name _cos@double , and reusing the cos() function to accept the float argument will have the assembly name _cos@float and will not conflict with cos() defined in the math library, And calling cos() with the argument float will be translated to a call to the assembly function _cos@float , which is your own cos() .

Note that function overloading is allowed only in C ++, not C.

In C, you can only do this without changing the arguments and return types of the function ( https://eval.in/93381 ), otherwise the previous error will be generated.

As follows from the assumption (cos is defined in math.c), the function cos() in this case is different from that defined in math.h , in which case the answer would be true if it were defined as taking an argument of type float and return a value of type float . In this case, the program will compile without any problems.

+2
source

Online Standard C2011 :

6.7 Announcements
...
<B> Limitations
...
4 All declarations of the same scope that relate to the same object or function must indicate compatible types

The code you set violates the above limitation; math.h declares cos as

 double cos(double x); 

This behavior cannot be explained as C; it can be explained as C ++, which allows you to rename the name.

Make sure that you are really talking about C, not C ++, otherwise you will find yourself in a very confused state.

+2
source

No, it does not compile.

The math.h header file declares double cos(double) . Attempting to reload is not allowed in C.

 error C2371: 'cos' : redefinition; different basic types 
0
source

All Articles