printf 在 C 中给我错误的输出 [英] printf giving me incorrect output in C
问题描述
这可能是一个非常基本的问题,但我在任何地方都找不到答案,这是我在用 C 编程几周后第一次遇到这个问题.本质上,如果我写一些看起来像这样的代码这个:
This is probably a very elementary problem, but I cannot find the answer anywhere, and this is the first time I've had the problem after several weeks of programming in C. In essence, if I write some code looking something like this:
int size;
scanf("%d", &size);
printf("size is %d", &size);
如果我输入,比如说,size = 2,程序会打印出类似 133692 或类似数字的内容.为什么是这样?我在这里做错了什么?
If I input, say, size = 2, the program will print back out something along the lines of 133692 or a similar number. Why is this? What have I done wrong here?
推荐答案
尝试
printf("size is %d", size);
&
为您提供变量的内存位置(地址).
&
gives you the memory location (address) of a variable.
printf("size is %d", &size);
所以,上面打印的是size
的内存位置(地址),而不是size
中存储的值.
So, the above will print the memory location(address) of size
, not the value stored in size
.
这篇关于printf 在 C 中给我错误的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!