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 |
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.
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
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 |
Write a program to illustrate how the two strings “MY POWERFUL” and “COMPUTER” can be joined into a new string using String library functions.
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
4.3.7 Miscellaneous Library
The functions in the Miscellaneous library include routines to convert data from one type to another type, as well as to perform some trigonometric functions. Table 4.10 lists the commonly used functions in this library.
Table 4.10: Commonly used Miscellaneous library functions
| Function | Description |
|---|---|
| ByteToStr | Convert a byte into string |
| ShortToStr | Convert a short into string |
| WordToStr | Convert an unsigned word into string |
| IntToStr | Convert an integer into string |
| LongToStr | Convert a long into string |
| FloatToStr | Convert a float into string |
| Bcd2Dec | Convert a BCD number into decimal |
| Dec2Bcd | Convert a decimal number into BCD |