Functions and Prototypes II

The Standard Library Functions

There are a great many standard functions that are included with C compilers and while these are not really part of the language, in the sense that you can re-write them if you really want to, most C programmers think of them as fixtures and fittings. List of the most common libraries and a brief description of the most useful functions they contain follows:

stdio.h: I/O functions:
getchar() returns the next character typed on the keyboard.
putchar() outputs a single character to the screen.
printf() as previously described
scanf() as previously described
string.h: String functions
strcat() concatenates a copy of str2 to str1
strcmp() compares two strings
strcpy() copys contents of str2 to str1
ctype.h: Character functions
isdigit() returns non-0 if arg is digit 0 to 9
isalpha() returns non-0 if arg is a letter of the alphabet
isalnum() returns non-0 if arg is a letter or digit
islower() returns non-0 if arg is lowercase letter
isupper() returns non-0 if arg is uppercase letter
math.h: Mathematics functions
acos() returns arc cosine of arg
asin() returns arc sine of arg
atan() returns arc tangent of arg
cos() returns cosine of arg
exp() returns natural logarithim e
fabs() returns absolute value of num
sqrt() returns square root of num
time.h: Time and Date functions
time() returns current calender time of system
difftime() returns difference in secs between two times
clock() returns number of system clock cycles since program execution
stdlib.h:Miscellaneous functions
malloc() provides dynamic memory allocation, covered in future sections
rand() as already described previously
srand() used to set the starting point for rand()
Throwing The Dice

How to use functions, we conclude this section with a program that, while it isn't state of the art, does show that there are things you can already do with C. It also has to be said that some parts of the program can be written more neatly with just a little more C. All the program does is to generate a random number in the range 1 to 6 and displays a dice face with the appropriate pattern. The main program isn't difficult to write because we are going to adopt the traditional programmer's trick of assuming that any function needed already exists. This approach is called stepwise refinement, and although its value as a programming method isn't clear cut, it still isn't a bad way of organising things:

main()
{
int r;
char ans;
ans = getans();
while(ans== 'y')
{
r = randn(6);
blines(25);
if (r==1) showone();
if (r==2) showtwo();
if (r==3) showthree();
if (r==4) showfour();
if (r==5) showfive();
if (r==6) showsix();
blines(21);
ans = getans();
}
blines(2);
}

Let's start with randn().

This is obviously an int function and it can make use of the existing rand() function in the standard library

int randn(int n)
{
return rand()%n + 1;
}

The single line of the body of the function just returns the remainder of the random number after dividing by n - % is the remainder operator - plus 1. An alternative would be to use a temporary variable to store the result and then return this value. You can also use functions within the body of other functions.

Next getans()
char getans()
{
int ans;
printf("Throw y/n ?");
ans = -1;
while (ans == -1)
{
ans=getchar();
}
return ans;
}

The blines(n) function simply has to use a for loop to print the specified number of lines:

void blines(int n)
{
int i;
for(i=1 ; i<=n ; i++) printf("\n");
}

Each function prints exactly three lines of dots and uses blank lines if necessary. The reason for this is that printing 25 blank lines should clear a standard text screen and after printing three lines printing 21 blank lines will scroll the pattern to the top of the screen.

void showone()
{
printf("\n * \n");
}
void showtwo()
{
printf(" * \n\n");
printf(" * \n");
}
void showthree()
{
printf(" * \n");
printf(" * \n");
printf(" *\n");
}
void showfour()
{
printf(" * * \n\n");
printf(" * * \n");
}
void showfive()
{
printf(" * * \n");
printf(" * \n");
printf(" * * \n"); }
void showsix()
{
int i;
for(i=1 ; i>=3 ; i++) printf(" * * \n");
}

The only excitement in all of this is the use of a for loop in showsix! Type this all in and add:

void showone();
void showtwo();
void showthree();
void showfour();
void showfive(); int randn();
char getans();
void blines();

before the main function if you type the other functions in after.