2

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.

Michael Sazonov
  • 1,531
  • 11
  • 21
  • 1
    First googled link: http://objectmix.com/c/32997-whats-use-__cdecl.html and your answer: `_CRTIMP is simply a macro, which probably expands to some non-standard calling convention specification or to nothing, depending. "CRT" probably means "C Runtime Library". "IMP" probably means "import". __cdecl is a calling convention specifier supported by Visual C++ and some other Windows C++ compilers.` – Vyktor Jan 29 '12 at 00:16
  • I suppose, I wasn't clear enough. Where does `cos( double )` exist? Where is the function itself? By the way, I'd visited that link ) – Michael Sazonov Jan 29 '12 at 00:18
  • 1
    @MichaelSazonov the headers only tell you which functions and data are available; the definitions themselves are elsewhere (the C standard library (msvcrt32.dll or something like that) in this instance) – Dave Jan 29 '12 at 00:27
  • 2
    Looking at headers is a painful process. They are often laden with macros and `#ifdef`'s in order to make the same header perform differently under different circumstances (o/s versions, standard versions, compilation options) without having to write everything out for each variation of the requirements. – Jonathan Leffler Jan 29 '12 at 00:29

3 Answers3

5

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.

Bernd Jendrissek
  • 1,088
  • 6
  • 15
3

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

John Carter
  • 53,924
  • 26
  • 111
  • 144
2

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.)

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631