C Standard Libraries and Fortran Intrinsic Functions

All programming languages provide access to a large collection of subprograms for common tasks such as computing square roots and other mathematical functions, performing input and output, sorting lists, etc. The only difference is in how this functionality is provided. An intrinsic, or built-in function is a function that is recognized by the compiler. Functions can also be provided by libraries that are separate from the compiler.

C

Unlike many languages, C has no intrinsic functions. All functions in C are provided by libraries, separate from the compiler. The interface (required arguments and return values) of common functions such as printf(), sin(), cos(), exp(), etc. are specified in header files such as stdio.h and math.h and the functions themselves are provided by precompiled libraries such as libc.so and libm.so.

The function interface comes in the form of a prototype, which simply states the return type and the type of each argument the function requires:

shell-prompt: fgrep 'sin(' /usr/include/math.h 
double  asin(double);
double  sin(double);
            

To use printf() in a C program, we must add the line #include <stdio.h>. To use sin() and other math functions, we must add #include <math.h> and use -lm to link with the standard math library.

To find out which header files and/or libraries are required for a given C library function, and what the interface looks like, simply check the man page:

shell-prompt: man sin
SIN(3)                 FreeBSD Library Functions Manual                 SIN(3)

NAME
     sin, sinf, sinl sine functions

LIBRARY
     Math Library (libm, -lm)

SYNOPSIS
     #include <math.h>

     double
     sin(double x);

     float
     sinf(float x);

     long double
     sinl(long double x);

DESCRIPTION
     The sin(), sinf(), and sinl() functions compute the sine of x (measured
     in radians).  A large magnitude argument may yield a result with little
     or no significance.

RETURN VALUES
     The sin(), sinf(), and sinl() functions return the sine value.

SEE ALSO
     acos(3), asin(3), atan(3), atan2(3), cos(3), cosh(3), csin(3), math(3),
     sinh(3), tan(3), tanh(3)

STANDARDS
     These functions conform to ISO/IEC 9899:1999
            

Note

One of the best ways to become a great C programmer is by browsing the header files. This is how you discover all the cool features that are available to make your life easier. As a new C programmer, there will be things in the header files that you don't understand. Ignore them if you don't have time to explore them right now, and just take in what comes easily for now.

shell-prompt: more /usr/include/math.h

[scroll past stuff that's Greek to you]

#define M_E             2.7182818284590452354   /* e */
#define M_LOG2E         1.4426950408889634074   /* log 2e */
#define M_LOG10E        0.43429448190325182765  /* log 10e */
#define M_LN2           0.69314718055994530942  /* log e2 */
#define M_LN10          2.30258509299404568402  /* log e10 */
#define M_PI            3.14159265358979323846  /* pi */
#define M_PI_2          1.57079632679489661923  /* pi/2 */
#define M_PI_4          0.78539816339744830962  /* pi/4 */
#define M_1_PI          0.31830988618379067154  /* 1/pi */
#define M_2_PI          0.63661977236758134308  /* 2/pi */
#define M_2_SQRTPI      1.12837916709551257390  /* 2/sqrt(pi) */
#define M_SQRT2         1.41421356237309504880  /* sqrt(2) */
#define M_SQRT1_2       0.70710678118654752440  /* 1/sqrt(2) */

#define MAXFLOAT        ((float)3.40282346638528860e+38)

[scroll down more]

int     isinf(double);
int     isnan(double);

double  acos(double);
double  asin(double);
double  atan(double);
double  atan2(double, double);
double  cos(double);
double  sin(double);
double  tan(double);

double  cosh(double);
double  sinh(double);
double  tanh(double);

double  exp(double);
double  frexp(double, int *);   /* fundamentally !__pure2 */
double  ldexp(double, int);
double  log(double);
double  log10(double);
double  modf(double, double *); /* fundamentally !__pure2 */

double  pow(double, double);
double  sqrt(double);

double  ceil(double);
double  fabs(double) __pure2;
double  floor(double);
double  fmod(double, double);

[and much more...]
            

Functions are subprograms which are called by using them within an expression, and for the most part, look like they would in any algebraic expression. The general form is

function-name(argument [, argument ...])
            

An argument is any value that we pass to the function, such as the angle required by the sine, cosine, and tangent functions. The function call itself is an expression that has the value returned by the function. For example, the value of the expression sin(M_PI) in a C program is 0.0, since the sine of any multiple of Pi is 0.

#include <stdio.h>  // For printf()
#include <math.h>   // For sin() and M_PI

int     main()

{
    printf("The sine of %f is %f\n", M_PI, sin(M_PI));
    
    return 0;
}
            

In the code above, M_PI is an argument to the sin() function, and "The sine of %f is %f\n" and sin(M_PI) are arguments to printf().

To compile this code, we can use the following:

cc -O sine-pi.c -o sine-pi -lm
            
Fortran

Fortran provides a wealth of intrinsic mathematical functions for computing trigonometry functions, logarithms, etc. The complete list of functions is too extensive to list here, but it includes all the standard trigonometric functions, square root, etc.

module constants
    double precision, parameter :: PI = 3.1415926535897932d0
end module constants

program sine_pi
    
    print *, 'The sine of ', PI, ' is ', sin(PI), '.'
    
end program
            

Fortran can also utilize addition functions from external libraries, as C does.

Practice

Note

Be sure to thoroughly review the instructions in Section 2, “Practice Problem Instructions” before doing the practice problems below.
  1. What is an intrinsic function?

  2. How many intrinsic (built-in) functions does the C language have? Elaborate.

  3. What is a prototype in C?

  4. Where do we find prototypes for C standard library functions such as sin()?

  5. How would we find out which header files and libraries are required for the sqrt() function?

  6. What is an argument?

  7. What is a return value?

  8. How do we call a function?

  9. How many intrinsic functions for Fortran have?

  10. Is Fortran limited to intrinsic functions only?