void main()
{
int a;
printf("Enter a value to store :");
scanf("%d",&a) ; // Why Can't we use a instead of &a to store value of variable a ???
printf("value of a is %d", a) ;
printf("\naddress of a is %d", &a) ;
getch() ;
}

- 400,186
- 35
- 402
- 621

- 31
- 5
-
2Welcome to stackoverflow.com. Please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please [take the tour](http://stackoverflow.com/tour) and [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). Lastly please read [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Oct 11 '18 at 06:22
-
Learn about functions and the reason for passing by pointer. Then it will become apparent. – DeiDei Oct 11 '18 at 06:22
-
As for your "question" (which you really should write separately and clearly outside code snippets), C only have *pass by value* when passing arguments to functions. That means values are copied, and arguments inside a function are then totally unrelated to the original expression used when calling the function. What `scanf` is doing is ***emulating** pass by reference*. – Some programmer dude Oct 11 '18 at 06:24
-
`int a[1]; scanf("%d", a); printf("you entered %d.\n", *a);` – pmg Oct 11 '18 at 07:05
1 Answers
Because in C parameters in function calls are passed by value. If you just give it a
, then there's no way to actually store a value. Sure, it can change that a
in the funtion, but then it only changed its copy of the value, not the original value. It doesn't even want that value, it wasnts to write a value. Therefore, what it needs is the address of a value, so it can use that address to write the new value there.
Why Can't we use a instead of &a to store value of variable a
Good question! What you have in mind is a concept called "Pass by reference", and some programming languages actually do this. There, the a
you pass would be the same a
, not just a copy of it, so if it is written to, the original is changed, too. For instance, in C++:
int a = 1;
void increase(int& myInt) {
myInt++;
}
int main() {
increase(a);
printf("%d\n", a);
return 0;
}
Here, 2
is printed, because the original a
was increased. Without the ampersand in the function signature, 1
would be printed because only the copy is modified.

- 16,736
- 2
- 25
- 44
-
@EricPostpischil The parameters in function calls. Sorry for being unclear, I added it to the answer. As I had some doubts, I wanted to double check it and it seems that the University of Utah is struggling with this... https://www.cs.utah.edu/~germain/PPS/Topics/C_Language/c_functions.html – Blaze Oct 11 '18 at 09:59
-
Can you please explain then why we do not use & [pass by value] for printf(). Please & Thank You! – A. man Oct 13 '18 at 15:02
-
1`printf` doesn't modify the variables you give it, so it is perfectly fine with a copy and doesn't need a pointer or reference to the original. The exception is when you want to print strings, then it expects a pointer to the first character in the string. So for instance if `char buf[32]` is my string, I could do `printf("string: %s", &buf[0])`. Or just `printf("string: %s", buf)`. – Blaze Oct 15 '18 at 06:28