小编Mar*_*rio的帖子

为什么scanf中需要%hd?

我创建了一个非常简单的程序,其中包含一个值,然后将其记忆到局部变量值,最后使用第二个选项,程序打印该值.

我的问题是:为什么只有在scanf参数中添加"h"时程序才能工作?换句话说:scanf()和我的本地int值变量之间有什么样的关系?

谢谢!

pS(我用Dev-C++(GCC)编译它.用Visual Studio工作)

#include <stdio.h>

main () {

    int value = 0;
    short choice = 0;

    do {
       printf("\nYour Choice ---> ");
       scanf("%d", &choice);  /* replace with "%hd" and it works */

       switch (choice) {
          case 1:
               printf("\nEnter a volue to store ");
               scanf("%d", &value);
               getchar();              
               printf("\nValue: %d", value);
               break;
          case 2:
               printf("\nValue: %d", value);            
               break;  
       }

    } while (choice < 3);

    getchar();
}
Run Code Online (Sandbox Code Playgroud)

c

6
推荐指数
1
解决办法
2万
查看次数

标签 统计

c ×1