/usr/include/ctype.h
for all the function prototypes.
#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; }
isalpha(c)
.
aA1!@
alphabetic: a alphabetic: A not alphabetic: 1 not alphabetic: ! not alphabetic: @ not alphabetic:
/usr/include/string.h
for all the function prototypes.
#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; }
s0:abcd! s1:abcdefghijklmnopqrstuvwxyz! s0:abcdefghijklmnopqrstuvwxyz! s1:ijklmnopqrstuvwxyz! s0:abcd! s1:abcdefghijklmnopqrstuvwxyz!
#include &time.h>
time_t
struct tm
with fields
tm_sec
, tm_min
,
tm_hour
, tm_mday
,
tm_mod
, tm_year
, etc.
localtime
function uses
struct tm
internally.
time_t mktime(struct tm*)
char *asctime(struct tm*)
struct tm
to string form.
double difftime(time_t,time_t)
#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; }
#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; }
#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; }