c - 将整数读取为字符并将其打印为整数

标签 c scanf

对于输入 65,此代码段的输出是 32566。 请解释其背后的概念。

#include<stdio.h>

int main()
{
  char ch;
  int i;
  scanf("%c", &i);
  printf("%d",i);
  return 0;
}

最佳答案

在你的代码中

   scanf("%c", &i);

原因 undefined behavior .您为 %c 转换说明符提供的参数类型无效。

%c 需要一个指向字符类型的指针作为相应的参数,但您提供了一个指向整数的指针。

关于c - 将整数读取为字符并将其打印为整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47440764/

相关文章:

c++ - 在 C/C++ 代码中解析 python 脚本文件的最佳方法是什么

c++ - C/C++ 服务器不发送最后几行文件

matlab - 在 Matlab 中,如何在任意大的矩阵上执行 fprintf 命令

c++ - SDL fscanf 函数问题

c - 为什么主要编译器对 stdint.h 使用 typedef 而对 stdbool.h 使用 #define?

c - CryoPID 如何创建 ELF header ,或者是否有一种简单的方法来生成 ELF?

c - sscanf 的宽度选项不起作用

c - 如何知道 fscanf 中的换行符?

c - "int32 undeclared"海合会错误

C编程: Read file and search in a second file based on the data found