Why we are using "&" when we denote a string in c and c++..???? Example :scanf("%d",&a) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why we are using "&" when we denote a string in c and c++..???? Example :scanf("%d",&a)

25th Jan 2018, 1:52 PM
Manimozhi Saravanan
Manimozhi Saravanan - avatar
4 Answers
+ 2
Why we are using "&" when we denote a string in c and c++..???? Example :scanf("%d",&a) Answer:We use &a to pass the reference to the variable a. If we would just pass a, scanf would get, say, '5' (if that's the value a had) and no way of modifying the variable. Instead we pass a reference to the physical location of a, so scanf can go there and change it for us.
25th Jan 2018, 1:59 PM
Manimozhi Saravanan
Manimozhi Saravanan - avatar
+ 2
The & sign is simply stating that whatever value is inputted should head to the address of the string, int, etc. This address is a hexadecimal that the computer will point to whenever the data value is called. The & is simply assuring that the input goes to the address of the data.
25th Jan 2018, 2:02 PM
Faisal
Faisal - avatar
+ 2
& simply refers to the memory address of the variable we declare. Eg in this, & is referring to the memory address of a amd telling the function scanf() to get the input as integer (%d) amd store it at the memory address which is denoted by a. . Hope this clears you doubt :)
25th Jan 2018, 2:17 PM
Atharv Paliwal
+ 1
We use &a to pass the reference to the variable a. If we would just pass a, scanf would get, say, '5' (if that's the value a had) and no way of modifying the variable. Instead we pass a reference to the physical location of a, so scanf can go there and change it for us.
25th Jan 2018, 1:56 PM
Vlad Serbu
Vlad Serbu - avatar