lib - Libraries and Interfaces c lient.c An interface is...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Libraries and Interfaces An interface is the boundary between the implementation of a library and programs that use that library (i.e. its clients ) . The purpose of an interface is to provide each client with the information it needs to use the library without revealing the details required by the implementation. In C, an interface is represented by a header file , which traditionally has the same name as the file that implements it with the .c extension replaced by .h . For example, you created a collection of functions that you want to make available to clients as a library. You need to create two files: - an interface ( mylib.h ) § contains only the functions prototypes - corresponding implementation ( mylib.c ) Putting the prototypes in the interface makes them available to clients and is called exporting those functions. Interfaces can export : - function prototypes - data types - constants In computer science the term package is used to describe the software that defines a library. That is: - a .h file, and - corresponding .c file Any source file that makes use of the definitions exported by the library The interface contains only the information about the library that its clients need to know. The implementation contains the code to make the library work, the details of which are not of interest to clients. client.c mylib.h mylib.h
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Example: Random Numbers In order to generate a random number in your program, you must use the standard library functions: rand(), srand() and time(). You must include the following libraries in your program: #include <stdio.h> //for standard input output #include <stdlib.h> //for using the random functions #include <time.h> // for using the time function The following program illustrates the use of these functions: int main() { int x, y, z, i; int loop; srand(time(NULL)); /* this basically turns on the random generator */ printf("Please Enter How many Random Numbers you would” “ like to generate\n"); scanf("%d", &loop); for(i = 1; i<=loop; i++){ // rand is a function that will return an integer. x = rand(); //will find random number btw 0 and 32767 y = rand()%100; //will find random number btw 0 and 99 z = ((rand()%100)-50); /* will find random number btw –50 and 49 */ printf(" %d %d %d\n", x, y, z); } return 0; } A user defined library The random.h interface /* * File: random.h * -------------- * This interface provides several functions for * generating random numbers. */ #ifndef _random_h
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 06/12/2011.

Page1 / 9

lib - Libraries and Interfaces c lient.c An interface is...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online