1

I want to store a value value via scanf() in the function lorem(), and use it in the function ipsum(). I used static int, but it doesn't work.

static int *value;
void lorem(){
  static int *value;
  scanf("%d", value);
}

void ipsum(){
  static int *value;
  printf("%d", value);
}

void main(){
  lorem();
  ipsum();
}

This is my code, but it keeps forgetting the value of value int.

3
  • This is very basic C that can/should be learnt from any C book or tutorial - passing values to and from functions. lorem can return a value that is then passed into ipsum. Or lorem can take a pointer to an integer which it can write directly and that is then passed to ipsum.
    – kaylum
    Commented Feb 19, 2021 at 3:19
  • 1
    you haven't pointed value to anywhere
    – phuclv
    Commented Feb 19, 2021 at 3:24
  • 1
    Despite having the same name, the value declared in function lorem() is a different object than the one declared in ipsum(), 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. Commented Feb 19, 2021 at 3:24

1 Answer 1

4

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.

  1. 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.

  2. 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.

  3. (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]
    
    1. 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).
    2. 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:

  1. (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:

  1. http://www.cplusplus.com/reference/cstdio/scanf/
  2. http://www.cplusplus.com/reference/cstdio/printf/
  3. Why does printf not flush after the call unless a newline is in the format string?
  4. https://www.cplusplus.com/reference/cstdio/fflush/
  5. What should main() return in C and C++?
9
  • 3
    note that %i in scanf is different to %d (if you don't know what the difference is , you probably wanted %d)
    – M.M
    Commented Feb 19, 2021 at 3:38
  • @M.M, ah! You're right. Fixing it now. I've actually never used scanf, ironically enough. Doesn't exist on microcontrollers and tiny embedded systems. Commented Feb 19, 2021 at 3:39
  • @NateEldredge, good point. Adding flush command. Commented Feb 19, 2021 at 3:50
  • @user3386109: Yep, apparently so. Thanks. Commented Feb 19, 2021 at 4:19
  • please don't use cplusplus.com and link to en.cppreference.com/w/c instead which is more correct and updated What's wrong with cplusplus.com?
    – phuclv
    Commented Feb 19, 2021 at 4:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.