Ç - 当时读单个int [英] C - Read a single int at the time

查看:103
本文介绍了Ç - 当时读单个int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种输入:

输入:2015年

和我想scanf函数他是这样的:

And I want to scanf him like this:

scanf("%1d", &arr);

但是,这肯定是不对的。我能做什么?
我想通过像2,整数以接收输入整数'0','1','5',而不是像2015

But this is definitely wrong. What can I do? I want to receive the input integer by integer like '2','0','1','5' , and not like "2015".

推荐答案

只是单独读取每个字符,以及个人字符秒值进行转换为整数。

Just read each character individually, and convert the individual chars to integers.

#include <stdio.h>

int main(int argc, char* argv[])
{
    size_t len, i;
    char buff[64];

    fgets(buff, sizeof(buff), stdin);

    for(i = 0, len = strlen(buff); i < len; ++i) {
        int curr = buff[i] - '0'; /* subtracting '0' converts the char to an int */
        printf("%d\n", curr);
    }
}

这篇关于Ç - 当时读单个int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆