You call input(h);
and then void input(int x) { ... }
. The value of h
is copied into x
If you want to get back the changed value, you can either :
return
a value from the function input()
:
// notice the int before function name
int input();
int main (void)
{
int h = 0;
// notice h = ...
h = input(h);
printf("%d\n", h);
}
// notice the int before function name
int input()
{
int x;
printf("Enter a number: ");
scanf("%d", &x);
// notice the return
return x;
}
Or, pass the variable as reference (pointer) :
// notice int *
void input(int *x);
int main (void)
{
int h = 0;
// notice the &h this is used to pass addresses of variables
input(&h);
printf("%d\n", h);
}
// notice int *
void input(int *x)
{
printf("Enter a number: ");
// notice the & removed
scanf("%d", x);
}