OS Libraries
Motivation
- By using libraries, you can achieve
shorter development time and
better reliability, maintainability, portability.
- Before writing a C function,
check for a library function that already accomplishes the task.
Character handling
- Often you need to classify characters as numeric, alphabetic, etc.
This classification depends on details of the underlying
representation of the characters.
For example, in ASCII:
- 'a' is represented as 97
- 'z' is represented as 122
- 'A' is represented as 65
- 'Z' is represented as 90
This representation may change.
- So, writing code that uses the representation directly is
- Hard to read
- Non-portable
- Character handling functions allow you to classify characters
in a readable and portable way.
There are many functions available.
See
/usr/include/ctype.h
for all the function prototypes.
- Example: classifying characters using the ASCII representation directly
#include <stdio.h>
#include <ctype.h>
int main()
{
int c;
c = getchar();
while (c != EOF) {
if (c >= 97 && c <= 122 || c >= 65 && c <= 90)
printf("alphabetic: %c\n",c);
else
printf("not alphabetic: %c\n",c);
c = getchar();
}
return 0;
}
- Example: classifying characters using the ctype.h functions.
Same as above except that the if condition is replaced by
isalpha(c)
.
- Run both examples on a file containing:
String handling
- Programmers must often manipulate strings in a variety of ways.
- Writing correct and efficient string handling routines is
surprizingly difficult.
- As a result, string handling routines have long been provided
in a standard C library.
Dozens of routines are available.
See
/usr/include/string.h
for all the function prototypes.
- Example: copying strings safely
#include <stdio.h>
#include <string.h>
int main()
{
char s0[5],s1[100];
char *small = "abcd";
char *large = "abcdefghijklmnopqrstuvwxyz";
strcpy(s1,large);
strcpy(s0,small);
printf("s0:%s! s1:%s!\n",s0,s1);
strcpy(s0,large);
printf("s0:%s! s1:%s!\n",s0,s1);
strcpy(s1,large);
strncpy(s0,large,4);
s0[4] = '\0';
printf("s0:%s! s1:%s!\n",s0,s1);
return 0;
}
- Output:
s0:abcd! s1:abcdefghijklmnopqrstuvwxyz!
s0:abcdefghijklmnopqrstuvwxyz! s1:ijklmnopqrstuvwxyz!
s0:abcd! s1:abcdefghijklmnopqrstuvwxyz!
Date handling
- Dates and times are widely used in computing.
- Pervasive in the financial industry:
Age calculations for insurance
Time stamps for electronic transfer
Time differences for interest calculations
- Heavily used in software development:
Computer backups
Make and make-like programs
- Global positioning system
- MANY other examples (time is money, literally)
- Typical questions
- Given a date, what day of the week is it?
- What is the date 90 days from now?
- Given two dates, what is the difference between them?
- The ANSI standard date/time library
#include &time.h>
- Two kinds of time
- Low-level time
- Number of clock ticks since Jan. 1, 1900
- Data type:
time_t
- High-level time
- Conventional day/month/year
- Data type:
struct tm
with fields
tm_sec
, tm_min
,
tm_hour
, tm_mday
,
tm_mod
, tm_year
, etc.
- The Perl
localtime
function uses
struct tm
internally.
- Functions
time_t mktime(struct tm*)
Converts high-level time to low-level time.
Calculates day of week from DD/MM/YY.
Normalizes high-level dates in a useful way.
char *asctime(struct tm*)
Converts a struct tm
to string form.
double difftime(time_t,time_t)
Calculates the difference, in seconds,
between two low-level dates.
Example: What day of the week is Nov. 24 1995?
#include <stdio.h>
#include <time.h>
int main()
{
struct tm high; /* "high-level" time: month, day, year, etc. */
/* given a date, what day of the week is it? */
high.tm_sec = 0;
high.tm_min = 30;
high.tm_hour = 13;
high.tm_mday = 24;
high.tm_mon = 10;
high.tm_year = 95;
mktime(&high); /* to compute day of week */
printf("day of week: %d\n",high.tm_wday);
printf("%s",asctime(&high)); /* human-readable form */
return 0;
}
Example: What is the date 90 days from Nov. 24 1995?
#include <stdio.h>
#include <time.h>
int main()
{
struct tm high; /* high-level" time: month, day, year, etc. */
/* today's date */
high.tm_sec = 0;
high.tm_min = 30;
high.tm_hour = 13;
high.tm_mday = 24;
high.tm_mon = 10;
high.tm_year = 95;
/* increment */
high.tm_mday += 90;
/* to normalize */
mktime(&high);
/* display */
printf("%s",asctime(&high));
return 0;
}
Example: How many days in the F96 term?
#include <stdio.h>
#include <time.h>
int main()
{
struct tm high0,high1; /* high-level" time: month, day, year, etc. */
time_t low0,low1; /* low-level time: ticks since Jan. 1, 1900 */
int diff;
/* 13:30 on first day of class F96 */
high0.tm_sec = 0;
high0.tm_min = 30;
high0.tm_hour = 13;
high0.tm_mday = 4;
high0.tm_mon = 8;
high0.tm_year = 96;
/* 13:30 on last day of class F96 */
high1.tm_sec = 0;
high1.tm_min = 30;
high1.tm_hour = 13;
high1.tm_mday = 4;
high1.tm_mon = 11;
high1.tm_year = 96;
/* convert to low-level times */
low0 = mktime(&high0);
low1 = mktime(&high1);
/* calculate the difference in seconds */
diff = difftime(low1,low0);
/* convert to days */
diff = diff / (24*60*60);
/* display */
printf("%d\n",diff);
return 0;
}
Bottom line
- Learn to use the character, string, and date library functions.
- Learn to write demonstration programs like the examples above.
It is the most efficient way to test your understanding.