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

发布于:2024-04-20 ⋅ 阅读:(23) ⋅ 点赞:(0)

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位计算机) 数据类型 内存大小 占位符
单精度浮点数 float 4 字节 %f, %e
双精度浮点数 double 8 字节 %lf, %e
整数 int 4 字节 %d
无符号整数 unsigned int 4 字节 %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