这样做:
#include <stdio.h>
static int value;
void lorem() {
// Note: `&` here should be read as "address of". This takes the
// address of `value` and passes it to scanf as type `int*`, or
// "pointer to int", AKA: "address of this int". `scanf()` then starts loading the
// number you type, byte-by-byte, into the memory at this address.
// Since that's the address for `value`, you end up putting a
// numerical value into `value.` Again, the address of `value` is also
// called a "pointer" to `value`, and `&value` is of type `int*` since
// `value` is of type `int`. And, ironically enough, passing by
// pointer is commonly called "pass by reference," and is the
// alternative to "pass by value".
scanf("%d", &value);
}
void ipsum() {
printf("%d", value);
}
int main() {
lorem();
ipsum();
return 0; // no error: program finished successfully
}
你有几个错误。
-
首先,不要做static int *value;,做static int value;。 int* 说要为 指向 int 的指针分配足够的内存,这是一个将 RAM 内存地址 存储到 int 的数字,但你没有想要那个,你需要足够的内存来存放 实际的 int。
-
其次,每次你在一个函数中执行static int *value;,你就是在告诉该函数使用value变量的这个本地版本,而不是全局版本,这不是什么你想要的。
-
(技术性问题):main() 应该是 int main() 而不是 void main()。将-Wpedantic 添加到您的构建标志中,否则您会看到警告:
main.c:32:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
- 在此注释中,还要将
return 0; 添加到main() 函数的末尾,以表明它运行时“没有错误”(0 按流行惯例在这里表示“没有错误”)。李>
- 我在我的仓库中记录了一些关于编译器构建标志的注释:https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world#additional-c-and-c-build-notes-ex-wgcc-or-clang-compilers。
还有:
- (也许在我下面的示例中存在问题,但不适用于您的原始代码):通过打印换行符 (
\n) 或调用 fflush(stdout) 来刷新 stdout在调用scanf() 之前。请参阅此处 (Why does printf not flush after the call unless a newline is in the format string?),另请参阅我的答案下方的 cmets。
带全局变量
在此处在线实时运行此代码:https://onlinegdb.com/HJ3bqfmtD。
更好的是:还可以通过说Enter an integer 和You entered 来告诉用户您想要什么:
#include <stdio.h>
static int value;
void lorem() {
printf("Enter an integer: ");
// Ensure the above printf is fully printed BEFORE waiting for
// user input. See my references below. Essentially, you may need
// to **either** have a `\n` at the end of the printf cmd, OR
// call `fflush(stdout)` like this, to ensure the stdout buffer
// is fully flushed (printed to screen), before the `scanf()`
// call reads in the user's input.
fflush(stdout);
scanf("%d", &value);
}
void ipsum() {
printf("You entered: %d\n", value);
}
int main() {
lorem();
ipsum();
return 0; // no error: program finished successfully
}
没有全局变量
在此处在线实时运行此代码:https://onlinegdb.com/ge-LVSsx7。
更好的是,避免在这种简单的情况下使用全局变量。全局变量在某些情况下很有用而且很好,但这不是其中之一。
#include <stdio.h>
int lorem() {
int value;
printf("Enter an integer: ");
// Ensure the above printf is fully printed BEFORE waiting for
// user input. See my references below. Essentially, you may need
// to **either** have a `\n` at the end of the printf cmd, OR
// call `fflush(stdout)` like this, to ensure the stdout buffer
// is fully flushed (printed to screen), before the `scanf()`
// call reads in the user's input.
fflush(stdout);
scanf("%d", &value);
return value;
}
void ipsum(int value) {
printf("You entered: %d\n", value);
}
int main() {
int val = lorem();
ipsum(val);
return 0; // no error: program finished successfully
}
参考资料:
- http://www.cplusplus.com/reference/cstdio/scanf/
- http://www.cplusplus.com/reference/cstdio/printf/
- Why does printf not flush after the call unless a newline is in the format string?
- https://www.cplusplus.com/reference/cstdio/fflush/
- What should main() return in C and C++?