When a variable is defined, the compiler allocates memory for that variable.
int i; // The compiler will allocate sizeof(int) bytes for i
i
defined above is not initialized and have indeterminate value.
To write data to that memory location allocated for i
, you need to specify the address of the variable. The statement
scanf("%d", &i);
will write an int
data by the user to the memory location allocated for i
.
If &
is not placed before i
, then scanf
will try to write the input data to the memory location i
instead of &i
. Since i
contains indeterminate value, there are some possibilities that it may contain a value equivalent to the value of a memory address or it may contain a value which is out of range of memory address.
In either case, the program may behave erratically and will lead to undefined behavior. In that case anything could happen.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…