为什么scanf没有正确读取值?

mos*_*awi 2 c unix compiler-construction printf scanf

谁能告诉我这里出了什么问题?

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

#define ERROR 0
#define MAX_INPUT_LINE 80
#define print(x) {fputs(x,stdout);}
#define SUCCESS 1

int main (long argc, char *argv[])
{
   int mode;
   printf("1 for hexidecimal or 2 for binary");
   scanf("%d", mode);

   printf("\n\n\nThe value of mode is %d\n", mode);
   return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我为二进制输入2时,我得到了这个:

The value of mode is 2665564
Run Code Online (Sandbox Code Playgroud)

显然我应该得到2,我做错了什么?是我的编译器,是因为我使用的是Cygwin?为什么模式不是2?

Nat*_*man 7

你的scanf错了.应该:

scanf("%d", &mode);
Run Code Online (Sandbox Code Playgroud)

该&告诉编译器发送scanf一个指针来mode(即它的地址),而不是实际值mode.这样scanf可以mode使用新的扫描值进行更新.