Why we use ampersand in scanf but dont use ampersand in assignment operator on left hand side though we are giving it a value?
1
Expert's answer
2011-06-24T07:49:36-0400
The function "scanf" can have many arguments and every argument can be different: int, float, char etc. Naturaly each type of argument must be read its own way. But at the same time the function "scanf" is the same, so function "scanf" uses arguments' adresses instead of arguments (adress of any argument is always the same type - non-specified pointer). This is good idea of unification. To know of what type is& some argument's adress the function "scanf" uses specification such as "%i" for int or "%f" for float. We can get adress using ampersand.
Assignment operator looks the same for every type of argument, but it's not true: for every type there is its own assignment operator, don't metter that it always looks the same (as a sign "="). Thus there is no any cause to use ampersand, compiler knows what exact assignment operator to use. If we try to write something like following: int i=0,j=1; &i=j; then we will get an error. This is because we tryed to assign some value not to the variable, but we tried to set its adress.
Comments
Leave a comment