atof(将字符串转换成浮点型数)
表头文件:
#include <stdlib.h>
定义函数:
double atof(const char *nptr)
功能说明:
把字符串转换成浮点数
代码示例:
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
float f;
char *str = "12345.67";
f = atof(str);
printf("string = %s\nfloat = %f\n", str, f);
return 0;
}
结果展示:
string = 12345.67
float = 12345.669922
atoi(将字符串转换成浮点型数)
表头文件:
#include <stdlib.h>
定义函数:
int atoi(const char *nptr)
功能说明:
把字符串转换成整型数
代码示例:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *ptr1 = "-12345.12";
char *ptr2 = "+1234w34";
char *ptr3 = " 456er12";
char *ptr4 = "789 123";
int a,b,c,d;
a = atoi(ptr1);
b = atoi(ptr2);
c = atoi(ptr3);
d = atoi(ptr4);
printf("a = %d, b = %d, c = %d, d = %d\n", a,b,c,d);
return 0;
}
结果展示:
a = -12345, b = 1234, c = 456, d = 789
atol(将字符串转换成浮点型数)
表头文件:
#include <stdlib.h>
定义函数:
long atol(const char *nptr)
功能说明:
把字符串转换成长整型数
代码示例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
long val;
char str[20];
strcpy(str, "98993489");
val = atol(str);
printf("str = %s, val = %ld\n", str, val);
strcpy(str, "runoob.com");
val = atol(str);
printf("str = %s, val = %ld\n", str, val);
return(0);
}
结果展示:
str = 98993489, val = 98993489
str = runoob.com, val = 0
tolower(将大写字母转换成小写字母)
表头文件:
#include <ctype.h>
定义函数:
int tolower(int c)
功能说明:
若参数c为大写字母,则将其对应的小写字母返回,若不需要转换,则将参数c返回。
代码示例:
#include <stdio.h>
#include <ctype.h>
int main()
{
int i = 0;
char c;
char str[] = "HeLLO WorLD";
while( str[i] )
{
putchar(tolower(str[i]));
i++;
}
return(0);
}
结果展示:
hello world
toupper(将小写字母转换成大写字母)
表头文件:
#include <ctype.h>
定义函数:
int toupper(int c)
功能说明:
若参数c为大写字母,则将其对应的小写字母返回,若不需要转换,则将参数c返回。
代码示例:
#include <stdio.h>
#include <ctype.h>
int main()
{
int i = 0;
char c;
char str[] = "HeLLO WorLD";
while( str[i] )
{
putchar(toupper(str[i]));
i++;
}
return(0);
}
结果展示:
HELLO WORLD