C语言常用库函数:常用计算与字符判断转换

一、常用计算

// 幂运算:math.h  
// 函数原型:double pow(double x, double y)
int a = pow(2, 3);
printf("apow=%d\n", a);

// 平方根:math.h  
// 函数原型:double sqrt(double x)
int b = sqrt(5);
double c = sqrt(5);
printf("bSqrt=%d, cSqrt=%f\n", b, c);

// 绝对值:stdlib.h  
// 函数原型:int abs(int i)
int d = abs(55 - 88);
printf("dAbs=%d\n", d);

// 最大值、最小值:math.c
// 函数原型:double fmax(double x, double y); double fmin(double x, double y);
double eMax = fmax(-1, 77);
double eMin = fmin(-1, 77);
printf("eMax=%f, eMin=%f\n", eMax, eMin);

二、判断数字

// 判断字符是否为十进制数字:ctype.c
// 函数原型:int isdigit(int c);
// 若参数c为阿拉伯数字0~9,则返回非0值,否则返回0。
int fNum = isdigit('9');
int fNotNum = isdigit('a');
printf("fNUm=%d, fNotNum=%d\n", fNum, fNotNum);

// 判断字符是否为十六进制数字:ctype.c
// 函数原型:int isxdigit(int c);
// 若参数c为阿拉伯数字0~9,则返回非0值,否则返回0。
int fXNum = isdigit('D');
int fNotXNum = isdigit('j');
printf("fNUm=%d, fNotNum=%d\n", fNum, fNotNum);

三、判断字母与大小写字母转换

// 返回值:非0(小写字母为2,大写字母为1)-true, 0-false

// 判断是否为字母: ctype.c
// int isalpha(int c);
// 判断字符ch是否为英文字母,若为英文字母,返回非0(小写字母为2,大写字母为1)。若不是字母,返回0。
int galpha = isalpha('a');
int gAlpha = isalpha('A');
int gNotAlpha = isalpha('4');
printf("\ngalpha=%d, gAlpha=%d, gNoAlpha=%d\n", galpha, gAlpha, gNotAlpha);

// 判断是否为大写字母: ctype.c
// int isupper(int c);
// 判断字符ch是否为大写字母,若为大写字母,返回非0(小写字母为2,大写字母为1)。否则,返回0。
int gupper = isupper('a');
int gUpper = isupper('A');
int gNotUpper = isupper('4');
printf("\ngupper=%d, gUpper=%d, gNotUpper=%d\n", gupper, gUpper, gNotUpper);

// 判断是否为小写字母: ctype.c
// int islower(int c);
// 判断字符ch是否为小写字母,若为小写字母,返回非0(小写字母为2,大写字母为1)。否则,返回0。
int glower = islower('a');
int gNotLOWER = islower('A');
int gNotlower = islower('4');
printf("\nglower=%d, gNotLOWER=%d, gNotlower=%d\n", glower, gNotLOWER, gNotlower);


// 把大写字母转换成小写字母: ctype.c
// int tolower(int c);
// 如果 c 有相对应的小写字母,则该函数返回 c 的小写字母,否则 c 保持不变。。返回值是一个可被隐式转换为 char 类型的 int 值。
int hToLower = tolower('a');
int hNotToLower = tolower('A');
printf("\nhToLower=%c, hNotToLower=%c\n", hToLower, hNotToLower);

// 把小写字母转换成大写字母: ctype.c
// int toupper(int c);
// 如果 c 有相对应的大写字母,则该函数返回 c 的大写字母,否则 c 保持不变。返回值是一个可被隐式转换为 char 类型的 int 值。
int hToUpper= toupper('a');
int hNotToUpper = toupper('A');
printf("\nhToUpper=%c, hNotToUpper=%c\n", hToUpper, hNotToUpper);

posted @ 2021-08-07 23:39  Pangolin2  阅读(345)  评论(0编辑  收藏  举报