Do this:
#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
}
You have a couple of mistakes.
First off, don't do static int *value;
, do static int value;
. The int*
says to allocate just enough memory for a pointer to an int, which is a number which stores a RAM memory address to an int, but you don't want that, you want enough memory for an actual int.
Second, each time you do static int *value;
inside a function, you are telling that function to use this local version of the value
variable, instead of the global version, which is NOT what you want.
(A problem by technicality): main()
should be int main()
not void main()
. Add -Wpedantic
to your build flags and you'll see a warning otherwise:
main.c:32:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
- On this note, also add
return 0;
to the end of the main()
function to indicate it ran with "no error" (0
is used by popular convention to mean "no error" here).
- I've documented some of my notes about compiler build flags in my repo here: https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world#additional-c-and-c-build-notes-ex-wgcc-or-clang-compilers.
Also:
- (Maybe a problem in my example below, but not applicable to your original code): flush
stdout
by printing a newline char (\n
) OR calling fflush(stdout)
before calling scanf()
. See here (Why does printf not flush after the call unless a newline is in the format string?), and see also the comments below my answer.
With global variable
Run this code live online here: https://onlinegdb.com/HJ3bqfmtD.
Even better: also tell the user what you want by saying Enter an integer
and 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
}
WithOUT global variable
Run this code live online here: https://onlinegdb.com/ge-LVSsx7.
And better still, avoid using global variables for such a simple case. Global variables are useful and good in some cases, but this isn't one of them.
#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
}
References:
- 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++?
lorem
can return a value that is then passed intoipsum
. Orlorem
can take a pointer to an integer which it can write directly and that is then passed toipsum
.value
to anywherevalue
declared in functionlorem()
is a different object than the one declared inipsum()
, and both of them are different from the one declared at file scope. Delete the declarations inside the two functions to make them use the object declared at file scope.