So i was not able to get the following code to work. User can pass and store a value to separate function and retrive it later. The function works in 3 ways.
- user can reset the value to 0 and return 0
- user can set the value to any input and return 0
- user can view the stored value by returning the stored value
the code is as follows:
#include <stdio.h>
#include <stdlib.h>
#pragma warning (disable:4996)
int storage(int input, int mode) {
int value;
if (mode == 0) { //if mode is zero, storage value is assigned as input
value = input;
return 0;
}
else { //if mode is non zero, simply return the storage value
return value;
}
}
int main(void) {
int input;
storage(0, 0); //reset storage value to 0
printf("out > %d\n", storage(0, 1)); //print storage value, should be 0
printf("in? > ");
scanf("%d", &input); //scan input from user
storage(input, 0); //assign input as new storage value
printf("out > %d\n", storage(0, 1)); //print new storage value, should be same to input
return 0;
}
But i keep getting data is uninitialized error:
Severity Code Description Project File Line Suppression State Error C4700 uninitialized local variable 'value' used
I am new to C, so im not exactly sure what to search online and most of the solution arent C, but C# or C++. Any help appreciated.