1

The function is not mapping the user input to the variable

#include <stdio.h>

void input(int x);

int main (void)
{
    int h = 0;
    input(h);
    printf("%d\n", h);
}

void input(int x)
{
    printf("Enter a number: ");
    scanf("%d", &x);
}

When entering something like 5, I expect 5 to be printed

Blaze
  • 16,736
  • 2
  • 25
  • 44
Anew
  • 19
  • 2
  • 1
    I suggest you research the difference between passing by reference vs by value: https://stackoverflow.com/questions/13654138/what-exactly-is-the-difference-between-pass-by-reference-in-c-and-in-c – Morten Jensen Jul 10 '19 at 11:38

5 Answers5

3

x was passing by value, scanf changed the local copy of x. You need to return user input instead:

int input();

int main (void)
{
    int h;
    h = input();
    printf("%d\n", h);
}

int input()
{
    int x;
    printf("Enter a number: ");
    scanf("%d", &x);
    return x;
}
Renat
  • 7,718
  • 2
  • 20
  • 34
2

This is because parameters in C are passed by copy. So the x that you modify is just a copy of what you passed, and at the end of input, that copy is discarded. Instead, you could pass a pointer. Since scanf already operates on a pointer, the fix is simple:

void input(int *x);    // parameter is now a pointer to an int

int main(void)
{
    int h = 0;
    input(&h); // pass the address instead of the value
    printf("%d\n", h);
}

void input(int *x)
{
    printf("Enter a number: ");
    scanf("%d", x); // no need to take the address again
}
Blaze
  • 16,736
  • 2
  • 25
  • 44
1

Any decent book, tutorial or class should have mentioned that arguments in C are passed by value. That means the value is copied into the local argument variable, and any modifications to that local variable won't reflect on the original variable.

There are two possible solutions:

  1. The simple one: Return the value from the function

    int input(void)
    {
        int x;
        printf("Enter a number: ");
        scanf("%d", &x);
        return x;
    }
    
    ...
    
    int h = input();
    printf("%d\n", h);
    
  2. The complicated solution: Emulate pass by reference

    void input(int *x)
    {
        printf("Enter a number: ");
        scanf("%d", x);
    }
    
    ...
    
    int h;
    input(&h);  // Pass a pointer to the variable
    printf("%d\n", h);
    
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

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);
}
Cid
  • 14,968
  • 4
  • 30
  • 45
0

The problem is that you didn't understand the basic workflow of functions. Maybe you should once again revise your basics and theory. This is how your code should have been written (I have tried my best to make minimal changes to your code):

#include <stdio.h>

int input();

int main (void)
{
    int y;
    y = input();
    printf("%d\n", y);
}

int input()
{
    printf("Enter a number: ");
    scanf("%d", &x);
    return x;
}

You should have set the return type of function input() as int. Then, a variable (y in this case) could get the value returned by input() and use it in the main() function to display the actual input that the user had given.

Sagun Raj Lage
  • 2,326
  • 2
  • 18
  • 28