Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何扫描同一输入位置中是否存在字符或int_C_Char_Int_Scanf - Fatal编程技术网

C 如何扫描同一输入位置中是否存在字符或int

C 如何扫描同一输入位置中是否存在字符或int,c,char,int,scanf,C,Char,Int,Scanf,例如,如果我有这样一个输入:10 3 128,但也有这样一个第二个输入:16 2 F 如何扫描这些值? 我尝试将其作为字符串读取,然后尝试将其转换为int或保留为char(如果需要) fscanf(in, "%d %d %c or %d, from, to, numorhex); 我的程序基本上是一个数字系统转换器,我想知道如何扫描十六进制字符和整数。首先,您可以将扫描的所有内容存储在int中,也可以存储为char。 当您要在扫描后打印它时。您可以从这两种类型执行此操作。您可以执行以下操作:

例如,如果我有这样一个输入:10 3 128,但也有这样一个第二个输入:16 2 F

如何扫描这些值? 我尝试将其作为字符串读取,然后尝试将其转换为int或保留为char(如果需要)

fscanf(in, "%d %d %c or %d, from, to, numorhex);

我的程序基本上是一个数字系统转换器,我想知道如何扫描十六进制字符和整数。

首先,您可以将扫描的所有内容存储在
int
中,也可以存储为
char

当您要在扫描后打印它时。您可以从这两种类型执行此操作。

您可以执行以下操作:

int main(void)
{
    char *line[] = {"10 3 128", "16 2 F", NULL};
    int  nb1;
    int  nb2;
    int  nb3;
    char letter;
    char end;


    for (size_t i = 0; line[i]; ++i) {

        if (end = EOF, sscanf(line[i], "%d %d %d%c", &nb1, &nb2, &nb3, &end) == 3 && end == EOF) {
            printf("number number number : %d %d %d\n", nb1, nb2, nb3);
        } else if (end = EOF, sscanf(line[i], "%d %d %c%c", &nb1, &nb2, &letter, &end) == 3 && end == EOF) {
            printf("number number letter : %d %d %c\n", nb1, nb2, letter);
        } else {
            printf("format line not supported : %s\n", line[i]);
        }
    }

    return (0);
}

<> P.>真正的“LE”,“%C”会接受任何字符,比如“1”,所以也许你会想在你认为是字母的东西中限制%C的值。你读了吗?