【C语言】字符串转为数值,补充:浮点数、整数、长整数

server/2024/10/18 18:16:22/

C语言的标准库stdlib.h中有函数可将字符串转为数值(浮点数、整数、长整数)。

#include <stdlib.h>
  • atof:将字符串转为双精度浮点数(类型:double)。
  • atoi:将字符串转为整数(类型:int)。
  • atol:将字符串转为长整数(类型:long int)。
  • strtod:将字符串转为双精度浮点数(类型:double),并获得数值后的其它字符信息。
  • strtol:将字符串转为长整数(类型:long int),并获得数值后的其它字符信息。
  • strtoul:将字符串转为无符号长整数(类型:unsigned long int),并获得数值后的其它字符信息。

1、atof 和 strtod      字符串转为浮点数

atof:    double  atof(const  char  *str)

参数:str是需要转为浮点数的字符串。

返回:双精度浮点数(double类型)。若没有执行有效的转换,则返回零(0.0)。

注意:字符串第一个字符是数值,否则无法执行有效转换。

 strtod:  double  strtod(const  char  *str, char  **endptr)

参数:str是需要转为浮点数的字符串,endptr是指向str中数值后下一个字符的指针。

返回:双精度浮点数(double类型)。若没有执行有效的转换,则返回零(0.0)。

注意:字符串第一个字符是数值,否则无法执行有效转换。

#include <stdio.h>
#include <stdlib.h>int main(void)
{char s1[] =  "2024, Good luck!";double x = atof(s1);printf("x = %lf\n", x);char s2[] =  "hello 2024, Good luck!";double y = atof(s2);printf("y = %lf\n", y);return 0;
}// 结果:
x = 2024.000000
y = 0.000000
#include <stdio.h>
#include <stdlib.h>int main(void)
{char s1[] =  "2024, Good luck!";char *p;double x = strtod(s1, &p);printf("x = %lf, other chars are \"%s\"\n", x, p);char s2[] =  "hello 2024, Good luck!";char *q;double y = strtod(s2, &q);printf("y = %lf, other chars are \"%s\"\n", y, q);return 0;
}// 结果:
x = 2024.000000, other chars are ", Good luck!"
y = 0.000000, other chars are "hello 2024, Good luck!"

2、atoi      字符串转为整数

atoi:      int  atoi(const  char  *str)

参数:str是需要转为整数的字符串。

返回:整数(int类型)。若没有执行有效的转换,则返回零。

注意:字符串第一个字符是数值,否则无法执行有效转换。

#include <stdio.h>
#include <stdlib.h>int main(void)
{char s1[] =  "2024, Good luck!";int x = atoi(s1);printf("x = %d\n", x);char s2[] =  "hello 2024, Good luck!";int y = atoi(s2);printf("y = %d\n", y);return 0;
}// 结果:
x = 2024
y = 0

3、atol 和 strtol      字符串转为长整数

atol:       long  int  atol(const  char  *str)

参数:str是需要转为长整数的字符串。

返回:长整数(long int 类型)。若没有执行有效转换,则返回零。

注意:字符串第一个字符是数值,否则无法执行有效转换。

 strtol:    long  int  strtol(const  char  *str, char  **endptr, int base)

参数:str是需要转为长整数的字符串,endptr是指向str中数值后下一个字符的指针,base是转换基数(介于2和36(含)之间,可以特殊值0)。

返回:长整型(long int 类型)。若没有执行有效转换,则返回零。

注意:

字符串第一个字符是数值,否则无法执行有效转换。

若参数base为0,则根据字符串的前缀来判断:若字符串以 '0x' 或 '0X' 开头,则视为十六进制;若以 '0' 开头,则视为八进制;否则视为十进制。

#include <stdio.h>
#include <stdlib.h>int main(void)
{char s1[] = "20240415, Good luck!";long x = atol(s1);                   // long 是 long int 的简写 printf("x = %ld\n", x);char s2[] = "hello 20240415, Good luck!";long y = atol(s2);printf("y = %ld\n", y);return 0;
}// 结果:
x = 20240415
y = 0
#include <stdio.h>
#include <stdlib.h>int main(void)
{char s1[] = "20240415, Good luck!";char *p;long x = strtol(s1, &p, 10);printf("x = %ld, other chars are \"%s\"\n", x, p);char s2[] = "hello 20240415, Good luck!";char *q;long y = strtol(s2, &q, 10);printf("y = %ld, other chars are \"%s\"\n", y, q);return 0;
}// 结果:
x = 20240415, other chars are ", Good luck!"
y = 0, other chars are "hello 20240415, Good luck!"

4、strtoul     字符串转为无符号长整数

strtoul:      unsigned long  int  strtoul(const  char  *str, char  **endptr,  int base)

参数:str是需要转为无符号长整数的字符串,endptr是指向str中数值后下一个字符的指针,base是转换基数(介于2和36(含)之间,可以特殊值0)。

返回:无符号长整型(unsigned long int 类型)。若没有执行有效转换,则返回零。

注意:

字符串第一个字符是数值,否则无法执行有效转换。

若参数base为0,则根据字符串的前缀来判断:若字符串以 '0x' 或 '0X' 开头,则视为十六进制;若以 '0' 开头,则视为八进制;否则视为十进制。

#include <stdio.h>
#include <stdlib.h>int main(void)
{char s1[] = "20240415, Good luck!";char *p;long x = strtoul(s1, &p, 10);printf("x = %lu, other chars are \"%s\"\n", x, p);char s2[] = "hello 20240415, Good luck!";char *q;long y = strtoul(s2, &q,10);printf("y = %lu, other chars are \"%s\"\n", y, q);return 0;
}// 结果:
x = 20240415, other chars are ", Good luck!"
y = 0, other chars are "hello 20240415, Good luck!"

补充:浮点数、整数、长整数

(注: 64位计算机)数据类型内存大小占位符
单精度浮点数float4 字节%f, %e
双精度浮点数double8 字节%lf, %e
整数int4 字节%d
无符号整数unsigned int4 字节%u
长整数long 4 字节%ld
无符号长整数unsigned long 4 字节%lu

注:各数据类型的内存存储大小与计算机位数有关。例如:int 类型,64位计算机(windows)4bytes。unsigned int 类型,64位计算机(windows)4bytes。

整数最高位为符号位(0为正数, 1为负数),可以有负数。但无符号整数最高位不是符号位,只是普通数值位,没有负数。

可以使用 sizeof 运算符 查看各数据类型的内存存储大小。

#include <stdio.h>int main(void)
{printf("memory size: float is %d bytes\n", sizeof(float));printf("memory size: double is %d bytes\n", sizeof(double));printf("memory size: int is %d bytes\n", sizeof(int));printf("memory size: unsigned int is %d bytes\n", sizeof(unsigned int));printf("memory size: long is %d bytes\n", sizeof(long));printf("memory size: unsigned long is %d bytes\n", sizeof(unsigned long));return 0;
}// 结果:   (Windows 64位计算机)
memory size: float is 4 bytes
memory size: double is 8 bytes
memory size: int is 4 bytes
memory size: unsigned int is 4 bytes
memory size: long is 4 bytes
memory size: unsigned long is 4 bytes

可使用 标准库float.h的宏 查看浮点数的最大值和最小值。

可使用 标准库limits.h的宏 查看各整数类型的最大值和最小值。

#include <stdio.h>
#include <float.h>
#include <limits.h>int main(void)
{printf("max value: float is %.10e\n", FLT_MAX);               // %e:(科学计数法)指数形式输出浮点数printf("min value: float is %.10e\n", FLT_MIN);printf("max value: double is %.10e\n", DBL_MAX);printf("min value: double is %.10e\n", DBL_MIN);printf("max value: int is %d\n", INT_MAX);printf("min value: int is %d\n", INT_MIN);printf("max value: unsigned int is %u\n", UINT_MAX);printf("max value: long is %ld\n", LONG_MAX);printf("min value: long is %ld\n", LONG_MIN);printf("max value: unsigned long is %lu\n", ULONG_MAX);return 0;
}// 结果:    (Windows 64位计算机)
max value: float is 3.4028234664e+038
min value: float is 1.1754943508e-038
max value: double is 1.7976931349e+308
min value: double is 2.2250738585e-308
max value: int is 2147483647
min value: int is -2147483648
max value: unsigned int is 4294967295
max value: long is 2147483647
min value: long is -2147483648
max value: unsigned long is 4294967295


http://www.ppmy.cn/server/4633.html

相关文章

【C++】-List经典面试笔试题总结-删除-插入-情况-合并-排序等经典操作

在C中&#xff0c;list 容器是标准模板库&#xff08;STL&#xff09;中的一种双向链表容器。以下是一些关于 list 的经典笔试面试题及解答&#xff1a; 1. list 容器的主要特点是什么&#xff1f; 解答&#xff1a; list 容器的主要特点包括&#xff1a; 它是一个双向链表结…

spring高级篇(二)

1、Aware和InitializingBean Aware和InitializingBean都与Bean的生命周期管理相关。 Aware接口: 概念: Aware接口是Spring框架中的一个标记接口&#xff0c;它表示一个类能够感知到&#xff08;aware of&#xff09;Spring容器的存在及其特定的环境。Spring框架提供了多个Awar…

免费申请泛域名证书

通配符证书是一种比较特殊的SSL/TLS 证书&#xff0c;可用于保护多个域名&#xff08;含主域名&#xff09;&#xff0c;由域名字段中的通配符 (*) 指示。这种证书主要用于具有很多子域的组织。通配符证书对主域及其所有次级子域有效。 对于免费通配符证书而言&#xff0c;目前…

近万字详解Docker常用功能合集(Docker系列第1章,共3章)

极简概括 官网&#xff1a;https://www.docker.com 利用比虚拟机更加轻量级的容器化虚拟技术&#xff0c;能够低成本的把当前环境快速打包或在新环境部署相同子环境的运维工具&#xff0c;基于Go语言实现&#xff0c;跨平台&#xff08;支持Linux、Windows、MacOS&#xff09;…

你的mongodb客户端是哪个呢?

MongoDB 是一种流行的文档数据库&#xff0c;它可以支持多种场景和应用。有很多客户端工具可以用来管理和操作 MongoDB&#xff0c;以下是一些常用的工具&#xff0c;以及它们的介绍&#xff1a; 一、MongoDB Shell MongoDB Shell 是连接&#xff08;和使用&#xff09;MongoD…

代码随想录算法训练营Day14 | 二叉树理论基础、递归遍历、迭代遍历、统一迭代 | Python | 个人记录向

本文目录 二叉树理论基础二叉树的形式二叉树的存储方式二叉树的遍历方式二叉树的代码定义 二叉树递归遍历前序中序后序 二叉树迭代遍历前序中序后序 二叉树统一迭代思路前序中序后序 以往忽略的知识点小结个人体会 二叉树理论基础 代码随想录&#xff1a;二叉树理论基础 二叉…

中文编程入门(Lua5.4.6中文版)第十二章用《魔兽天下》的概念来解释Lua的元表概念。

如果要找一款网游来类比上述关于Lua元表的解释风格&#xff0c;可以考虑《魔兽天下》。尽管《魔兽天下》是一款大型多人在线角色扮演游戏&#xff08;MMORPG&#xff09;&#xff0c;其核心游戏机制并不直接涉及Lua编程语言或元表概念&#xff0c;但其世界观和游戏内元素与解释…

如何导出https服务器端证书

如何导出https服务器端证书&#xff08;即SSL证书中的服务器证书&#xff09;&#xff1f;要导出https服务器端证书&#xff0c;可以按照以下流程进行操作。 1&#xff09;登录服务器 使用适当的登录方式登录服务器&#xff0c;以获得访问权限。 定位证书文件 找到证书文件…