如果以下函数,您在使用的时候,总是输出一个莫名的值,是因为您忘记了引用头文件

#include <stdlib.h>

1- atoi

int atoi(const char *nptr);

把字符串转换成整型数。ASCII to integer 的缩写。

#include <stdio.h>
void funcAtoi(char *str)
{
float n;
n = atoi(str);
printf("str = %s, n = %f\n", str, n);
} int main()
{
char *str1 = "1234.567";
char *str2 = "你好";
funcAtoi(str1);
funcAtoi(str2);
system("PAUSE");
return -;
}

2- atol

long atol(const char *nptr);

把字符串转换成长整型数

 void funcAtol(char *str)
{
long n;
n = atol(str);
printf("str = %s, n = %ld\n", str, n);
} int main()
{
char *str1 = "1234.567";
char *str2 = "你好";
funcAtol(str1);
funcAtol(str2);
system("PAUSE");
return -;
}

3- strtod

strtod(将字符串转换成浮点数)

头文件:

#include <stdlib.h>

函数原型:

double strtod(constchar*nptr,char**endptr);

nptr:传入需要转换的字符串

endptr:接收字符串中无法转换的内容 例如以下红色字体部分 str = "hello",

atof主要的区别是:

strtod可以把输入的"123.456hello"拆分2部分返回,函数返回123.456,参数endptr返回hello

atof只可以返回"123.456hello"的数字部分,返回为123.456000 返回也是double类型

可以利用strtod函数处理数字字母组合的字符串.

 void funcStrtod()
{
char *str;
char *a = "12345.6789";
char *b = "1234.567hello";
char *c = "-123.23e4";
printf( "a = %.2lf\n", strtod(a,&str) );
printf( "str = %s\n", str );
9 printf( "b = %lf\n", atof(b));
10 printf( "b = %lf\n", strtod(b,&str) );
printf( "str = %s\n", str );
printf( "c = %lf\n", strtod(c,NULL) );
}

4- strtol

long int strtol(const char *nptr,char **endptr,int base);

nptr是需要转换的字符串

endptr是一个传出参数,函数返回时指向后面未被识别的第一个字符。

base代表采用的进制方式 10, 0表示10进制 16, 0x表示16进制 8, 0开始不是0x表示8进制

 主要特点: 可以读取2,4,8,16指定进制的数据.

 void funcStrtol()
{
char *str;
double x;
char *a = "0xf1hello world"; x = strtod( a, &str );
printf("a = %s\n", a );
printf("strtod = %f\n", x );
printf("Stopped scan at: %s\n\n", str ); x = strtol(a, &str, );
printf("a = %s\n", a );
printf("strtol = %f\n", x );
printf("base = %d\n", );
printf("Stopped scan at: %s\n\n", str );
}

返回值十进制 241 整好是十六进制0xf1

 5- strtoul

#include<stdlib.h>

unsigned long strtoul(const char *nptr,char **endptr,int base);

将字符串转换成无符号长整型数

例子:将十六进制 0xFF,转换成 10进制,得到 255

 void funcStrtoul()
{
char *a = "0xFF";
long n;
n = strtoul(a, NULL, );
printf("%d\n", n);
}

6- _gcvt

gcvt是把浮点数转换成字符串,同时返回一个指向字符串的存储位置的指针的函数。

char *gcvt(double value, int ndigit, char *buf);

 void func()
{
char str[];
double num;
int sig = ; /* significant digits */ num = 123456.02;
_gcvt(num, sig, str);
printf("\nstring = %s\n", str); /* a regular number */
num = 9.876;
_gcvt(num, sig, str);
printf("string = %s\n", str);
/* a negative number */
num = -123.4567;
_gcvt(num, sig, str);
printf("string = %s\n", str);
/* scientific notation */
num = 0.678e5;
_gcvt(num, sig, str);
printf("string = %s\n", str);
}

如果需要转换的值超过ndigit数,则使用科学计数法 && 四舍五入.

atoi atol strtod strtol strtoul _gcvt的更多相关文章

  1. strtol函數的用法 atof, atoi, atol, strtod, strtoul

    相关函数: atof, atoi, atol, strtod, strtoul表头文件: #include <stdlib.h>定义函数: long int strtol(const ch ...

  2. 字符串转换atof atoi atol gcvt strtod strtol strto ul toascii tolower toupper

    atof(将字符串转换成浮点型数) 相关函数 atoi,atol,strtod,strtol,strtoul 表头文件 #include <stdlib.h> 定义函数 double at ...

  3. c语言常用数据类型转换整理

    你要发送原始数据流 还是 格式化输出? 如果是格式化 按原子说的 ,用sprintf / printf; 如果发送原始内存数据流, 可按下面发送, 发送 #define BYTE0(pointer) ...

  4. c语言字符串 数字转换函数大全

    最近学数据结构老是做实验 常用到字符串和数字的转换 想找却发现网上的资料太散 所以搜集整理一下 方便以后再用 atof(将字符串转换成浮点型数) atoi(将字符串转换成整型数) atol(将字符串转 ...

  5. Unix/Linux环境C编程入门教程(26) 字符数字那些事儿

    1.gcvt() strtod() strtol() strtoul() toascii() tolower() toupper函数介绍 gcvt(将浮点型数转换为字符串,取四舍五入) 相关函数 ec ...

  6. C语言中字符串如何转换为二进制、八进制、十进制、十六进制

    在C语言某个程序当中需要把文本16进制转换成对应的16进制数,比如字符串"0x1a"转换成10进制的26,可以用以下函数来实现 相关函数: atof, atoi, atol, st ...

  7. linux常用C函数目录

    字符测试篇 isalnum isalpha isascii iscntrl isdigit isgraphis islower isprint isspace ispunct isupper isxd ...

  8. So many good projects for studying C programming lanuage.

    Some one asked a question for studying C programming language on stackexachange.com. He got a bucket ...

  9. [C++]PAT乙级1005. 继续(3n+1)猜想 (25/25)

    /* 1005. 继续(3n+1)猜想 (25) 卡拉兹(Callatz)猜想已经在1001中给出了描述.在这个题目里,情况稍微有些复杂. 当我们验证卡拉兹猜想的时候,为了避免重复计算,可以记录下递推 ...

随机推荐

  1. Tomcat学习笔记 - 错误日志 - NetBeans配置tomcat出错情况总结 -- 尚未授予访问 Tomcat 服务器的权限。请在服务器管理器的 Tomcat 定制器中设置 "manager-script" 角色的正确用户名和口令。 有关详细信息, 请查看服务器日志。

    错误描述: 发布时控制台出现: 部署错误: 尚未授予访问 Tomcat 服务器的权限.请在服务器管理器的 Tomcat 定制器中设置 "manager-script" 角色的正确用 ...

  2. Retrieving the COM class factory for component with CLSID XX failed due to the following error: 80070005 拒绝访问。

    环境及异常信息说明 环境说明: Win2008 R2 企业版 x64 .IIS 7.0 功能说明:服务端操作Excel,(上传Excel到服务器,并在服务器端读取Excel中的数据) 异常信息:Ret ...

  3. 符号三角形(hdu 2510 搜索+打表)

    符号三角形 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  4. 优盘文件系统(FOR C)

    优盘上的数据按照其不同的特点和作用大致可分为5 部分:MBR 区.DBR 区.FAT 区.FDT区和DATA 区. 主引导记录(MBR) 绝对扇区号为:MBR_LBA=0x00000000 处是主引导 ...

  5. C# Process类_进程管理器Demo

    Process用于管理计算机的进程,下面给出一个C#进程管理器的DEMO. namespace ProcessManager { public partial class Form1 : Form { ...

  6. UESTC_Frozen Rose-Heads CDOJ 791

    The winter is coming and all the experts are warning that it will be the coldest one in the last hun ...

  7. LintCode-A + B 用位操作模拟加法

    class Solution { public: /* * @param a: The first integer * @param b: The second integer * @return: ...

  8. 转(havel 算法)

    http://www.cnblogs.com/wally/p/3281361.html poj 1659(havel算法) 题目链接:http://poj.org/problem?id=1659 思路 ...

  9. REDHAT、CenterOS使用安装Linux系统时的光盘镜像来安装软件

    使用安装Linux系统时的光盘镜像来安装软件 (1)以虚拟机上,安装mysql为例: 查看mysql是否安装 rpm -qa|grep -i mysql    显示下面,证明mysql已安装客户端,下 ...

  10. 2014年百度之星资格赛第二题Disk Schedule

    Problem Description 有非常多从磁盘读取数据的需求,包含顺序读取.随机读取.为了提高效率,须要人为安排磁盘读取. 然而,在现实中,这样的做法非常复杂. 我们考虑一个相对简单的场景. ...