+ 1

About Destructive

My teacher is said that if I do that: int sum; sum=5; //initialize sum=6; //destructive The second assignment process is called destructive. But when I looked programming book, it says that when you assign a value to a variable, you did the destructive process. It says variables have default value. I confused. Can anyone tell me what is the truth?

20th Sep 2017, 12:42 PM
Yusuf
Yusuf - avatar
4 odpowiedzi
+ 1
when you assign a value to a variable, WHICH HAS VALUE in C (and C++) variable hasn't default value, you should initialize it first. For example, in Pascal integer variable has default value 0
20th Sep 2017, 1:01 PM
Dmitry Semigradsky
Dmitry Semigradsky - avatar
+ 1
A variable has a default value. But it is not a defined value like 0 or "", it is rather the way the memory was when a space was allocated (by your computer, not you, in the stack) to hold your variable
20th Sep 2017, 8:47 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 1
Someone says it has a default value, someone says has not ... But what I see is that when I print the value of a variable I am getting memory adress of my variable in C++, but in C I am getting. Anyway I confused.
20th Sep 2017, 9:43 PM
Yusuf
Yusuf - avatar
+ 1
The state of the memory is not changed when you define a variable, so the variable contain what the memory had before.
21st Sep 2017, 6:54 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar