c - 没有错误,但为什么我们没有得到第二个 scanf() 语句的输入

标签 c

#include<stdio.h>
int main()
{ 
    char ch;
    int i;
    scanf("%c", &i);/* i know this must be reversed for next input but I want to know the impact of this code if we use scanf type char for integer data type*/
    scanf("%d", &ch);
    printf("%c %d", ch, i);
    return 0;
}

没有错误,但为什么没有获取第二个 scanf() 语句的输入

最佳答案

您将 i 声明为 int,将 ch 声明为 char

在您的 scanf 中,您有 %c 代表 i%d 代表 ch%c 用于 char%d 用于 int

所以只要把它们转过来就可以了。

使用printf,您将获得正确的格式。

关于c - 没有错误,但为什么我们没有得到第二个 scanf() 语句的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41628677/

相关文章:

c - c中的声明与定义

c - MPI_ERR_BUFFER 使用带有大消息的 MPI_Bsend()

c - 如何将字符数组转换为十进制整数数组?

c - 如何在一行中打印指定数量的字符

c 二进制文件读取问题

c - 如何在C中读取lua表

c - (1)在函数 ‘main’ :(2) warning: format ‘%d’ expects argument of type ‘int’ , 中,但参数 2 的类型为 ‘int *’ [-Wformat]

c - 类型转换为位字段

c - 函数原型(prototype)问题

c - C语言中如何初始化寄存器unsigned long