Friday 6 April 2012

C Programming - Sample - 4 - USE OF MATH FUNCTIONS

USE OF MATH FUNCTIONS :
We often use standard mathematical functions such as cos, sin, exp. etc. We shall see now the use of a mathematical function in a program. The standard mathematical functions are defined and kept as a part of C math library. If we want to use any of these mathematical functions, we must add an #include instruction in the program. Like #define, it is also a compiler directive that instructs the compiler to link the specified mathematical functions from the library. The instruction is of the form

#include <math.h>

math.h is the filename containing the required function. Figure illustrates the use of cosine func¬tion. The program calculates cosine values for angles 0, 10, 20 180 and prints out the results with headings.

Another #include instruction that is often required is

//include <stdio.h>

stdio.h refers to the standard 1/0 header file containing standard input and output functions




As mentioned earlier, C programs are divided into modules or functions. Some functions are written by users like us and many others are stored in the C library. Library functions are grouped category-wise and stored in different files known as header files. If we want to access the functions stored in the library, it is necessary to tell the compiler about the files to be accessed.

This is achieved by using the preprocessor directive #include as follows: 

#include < filename >

filename is the name of the library file that contains the required function defini¬tion. Preprocessor directives are placed at the beginning of a program. '



No comments:

Post a Comment