## 4.3.6 ANSI C Library

4.3.6 ANSI C Library

The ANSI C library consists of the following libraries (further details on these libraries are available in the mikroC user manual):

• Ctype library

• Math library

• Stdlib library

• String library

### Ctype Library

The functions in the Ctype library are mainly used for testing or data conversion. Table 4.6 lists the commonly used functions in this library.

Table 4.6: Commonly used Ctype library functions

Function Description
isalnum Returns 1 if the specified character is alphanumeric (a–z, A–Z, 0–9)
isalpha Returns 1 if the specified character is alphabetic (a–z, A–Z)
iscntrl Returns 1 if the specified character is a control character (decimal 0–31 and 127)
isdigit Returns 1 if the specified character is a digit (0–9)
islower Returns 1 if the specified character is lowercase
isprint Returns 1 if the specified character is printable (decimal 32–126)
isupper Returns 1 if the specified character is uppercase
toupper Convert a character to uppercase
tolower Convert a character to lowercase

### Math Library

The functions in the Math library are used for floating point mathematical operations. Table 4.7 lists the commonly used functions in this library.

Table 4.7: Commonly used Math library functions

Function Description
acos Returns in radians the arc cosine of its parameter
asin Returns in radians the arc sine of its parameter
atan Returns in radians the arc tangent of its parameter
atan2 Returns in radians the arc tangent of its parameter where the signs of both parameters are used to determine the quadrant of the result
cos Returns the cosine of its parameter in radians
cosh Returns the hyperbolic cosine of its parameter
exp Returns the exponential of its parameter
fabs Returns the absolute value of its parameter
log Returns the natural logarithm of its parameter
log10 Returns the logarithm to base 10 of its parameter
pow Returns the power of a number
sin Returns the sine of its parameter in radians
sinh Returns the hyperbolic sine of its parameter
sqrt Returns the square root of its parameter
tan Returns the tangent of its parameter in radians
tanh Returns the hyperbolic sine of its parameter

### Stdlib Library

The Stdlib library contains standard library functions. Table 4.8 lists the commonly used functions in this library.

Table 4.8: Commonly used Stdlib library functions

Function Description
abs Returns the absolute value
atof Converts ASCII character into floating point number
atoi Converts ASCII character into integer number
atol Converts ASCII character into long integer
max Returns the greater of two integers
min Returns the lesser of two integers
rand Returns a random number between 0 and 32767; function srand must be called to obtain a different sequence of numbers
srand Generates a seed for function rand so a new sequence of numbers is generated
xtoi Convert input string consisting of hexadecimal digits into integer

### Example 4.16

Write a program to calculate the trigonometric sine of the angles from 0° to 90° in steps of 1° and store the result in an array called Trig_Sine.

### Solution 4.16

The required program listing is shown in Figure 4.27 (program SINE.C). A loop is created using a for statement, and inside this loop the sine of the angles are calculated and stored in array Trig_Sine. Note that the angles must be converted into radians before they are used in function sin.

```/******************************************************************            TRIGONOMETRIC SINE OF ANGLES 0 to 90 DEGREES             ========================================== This program calculates the trigonometric sine of angles from 0 degrees to 90 degrees in steps of 1 degree. The results are stored in an array called Trig_Sine. Programmer: Dogan Ibrahim File:       SINE.C Date:       May, 2007 ********************************************************************/ void main() {  unsigned char j;  double PI = 3.14159, rads;  for(j = 0; j <= 90; j++) {   rads = j * PI /180.0;   angle = sin(rad);   Trig_Sine[j] = angle;  } }```

Figure 4.27: Calculating the sine of angles 0 to 90

### String Library

The functions in the String library are used to perform string and memory manipulation operations. Table 4.9 lists the commonly used functions in this library.

Table 4.9: Commonly used String library functions

Function Description
strcat, strncat Append two strings
strchr, strpbrk Locate the first occurrence of a character in a string
strcmp, strncmp Compare two strings
strcpy, strncpy Copy one string into another one
strlen Return the length of a string

### Example 4.17

Write a program to illustrate how the two strings “MY POWERFUL” and “COMPUTER” can be joined into a new string using String library functions.

### Solution 4.17

The required program listing is shown in Figure 4.28 (program JOIN.C). The mikroC String library function strcat is used to join the two strings pointed to by p1 and p2 into a new string stored in a character array called New_String.

```/******************************************************************                        JOINING TWO STRINGS                        =================== This program shows how two strings can be joined to obtain a new string. mikroC library function strcat is used to join the two strings pointed to by p1 and p2 into a new string stored in character array New_String. Programmer: Dogan Ibrahim File:       JOIN.C Date:       May, 2007 *******************************************************************/ void main() {  const char *p1 = "MY POWERFUL ";    // First string  const char *p2 = "COMPUTER";        // Second string  char New_String[80];  strcat(strcat(New_String, p1), p2); // join the two strings }```

Figure 4.28: Joining two strings using function strcat

Оглавление книги

Генерация: 0.053. Запросов К БД/Cache: 0 / 0
поделиться
Вверх Вниз