函数讲解部分参考http://net.pku.edu.cn/~yhf/linux_c/

atof(将字符串转换成浮点型数)
相关函数
atoi,atol,strtod,strtol,strtoul
表头文件
#include <stdlib.h>
定义函数
double atof(const char *nptr);
函数说明
atof()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束时('\0')才结束转换,并将结果返回。参数nptr字符串可包含正负号、小数点或E(e)来表示指数部分,如123.456或123e-2。
返回值
返回转换后的浮点型数。
附加说明
atof()与使用strtod(nptr,(char**)NULL)结果相同。
范例
/* 将字符串a 与字符串b转换成数字后相加*/
#include<stdlib.h>
main()
{
char *a=”-100.23”;
char *b=”200e-2”;
float c;
c=atof(a)+atof(b);
printf(“c=%.2f\n”,c);
}
执行
c=-98.23
 

atoi(将字符串转换成整型数)
相关函数
atof,atol,atrtod,strtol,strtoul
表头文件
#include<stdlib.h>
定义函数
int atoi(const char *nptr);
函数说明
atoi()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束时('\0')才结束转换,并将结果返回。
返回值
返回转换后的整型数。
附加说明
atoi()与使用strtol(nptr,(char**)NULL,10);结果相同。
范例
/* 将字符串a 与字符串b转换成数字后相加*/
#include<stdlib.h>
mian()
{
char a[]=”-100”;
char b[]=”456”;
int c;
c=atoi(a)+atoi(b);
printf(c=%d\n”,c);
}
执行
c=356
 

atol(将字符串转换成长整型数)
相关函数
atof,atoi,strtod,strtol,strtoul
表头文件
#include<stdlib.h>
定义函数
long atol(const char *nptr);
函数说明
atol()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束时('\0')才结束转换,并将结果返回。
返回值
返回转换后的长整型数。
附加说明
atol()与使用strtol(nptr,(char**)NULL,10);结果相同。
范例
/*将字符串a与字符串b转换成数字后相加*/
#include<stdlib.h>
main()
{
char a[]=”1000000000”;
char b[]=” 234567890”;
long c;
c=atol(a)+atol(b);
printf(“c=%d\n”,c);
}
执行
c=1234567890
 

gcvt(将浮点型数转换为字符串,取四舍五入)
相关函数
ecvt,fcvt,sprintf
表头文件
#include<stdlib.h>
定义函数
char *gcvt(double number,size_t ndigits,char *buf);
函数说明
gcvt()用来将参数number转换成ASCII码字符串,参数ndigits表示显示的位数。gcvt()与ecvt()和fcvt()不同的地方在于,gcvt()所转换后的字符串包含小数点或正负符号。若转换成功,转换后的字符串会放在参数buf指针所指的空间。
返回值
返回一字符串指针,此地址即为buf指针。
附加说明
范例
#include<stdlib.h>
main()
{
double a=123.45;
double b=-1234.56;
char *ptr;
int decpt,sign;
gcvt(a,5,ptr);
printf(“a value=%s\n”,ptr);
ptr=gcvt(b,6,ptr);
printf(“b value=%s\n”,ptr);
}
执行
a value=123.45
b value=-1234.56
 

strtod(将字符串转换成浮点数)
相关函数
atoi,atol,strtod,strtol,strtoul
表头文件
#include<stdlib.h>
定义函数
double strtod(const char *nptr,char **endptr);
函数说明
strtod()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,到出现非数字或字符串结束时('\0')才结束转换,并将结果返回。若endptr不为NULL,则会将遇到不合条件而终止的nptr中的字符指针由endptr传回。参数nptr字符串可包含正负号、小数点或E(e)来表示指数部分。如123.456或123e-2。
返回值
返回转换后的浮点型数。
附加说明
参考atof()。
范例
/*将字符串a,b,c 分别采用10,2,16 进制转换成数字*/
#include<stdlib.h>
mian()
{
char a[]=”1000000000”;
char b[]=”1000000000”;
char c[]=”ffff”;
printf(“a=%d\n”,strtod(a,NULL,10));
printf(“b=%d\n”,strtod(b,NULL,2));
printf(“c=%d\n”,strtod(c,NULL,16));
}
执行
a=1000000000
b=512
c=65535
 

strtol(将字符串转换成长整型数)
相关函数
atof,atoi,atol,strtod,strtoul
表头文件
#include<stdlib.h>
定义函数
long int strtol(const char *nptr,char **endptr,int base);
函数说明
strtol()会将参数nptr字符串根据参数base来转换成长整型数。参数base范围从2至36,或0。参数base代表采用的进制方式,如base值为10则采用10进制,若base值为16则采用16进制等。当base值为0时则是采用10进制做转换,但遇到如'0x'前置字符则会使用16进制做转换。一开始strtol()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,再遇到非数字或字符串结束时('\0')结束转换,并将结果返回。若参数endptr不为NULL,则会将遇到不合条件而终止的nptr中的字符指针由endptr返回。
返回值
返回转换后的长整型数,否则返回ERANGE并将错误代码存入errno中。
附加说明
ERANGE指定的转换字符串超出合法范围。
范例
/* 将字符串a,b,c 分别采用10,2,16进制转换成数字*/
#include<stdlib.h>
main()
{
char a[]=”1000000000”;
char b[]=”1000000000”;
char c[]=”ffff”;
printf(“a=%d\n”,strtol(a,NULL,10));
printf(“b=%d\n”,strtol(b,NULL,2));
printf(“c=%d\n”,strtol(c,NULL,16));
}
执行
a=1000000000
b=512
c=65535
 

strtoul(将字符串转换成无符号长整型数)
相关函数
atof,atoi,atol,strtod,strtol
表头文件
#include<stdlib.h>
定义函数
unsigned long int strtoul(const char *nptr,char **endptr,int base);
函数说明
strtoul()会将参数nptr字符串根据参数base来转换成无符号的长整型数。参数base范围从2至36,或0。参数base代表采用的进制方式,如base值为10则采用10进制,若base值为16则采用16进制数等。当base值为0时则是采用10进制做转换,但遇到如'0x'前置字符则会使用16进制做转换。一开始strtoul()会扫描参数nptr字符串,跳过前面的空格字符串,直到遇上数字或正负符号才开始做转换,再遇到非数字或字符串结束时('\0')结束转换,并将结果返回。若参数endptr不为NULL,则会将遇到不合条件而终止的nptr中的字符指针由endptr返回。
返回值
返回转换后的长整型数,否则返回ERANGE并将错误代码存入errno中。
附加说明
ERANGE指定的转换字符串超出合法范围。
范例
参考strtol()
 

toascii(将整型数转换成合法的ASCII 码字符)
相关函数
isascii,toupper,tolower
表头文件
#include<ctype.h>
定义函数
int toascii(int c)
函数说明
toascii()会将参数c转换成7位的unsigned char值,第八位则会被清除,此字符即会被转成ASCII码字符。
返回值
将转换成功的ASCII码字符值返回。
范例
#include<stdlib.h>
main()
{
int a=217;
char b;
printf(“before toascii () : a value =%d(%c)\n”,a,a);
b=toascii(a);
printf(“after toascii() : a value =%d(%c)\n”,b,b);
}
执行
before toascii() : a value =217()
after toascii() : a value =89(Y)
 

tolower(将大写字母转换成小写字母)
相关函数
isalpha,toupper
表头文件
#include<stdlib.h>
定义函数
int tolower(int c);
函数说明
若参数c为大写字母则将该对应的小写字母返回。
返回值
返回转换后的小写字母,若不须转换则将参数c值返回。
附加说明
范例
/* 将s字符串内的大写字母转换成小写字母*/
#include<ctype.h>
main()
{
char s[]=”aBcDeFgH12345;!#$”;
int i;
printf(“before tolower() : %s\n”,s);
for(i=0;I<sizeof(s);i++)
s[i]=tolower(s[i]);
printf(“after tolower() : %s\n”,s);
}
执行
before tolower() : aBcDeFgH12345;!#$
after tolower() : abcdefgh12345;!#$
 

toupper(将小写字母转换成大写字母)
相关函数
isalpha,tolower
表头文件
#include<ctype.h>
定义函数
int toupper(int c);
函数说明
若参数c为小写字母则将该对映的大写字母返回。
返回值
返回转换后的大写字母,若不须转换则将参数c值返回。
附加说明
范例
/* 将s字符串内的小写字母转换成大写字母*/
#include<ctype.h>
main()
{
char s[]=”aBcDeFgH12345;!#$”;
int i;
printf(“before toupper() : %s\n”,s);
for(i=0;I<sizeof(s);i++)
s[i]=toupper(s[i]);
printf(“after toupper() : %s\n”,s);
}
执行
before toupper() : aBcDeFgH12345;!#$
after toupper() : ABCDEFGH12345;!#$

linux实例代码如下:

 #include <stdio.h>

 #include <stdlib.h>

 void atof_fun();//string reverse to float

 void atoi_fun();//string reverse to int

 void atol_fun();//string reverse to long int

 void gcvt_fun();//float reverse to string

 void strtod_fun();//string reverse to float

 void strtol_fun();//string reverse to long int

 void toascii_fun();//int reverse to ascii

 void tolower_fun();//such as A reverse a;

 void toupper_fun();//such as a reverse A;

 int main()

 {

     int Index;

     printf("1.atof---string reverse to float\n");

     printf("2.atoi---string reverse to int\n");

     printf("3.atol---string reverse to long int\n");

     printf("4.gcvt---float reverse to string\n");

     printf("5.strtod---string reverse to float\n");

     printf("6.strtol---string reverse to long int\n");

     printf("7.toascii---int reverse to ascii\n");

     printf("8.tolower---such as A reverse to a\n");

     printf("9.toupper---such as a reverse to A\n");

     printf("0.Exit\n");

     while()

     {

         printf("Please choose the function you want done(0-exit):\n");

         scanf("%d",&Index);

         if(Index==)

         {

             printf("Bye Bye\n");

             break;

         }

         else

         {

             switch(Index)

             {

                 case :atof_fun();

                     break;

                 case :atoi_fun();

                     break;

                 case :atol_fun();

                     break;

                 case :gcvt_fun();

                     break;

                 case :strtod_fun();

                     break;

                 case :strtol_fun();

                     break;

                 case :toascii_fun();

                     break;

                 case :tolower_fun();

                     break;

                 case :toupper_fun();

                     break;

                 default:

                     break;

             }

         }

     }

    return ;

 }

 //string reverse to float

 void atof_fun()

 {

     char a[];

     char b[];

     printf("Please input the string a:\n");

     scanf("%s",a);

     printf("Please input the string b:\n");

     scanf("%s",b);

     float c;

     c=atof(a)+atof(b);

     printf("float number c=%.2f\n",c);

 }

 //string reverse to int

 void atoi_fun()

 {

     char a[];

     char b[];

     printf("Please input the string a:\n");

     scanf("%s",a);

     printf("Please input the string b:\n");

     scanf("%s",b);

     int c;

     c=atoi(a)+atoi(b);

     printf("int number c=%d\n",c);
} //string reverse to long int void atol_fun() { char a[]; char b[]; printf("Please input the string a:\n"); scanf("%s",a); printf("Please input the string b:\n"); scanf("%s",b); long c; c=atol(a)+atol(b); printf("long int number c=%ld\n",c); } //float reverse to string void gcvt_fun() { double a; int aIndex; double b; int bIndex; printf("Please input a and aIndex:\n"); scanf("%lf,%d",&a,&aIndex); printf("Please input b and bIndex:\n"); scanf("%lf,%d",&b,&bIndex); char str[]; gcvt(a,aIndex,str); printf("%lf reverse to string---%s\n",a,str); gcvt(b,bIndex,str); printf("%lf reverse to string---%s\n",b,str); } //string reverse to float void strtod_fun() { char a[],*ptr; double value; printf("Please input a string:\n"); scanf("%s",a); value=strtod(a,&ptr); printf("The string is %s the number is %lf\n",a,value); } //string reverse to long int void strtol_fun() { char a[],*ptr; long int value; printf("Please input a string\n"); scanf("%s",a); value=strtol(a,&ptr,); printf("The string is %s the number is %ld\n",a,value); } //int reverse to ascii
void toascii_fun() { int a; char b; printf("Please input the non-ascii number a:\n"); scanf("%d",&a); b=toascii(a); printf("Before ascii---(%d)to(%c)\n",a,a); printf("After ascii---(%d)to(%c)\n",a,b); } //A reverse to a void tolower_fun() {
char a[]; int i; printf("Please input the string of a:\n"); scanf("%s",a); printf("Before tolower:%s\n",a); for(i=;a[i]!=;i++) { a[i]=tolower(a[i]); } printf("After tolower:%s\n",a); } //a reverse to A void toupper_fun() {
char a[]; int i; printf("Please input the string of a:\n"); scanf("%s",a); printf("Before toupper:%s\n",a); for(i=;a[i]!=;i++) { a[i]=toupper(a[i]); } printf("After toupper:%s\n",a); }


运行结果:

 

最新文章

  1. [LeetCode] Sequence Reconstruction 序列重建
  2. Spark入门实战系列--7.Spark Streaming(上)--实时流计算Spark Streaming原理介绍
  3. Codeforces 578B &quot;Or&quot; Game
  4. 每日一笔记之2:QT之坐标系统:
  5. VCL ActiveX 播放视频
  6. 关于 Unity NavMesh 数据的访问
  7. eclipse指定启动时的jdk(xjl456852原创)
  8. android 内存泄漏分析技巧
  9. #ifdef,#else,#endif,#if 拾忆
  10. 让Visualstudio在win10下使用管理员方式运行
  11. callback回调函数的理解
  12. Nginx 防盗链 secure_link 模块
  13. day 59 Bootstrap学习
  14. NetBeans IDE 多行标签设置方法
  15. 异构数据库之间完全可以用SQL语句导数据
  16. js实现类似qq表情(插入图片以及获取光标的效果)
  17. linux文件系统命令(1)---概述
  18. 【iOS开发】 AudioSession设置, 切换扬声器和听筒详解-保留其他应用音乐(备忘)
  19. [语法]C语言中二维数组做输入参数
  20. jq仿 妙味课堂导航01

热门文章

  1. Linux系统目录/bin /sbin /usr/bin /usr/sbin和/lib /usrlib的一些分析
  2. Android中退出多个Activity的两个经典方法
  3. GetCurrentDirectory、SetCurrentDirectory和GetModuleFileName
  4. 解决问题之,wp项目中使用MatchCollection正则表达式匹配出错
  5. poj 3301 Texas Trip(几何+三分)
  6. VF(动态规划)
  7. Java中对象的三种状态
  8. ISO9126 质量模型
  9. web推送
  10. 项目总结之MIT (一)