Question

I'm new to C. I was traveling through math.h, looking for its mathematical algorithms, but encountered only this kind of lines:

_CRTIMP double __cdecl sin (double);
_CRTIMP double __cdecl cos (double);
...

Now, I couldn't find the algorithm itself. I know _CRTIMP is a kind of run-time library C uses, but I just can't figure out what the whole line means. Could you, please, explain?

Besides, I would like to know where these functions are defined at.

Was it helpful?

Solution

C headers typically contain only function prototype declarations, not definitions. Function prototypes specify what is called the "function signature": return value, arguments, and sometimes calling convention (when & where compilers support this). The function definitions are in a separate source file, that gets compiled separately from your own (including any headers your source file #include's). Definitions of library functions might be in C, they might also be in assembly, but that shouldn't matter to your code (only to your curiosity). But you probably don't compile those yourself anyway; instead, your development environment / operating system comes with a standard library (a binary object file) that contains many already-compiled functions. Your development environment simply links your code to that library.

OTHER TIPS

C header files will only contain the declaration of functions, not their definitions.

You're looking for the source code of the functions declared in math.h, here's one implementation of sin: http://fxr.watson.org/fxr/source//arch/i386/math-emu/poly_sin.c?v=linux-2.4.22

The C header files that are included with your OS, compiler, or C runtime library are not really intended for human consumption. You can certainly read them, and you can learn quite a bit by trying to understand them, but they're primarily intended for use by the compiler. As you've seen in these examples, they tend to depend on a lot of compiler-specific features (a habit you should try to avoid in your own code).

They also tend to have a lot of #ifdefs, so the same headers can be used with different systems.

If you just want to know how to use the sin function, for example, you're better off reading your system's documentation. On my Ubuntu system, for example, man sin shows this (among other things):

SYNOPSIS

   #include <math.h>

   double sin(double x);
   float sinf(float x);
   long double sinl(long double x);

   Link with -lm.

The _CRTIMP and __cdecl are probably important to the compiler, but as a programmer you can safely ignore them.

If you're looking for the source code that implements the sin function, that may or may not be available. It might be written in a language other than C; there have even been systems where it's implemented in hardware (though a small wrapper would still be required).

Another answer provides a link to one implementation, but that's probably not the one used on your system.

And you don't need to get too bogged down in how the sin function is implemented. It's certainly a good thing to know, but you don't need that information to write code that uses it. (I absolutely do not want to discourage curiosity.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top