Library Functions
返回
signal.h, raise
#include
#include
int main(void)
{
puts("start");
puts("This is 'raise' test.");
raise(SIGABRT);
puts("end.");
return 0;
}
/*
运行结果
start
This is 'raise' test.
*/
stdarg.h, 可变数据长度函数
#include
#include
int maxmin(int mode, int ct, ...);
int main(void)
{
int n;
n = maxmin('B', 4, 1030, 300, 100, 20);
printf("The biggest value is =%d\n",n);
n = maxmin('S', 3, 30, 20, 22, 10, 30);
printf("The smallest value is =%d\n", n);
return 0;
}
int maxmin(int mode, int ct, ...)
{
int i, wk, ans = 0;
va_list ap;
va_start(ap, ct);
for ( i=1; i<=ct; i++ ) {
wk = va_arg(ap, int);
if ( i == 1 ) ans = wk;
if (mode == 'B') {
if (ans < wk ) ans = wk;
}
else if (mode == 'S') {
if (ans > wk) ans = wk;
}
}
va_end(ap);
return ans;
}
/*
运行结果
The biggest value is =1030
The smallest value is =20
*/
stdlib.h,qsort
#include
#include
int intcmp(const void *a, const void *b);
int main(void)
{
int i;
int dt[10] = {45, 603, 23, 807, 53, 67, 32, 19, 713, 66};
qsort(dt, 10, sizeof(int), intcmp);
for (i=0; i<=9; i++)
printf("%d ", dt[i]);
printf("\n");
return 0;
}
int intcmp(const void *a, const void *b)
{
return *(int *)a - *(int *)b;
}
/*
运行结果
19 23 32 45 53 66 67 603 713 807
*/
stdlib.h,getenv
#include
#include
int main(void)
{
char *p;
p = getenv("PATH");
if ( p != NULL ) printf("%s\n", p);
p = getenv("windir");
if ( p != NULL ) printf("%s\n", p);
p = getenv("PROGRAMFILES");
if ( p != NULL ) printf("%s\n", p);
return 0;
}
/*
运行结果
C:\Perl\bin\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem
C:\WINDOWS
C:\Program Files
*/
stdlib.h div
#include
#include
int main(void)
{
div_t d;
d = div(8888,6);
printf("quotient=%d, remainder=%d\n",d.quot,d.rem);
return 0;
}
/*
运行结果
quotient=1481, remainder=2
*/
stdlib.h,bsearch
#include
#include
int intcmp(const void *a, const void *b);
int main(void)
{
int entry[100] = {34, 54, 22, 360, 52, 87, 37, 55,0};
int n = 0, nbr, *p;
while (entry[n])
++n;
qsort(entry, n, sizeof(int), intcmp);
nbr = 360;
p = (int *)bsearch(&nbr, entry, n, sizeof(int), intcmp);
if (p == NULL) printf("NO registed.\n");
else printf("%d registed\n", *p);
return 0;
}
int intcmp(const void *a, const void *b)
{
return *(int *)a - *(int *)b;
}
/*
运行结果
360 registed
*/
assert.h
#include
#include
int main(int argc, char *argv[])
{
printf("Start\n");
assert(argc != 1);
printf("End!\n");
return 0;
}
/*
运行结果
C:\>assert
Start
Assertion failed: argc != 1, file assert.c, line 9
abnormal program termination
C:\>assert sss
Start
End!
*/
确认浮动小数点的数值范围
#include
#include
int main(void)
{
printf("sizeof(float)=%d\n",sizeof(float));
printf("FLT_DIG=%d\n",FLT_DIG);
printf("FLT_MIN=%.20g\n",FLT_MIN);
printf("FLT_MAX=%.20g\n",FLT_MAX);
printf("sizeof(double)=%d\n",sizeof(double));
printf("DBL_DIG=%d\n",DBL_DIG);
printf("DBL_MIN=%.20g\n",DBL_MIN);
printf("DBL_MAX=%.20g\n",DBL_MAX);
printf("sizeof(long double)=%d\n",sizeof(long double));
printf("LDBL_DIG=%d\n",LDBL_DIG);
printf("LDBL_MIN=%.20g\n",LDBL_MIN);
printf("LDBL_MAX=%.20g\n",LDBL_MAX);
return 0;
}
/*
运行结果
sizeof(float)=4
FLT_DIG=6
FLT_MIN=1.1754943508222875e-038
FLT_MAX=3.4028234663852886e+038
sizeof(double)=8
DBL_DIG=15
DBL_MIN=2.2250738585072014e-308
DBL_MAX=1.7976931348623157e+308
sizeof(long double)=8
LDBL_DIG=15
LDBL_MIN=2.2250738585072014e-308
LDBL_MAX=1.7976931348623157e+308
*/
time.h
#include
#include
int main(void)
{
time_t shijian;
struct tm *ltime;
time(&shijian);
ltime = localtime(&shijian);
printf("%s\n",asctime(ltime));
return 0;
}
/*
运行结果
Tue Sep 16 15:02:46 2008
*/
stdlib.h atexit
#include
#include
void endmsg(void);
int main(void)
{
if (atexit(endmsg) == 0 )
puts("Registed!");
puts("abcdefghijk");
return 0;
}
void endmsg(void)
{
puts("It's OK!\n");
}
int main(void)
{
char ss;
double ddt;
int idt;
long int ldt;
ss = 8888;
ddt = atof(ss);
idt = atoi(ss);
ldt = atol(ss);
}
test080919.c
test080919.c(14) : warning C4047: 'function' : 間接参照のレベルが 'const char *'
と 'char ' で異なっています。
test080919.c(14) : warning C4024: 'atof' : の型が 1 の仮引数および実引数と異なり
ます。
test080919.c(15) : warning C4047: 'function' : 間接参照のレベルが 'const char *'
と 'char ' で異なっています。
test080919.c(15) : warning C4024: 'atoi' : の型が 1 の仮引数および実引数と異なり
ます。
test080919.c(16) : warning C4047: 'function' : 間接参照のレベルが 'const char *'
と 'char ' で異なっています。
test080919.c(16) : warning C4024: 'atol' : の型が 1 の仮引数および実引数と異なり
ます。
ss = abc;
test080919.c(13) : error C2065: 'abc' : 定義されていない識別子です。
char ss[];
test080919.c(8) : error C2133: 'ss' : サイズが不明です。
test080919.c(13) : warning C4047: '=' : 間接参照のレベルが 'char []' と 'const int
' で異なっています。
// stdlib.h atof,atoi,atol
#include
#include
int main(void)
{
char *ss = "8888";
double ddt;
int idt;
long int ldt;
ddt = atof(ss);
idt = atoi(ss);
ldt = atol(ss);
printf("ss ==>%s\n",ss);
printf("ddt==>%f\n",ddt);
printf("idt==>%d\n",idt);
printf("ldt==>%d\n",ldt);
return 0;
}
/*
运行结果
ss ==>8888
ddt==>8888.000000
idt==>8888
ldt==>8888
*/
// stdlib.h calloc
#include
#include
int main(void)
{
int *p;
p = (int *)calloc(1000000000000000000,sizeof(int));
if (p == NULL) {
printf("Can't obtain memory!\n");
}
return 0;
}
/*
运行结果
C:\VC6prg\c2pro>test080924
Can't obtain memory!
*/
p = (int *)calloc(1000000000000000000000,sizeof(int));
C:\VC6prg\c2pro>cl test080924.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8168 for 80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.
test080924.c
test080924.c(10) : error C2177: 定数が大きすぎます。
time.h gmtime
#include
#include
int main(void)
{
time_t shijian;
struct tm *ltime;
struct tm *gtime;
time(&shijian);
ltime = localtime(&shijian);
printf("Local time:%s", asctime(ltime));
gtime = gmtime(&shijian);
printf("UTC time: %s", asctime(gtime));
return 0;
}
/*
运行结果
Local time:Mon Oct 27 12:25:08 2001
UTC time: Mon Oct 27 03:25:08 2001
*/
返回