Why use symbol & when using scanf in C?

  • Thread starter jd12345
  • Start date
  • Tags
    Symbol
In summary, the symbol "&" is used before a variable in scanf because it generates a pointer to that variable. This is necessary for scanf to store the value and change the variable. In contrast, printf uses a copy of the value of the variable, which is passed as a parameter. This is just a convention and serves a purpose for scanf to function properly.
  • #1
jd12345
256
2
Why use symbol "&" when using scanf in C?

When using scanf like in scanf("%d",&x); why do we use '&' before x? can't we write directly ,x as in printf command? Is it just a convention? I think it should have a reason.. they won't just put an "&" just like that
 
Technology news on Phys.org
  • #2


"&x" means to generate a pointer to "x". Scanf needs a pointer to x in order to store the value and change "x". Printf uses a copy of the value of "x" passed as a parameter (usually on the stack), since it isn't changing the value of "x".
 

1. Why is the symbol & used with scanf in C?

The symbol & is used in scanf to pass the memory address of a variable. This allows the scanf function to directly modify the value of the variable in memory.

2. Can I use scanf without the symbol &?

No, the symbol & is necessary when using scanf in order to pass the memory address of the variable and allow for direct modification.

3. What happens if I forget to use the symbol & with scanf?

If you forget to use the symbol &, the scanf function will not be able to modify the value of the variable and may lead to unexpected results or errors.

4. Why is the symbol & not used with other functions in C?

The symbol & is primarily used with scanf in order to modify the value of a variable directly. Other functions in C typically use pointers to access and modify variables.

5. Is there an alternative to using the symbol & with scanf?

No, there is not an alternative to using the symbol & with scanf. It is necessary in order to pass the memory address of the variable and allow for direct modification.

Similar threads

  • Programming and Computer Science
Replies
2
Views
930
  • Programming and Computer Science
Replies
4
Views
733
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
667
  • Engineering and Comp Sci Homework Help
Replies
3
Views
878
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Programming and Computer Science
2
Replies
47
Views
4K
Back
Top