Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What will happen if '&' is not put in a 'scanf' statement?

Tags:

c

stdio

scanf

I had gone to an interview in which I was asked the question:

What do you think about the following?

int i; scanf ("%d", i); printf ("i: %d\n", i); 

I responded:

  • The program will compile successfully.
  • It will print the number incorrectly but it will run till the end without crashing

The response that I made was wrong. I was overwhelmed.

After that they dismissed me:

The program would crash in some cases and lead to an core dump.

I could not understand why the program would crash? Could anyone explain me the reason? Any help appreciated.

like image 943
Ashish Ahuja Avatar asked Jan 01 '16 14:01

Ashish Ahuja


People also ask

What will happen if we cut down the trees?

Without trees, formerly forested areas would become drier and more prone to extreme droughts. When rain did come, flooding would be disastrous. Massive erosion would impact oceans, smothering coral reefs and other marine habitats.

What will happen if there are no trees on Earth?

Substandard soil Without trees and roots to hold soil together, erosion would quickly occur and heavy rains would easily wash soil away. The soil would also be full of dangerous chemicals and pollutants that are normally filtered by trees, so attempting to grow anything on Earth would prove difficult.

What will happen if tree?

Trees purify the air by absorbing pollutants such as sulfur dioxide and nitrogen dioxide, reducing pollution. Trees also help prevent topsoil erosion because they break the force of wind and rain on soil, their roots bind the soil, and their decayed, falling leaves are absorbed by the earth and enrich the soil.

What will happen if the habitat?

The habitat of an animal provides it with necessities such as shelter, food, and protection. If the habitat of an animal is disturbed, then it will be forced to go to other places in search of food and shelter. The animal could get killed by other animals in this process.


1 Answers

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.

like image 177
haccks Avatar answered Oct 13 '22 11:10

haccks