为什么在C输入中需要变量的内存位置而不是变量本身的名称?

Har*_*esh 0 c memory variables location scanf

有什么特别的原因,为什么在输入C(scanf)时更喜欢采用变量的内存位置而不是变量本身的名称?我知道上面强制要求的scanf的定义?但有什么特别的原因吗?就像在c ++中一样,如果你想要输入,你会写cin >> var; 但为什么不呢?是否可以让语言更快?

unw*_*ind 5

这是因为C中函数的值是按值传递.当然,作为变量的编译语言名称在运行时不存在,它是内存中的所有地址.

如果直接传递变量,只需传递值,这是printf()需要的:

int a = 32;
printf("a=%d\n", a);
Run Code Online (Sandbox Code Playgroud)

但如果你用scanf()', it would just get the integer value32 执行相同的, with no idea where it came from. Since the point of扫描()`是要改变调用者变量的值,你传递变量的地址:

int a = 32;
scanf("%d", &a);
Run Code Online (Sandbox Code Playgroud)

然后里面的代码scanf()可以在给定的地址写一个新的整数值,这会导致值改变a.