首页 > 解决方案 > 代码在输出 C 处无法正常工作

问题描述

我试图从键盘读取 2 个变量并将它们写在屏幕上,但我遇到了问题,程序只显示一个..

#include <stdio.h>
#include <stdlib.h>

int main()
{
    short int n,x;
    scanf("%d",&n);
    scanf("%d",&x);
    printf("%d %d",n,x);
    return 0;
}

我介绍了 14 和 15,程序返回我 0 和 15 有人能告诉我为什么吗?

标签: c

解决方案


对 short int 使用 %hd 格式说明符 对 unsigned int 使用 %hu 格式说明符 对 int 使用 %d 格式说明符 对 long int 使用 %ld 格式说明符

#include <stdio.h>
#include <stdlib.h>

int main() {
  short int n, x;
  scanf("%hd", &n); // Notice %hd instead of %d for short int
  scanf("%hd", &x); // Notice %hd instead of %d for short int
  printf("%hd%hd", n, x);// Notice %hd instead of %d for short int
  return 0;
}

推荐阅读