使用strtol()将字符串值转换为long类型值

#include <stdio.h>
#include <stdlib.h>

#define LIM 30
char * s_gets(char * st, int n);

int main(){

    char number[LIM];
    char * end;
    long value;

    puts("Enter a number (empty line to quit):");
    while(s_gets(number, LIM) && number[0] != '/0')
    {
        value = strtol(number, &end, 10);  /* base 10 */
        printf("base 10 input, base 10 output: %ld, stopped at %s (%d)/n",value, end, *end);
        value = strtol(number, &end, 16);  /* base 16 */
        printf("base 16 input, base 10 output: %ld, stopped at %s (%d)/n",value, end, *end);
        puts("Next number:");
    }
    puts("Bye!/n");

    return 0;
}

char * s_gets(char * st, int n){
    char * ret_val;
    int i = 0;

    ret_val = fgets(st, n, stdin);
    if (ret_val)
    {
        while (st[i] != '/n' && st[i] != '/0')
            i++;
        if (st[i] == '/n')
            st[i] = '/0';
        else // must have words[i] == '/0'
            while (getchar() != '/n')
                continue;
    }
    return ret_val;
}

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/266708.html

(0)
上一篇 2022年6月7日
下一篇 2022年6月7日

相关推荐

发表回复

登录后才能评论