小编bAs*_*AsH的帖子

为什么type int适用于sscanf但int16_t不适用?

我正在尝试将用户输入流的值分配给变量M和N.如果我指定类型为int的M和N,我可以使我的代码工作.但是,当我使用stdint.h将它们指定为int16_t时,它将读取第一个值,但不会读取最后一个值.为什么是这样?

这里的代码工作得很好......

#include <stdio.h>
#include <stdint.h>
int main(void)
{
    char str[10];
    int M, N;
    fgets(str, 10, stdin);
    sscanf(str, "%d%d", &M, &N);
    printf("M is: %d\n", M);
    printf("N is: %d\n", N);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在这里它不起作用.

#include <stdio.h>
#include <stdint.h>
int main(void)
{
    char str[10];
    int16_t M, N;
    fgets(str, 10, stdin);
    sscanf(str, "%d%d", &M, &N);
    printf("M is: %d\n", M);
    printf("N is: %d\n", N);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c scanf fgets stdint

1
推荐指数
1
解决办法
131
查看次数

标签 统计

c ×1

fgets ×1

scanf ×1

stdint ×1