• C语言把字符串转换为数字


    C当中有一些函数专门用于把字符串形式转换成数值形式

    printf()函数和sprintf()函数 -->通过转换说明吧数字从数字形式转换为字符串形式;

    scanf()函数把输入字符串转换为数值形式;

    应用场景:

    编写程序需要使用数值命令形参,但是命令形参被读取为字符串。要使用数值必须先把字符串转换为数字。

    atoi()函数

    int atoi(char *str);

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 
     4 int main(void)
     5 {
     6     int i,times;
     7     
     8     if(argc < 2 || times = atoi(argv[1])<1)
     9         printf("Usage:%s positive-number
    ",argv[0]);
    10     else
    11         for(i=0;i<times;i++)
    12             puts("Hello,good looking!");
    13     
    14     return 0;
    15 }

    程序运行示例:

    $ hello 3

    Hello, good looking!

    Hello, good looking!

    Hello, good looking!

    作用就是根据参数,选择打印几次Hello, good looking!

    如果参数开头是非数字字符,则atoi函数返回值是0;因为这种行为是未定义的。因此需要有错误检测功能的strtol()函数会更安全。

     

    stdlib.h头文件:不仅包含atoi()函数,还包含了atof()函数、atol()函数;

    atof()函数把字符串转换成double类型的值;

    atol()函数把字符串转换成long类型的值;

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++

    strtol函数原型:long strtol(const char * restrict nptr,char ** restrict endptr, int base);

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #define LIM 30
     4 char * s_gets(char * st, int n);
     5 
     6 
     7 int main(void)
     8 {
     9     char number[LIM];
    10     char * end;
    11     long value;
    12 
    13     puts("Enter a number (empty line to quit);");
    14     while(s_gets(number,LIM)&& number[0] !='')
    15     {
    16         value =strtol(number,&end,10);
    17         printf("base 10 input,base 10 output:%ld,stopped at %s (%d)
    ",value,end, *end);
    18         value = strtol(number, &end, 16);
    19         printf("base 16 input,base 10 output:%ld,stopped at %s (%d)
    ",value,end, *end);
    20         puts("Next number:");
    21     }
    22     puts("Bye!
    ");
    23     return 0;
    24 }
    25 
    26 char * s_gets(char * st, int n)
    27 {
    28     char * ret_val;
    29     int i=0;
    30 
    31     ret_val = fgets(st, n, stdin); //读取成功,返回一个指针,指向输入字符串的首字符;
    32     if(ret_val)
    33     {
    34         while(st[i]!='
    ' && st[i]!='')
    35             i++;
    36         if(st[i] =='
    ') //fgets会把换行符也吃进来了,fgets会在末尾自动加上;
    37             st[i]='';
    38         else   //其实是''
    39             while(getchar() != '
    ')  //会把缓冲区后续的字符都清空
    40                 continue;
    41     }
    42     return ret_val;
    43 }

    程序运行:

    Enter a number (empty line to quit);
    10
    base 10 input,base 10 output:10,stopped at (0)
    base 16 input,base 10 output:16,stopped at (0)
    Next number:
    10atom
    base 10 input,base 10 output:10,stopped at atom (97)
    base 16 input,base 10 output:266,stopped at tom (116)
    Next number:

  • 相关阅读:
    LeetCode 230. 二叉搜索树中第K小的元素
    LeetCode 669. 修剪二叉搜索树
    LeetCode 94. 二叉树的中序遍历
    LeetCode 145. 二叉树的后序遍历
    LeetCode 144. 二叉树的前序遍历
    Not registered via @EnableConfigurationProperties or marked as Spring component
    maven依赖的报错Unable to import maven project: See logs for details
    GDIPlus的使用准备工作
    全局变量替代方案:控制反转,依赖注入
    MFC使用TRACKMOUSEEVENT触发mouseHover和mouseLeave
  • 原文地址:https://www.cnblogs.com/grooovvve/p/9938986.html
Copyright © 2020-2023  润新知