2

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.

  • 1
    Does this answer your question? [How to replace C standard library function ?](https://stackoverflow.com/questions/9107259/how-to-replace-c-standard-library-function) Your program will do as you expect if compiled with the `-fno-builtin` flag. – chash Jun 16 '20 at 16:46

1 Answers1

1

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.

Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271