Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two declarations of the same function in c

Tags:

c

I had this question in a test, and I still don't understand the answer I was given:

Let's 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));
}

Let's assume cos and sin are declared and defined in the math library (receiving and returning double), and I'm trying to link my code with the math library.

Another assumption is that cos is defined in math.c.

The question was:

"Will the code compile/link successfully? if so, which cos function will be called?"

The answer was:

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

How could this behavior be explained? Aren't these multiple definitions of the same function?

like image 847
Paz Avatar asked Jan 22 '14 14:01

Paz


People also ask

Can a function have multiple declarations?

Yes, it is true. A function can have more than one similar declarations, but exactly one definition for particular declaration.

Can a variable have multiple declarations?

Every declaration should be for a single variable, on its own line, with an explanatory comment about the role of the variable. Declaring multiple variables in a single declaration can cause confusion regarding the types of the variables and their initial values.

What is multiple declaration error in C?

E2238 Multiple declaration for 'identifier' (C++)An identifier was improperly declared more than once. This error might be caused by conflicting declarations, such as: int a; double a; A function declared in two different ways. A label repeated in the same function.

What is multiple declaration?

All function declarations for a particular function must have the same number and type of parameters, and must have the same return type. These return and parameter types are part of the function type, although the default arguments and exception specifications are not.


3 Answers

Your teacher may have made a mistake and intended to use double cos(double x). In this case, many C implementations will accept the program, and it will link and run because the linker takes every module from the object modules it is supplied but only takes the needed modules from the libraries it is supplied. Thus, because cos is already defined in the program, the linker will not take it from the math library. However, although this works in many C implementations, it violates the rules of standard C, which reserves the library identifiers; normal programs may not define them.

Another possibility is that your teacher did not intend to include math.h. This would make the declaration of cos not be an error, since it would not be conflicting with another declaration, but it would mean that sin should also be declared by the program, since it is used.

like image 120
Eric Postpischil Avatar answered Oct 31 '22 01:10

Eric Postpischil


It will not compile.

I added a return 0; at the end of main() to remove a 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 fails at the compile stage because math.h also defines a function called cos. Note the prototype for cos is:

double cos(double x);

not

float cos(float x);

If you did not include math.h, you would be able to compile, but would 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 is not a normal function, but handled as a builtin. As you can see it's defined in terms of sin. If cos were a normal function, you would see a duplicate symbol error of some sort.

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

like image 42
abligh Avatar answered Oct 31 '22 01:10

abligh


Online C2011 standard:

6.7 Declarations
...
Constraints
...
4 All declarations in the same scope that refer to the same object or function shall specify compatible types

The code you posted violates the above constraint; math.h declares cos as

double cos(double x);    

This behavior cannot be explained as C; it can be explained as C++, which allows for name overloading.

Make sure you're really talking about C and not C++, otherwise you are going to wind up being very confused.

like image 35
John Bode Avatar answered Oct 31 '22 03:10

John Bode