-1

This is a simple program

#include<stdio.h>
void get(int,int);

void main()
{
  int a,b;
  get(a,b);
  printf("In main");
  printf("%d",a);
}

void get(int m,int n)
{
  printf("enter the value");
  scanf("%d%d",&m,&n);
}

and I got an output is

enter the value

    4
    5

in main:

0

Why is the value of m in get() not assigned to a in main()? What's my mistake?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
abinaya
  • 7
  • 1
  • 1
    C uses call by value. `m` and `n` are *inputs* to the function `get()`, but if `get` modifies them (as it does here), the modified values do *not* make their way back to `main()`. In essence, you're trying to define a `get` function that returns two values. There are ways to do this, but they're more advanced. Suggest not worrying about this for now. – Steve Summit Mar 15 '18 at 07:23
  • You probably want a pointer to the value so you can change it and the value will also be changed outside the scope of the `get()` function. Otherwise you'll want to return something as those values are thrown away at the end of your function. – MeetTitan Mar 15 '18 at 07:24
  • 1
    Please ___properly___ indent your code. Machine (Compiler) can read and compile anything, but for humans, it needs to make a little _sense_ while reading a block of text as _code_. When asking question, there was a big orange __How to Format__ box to the right of the text area. There was also an entire toolbar of formatting aids. And a __[?]__ button giving formatting help. And a preview area showing what your post would look like when posted. Making your post clear, and demonstrating that you took the time to do so, improves your chances of getting good answers. – Sourav Ghosh Mar 15 '18 at 07:26
  • @RohanKumar Your edit might have been accepted sooner if you would have been more thorough with editing the prose for readability... – Yunnosch Mar 15 '18 at 07:27
  • 2
    Also, unrelated to your problem, but you should know: `main()` should be `int`, not `void`. Best to get that right while you're still learning. – Steve Summit Mar 15 '18 at 07:35
  • Think of what would happen if you called `get` like this: `get(1, 3)`. – Jabberwocky Mar 15 '18 at 07:55

4 Answers4

2

You're passing your main variables via value. Read about how you can pass by reference, here. scanf requires addresses of variables in order to modify them; so you need to pass their addresses like this:

get(&a, &b);

And you can modify your get() method like this:

void get(int* pM,int* pN) {
  printf("enter the value");
  scanf("%d%d, pM, pN);
}
Rohan Kumar
  • 5,427
  • 8
  • 25
  • 40
0

Your scanf reads into function local variables which store the values you give as parameters to the function.
Their values are not visible in the variables you give as parameters to the function.
You probably want to use pointers to variables as parameters, then the read values can end up in the variables pointed to by those pointer-parameters.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
0

This is basically happening due to scope of variables, since integers are passed by value and not by reference.

You need to return values of a and b for them to be present in main().

see more here https://www.tutorialspoint.com/cprogramming/c_function_call_by_value.htm

#inlcude<stdio.h>
int get(int,int);
void main() {
    int a,b;
    a = get(a,b);
    printf("In main");
    printf("%d",a);
}
int get(int m,int n){
    printf("enter the value");
    scanf("%d%d,&m,&n);
    return m;
}

Enter the value

10 20

In main

10

Also, read about indenting the code so that it's more readable.

Yuvraj Jaiswal
  • 1,605
  • 13
  • 20
0

Why is the value of m in get() not assigned to a in main()? What's my mistake?

First, you need to understand the concept of parameter passing in C.
[If you are not aware of formal and actual parameters, check this]

Technically, everything in C is pass-by-value. Here,

get(a,b);

you are passing the value of a and b variable to function get(). The value of actual parameter a and b will be copied to formal parameters m and n [in this case, the value of a and b variable is garbage since you have not initialized them]. Any modification to the value of formal parameters (m and n) in the calling function will not reflect in the actual parameters (a and b) because formal parameter storage is separate. Hence, the value of m in get() does not assigned to a in main().

Below part of the answer is based on the assumption that you are aware of the concept of pointers in C language. If not, I would suggest to pick choose a good C language book/tutorial and go through the concept of pointers.

C language provides a facility to pass a pointer to a function which is also pass-by-value only. It copies the value of the pointer, i.e. the address, to the function formal parameters and you can access the value stored at that address by dereferencing the pointer. Hence, any changes made in the value at the address passed will reflect in the calling function actual parameters.

So, you can do:

#include<stdio.h>

void get(int *, int *);

int main()
{
    int a, b;
    get(&a, &b);
    printf("In main\n");
    printf("a : %d, b = %d\n", a, b);
}

void get(int *m,int *n)
{
    printf("Enter the value:\n");
    scanf("%d%d", m, n); // m holds the address of a and n holds the address of b variable. 

    printf("Value entered:\n");
    printf("%d %d\n", *m, *n); //dereferencing the pointer m and n
}
Community
  • 1
  • 1
H.S.
  • 11,654
  • 2
  • 15
  • 32