I had gone to an interview in which I was asked the question:
What do you think about the following?
scanf ("%d", i);
printf ("i: %d\n", i);
The program would crash in some cases and lead to an core dump.
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
will write an
int data by the user to the memory location allocated for
& is not placed before
scanf will try to write the input data to the memory location
i instead of
i contains indeterminate value, there are some possibilities that it may contain the value equivalent to the value of a memory address or it may contain the 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.