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 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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With