+ 5
Declaring but not assigning
What happens behind the scenes when you declare a variable but do not assign any value to it?
9 Respuestas
+ 11
There's this discussion if it comes to C++ (you didn't tag any language), quite enlightening, shame one of the responders is no longer here with us ...
https://www.sololearn.com/Discuss/1128723/?ref=app
+ 7
Matthias that's right, the thing is, the OP didn't specify the desired language, so I was practically shooting blanks, but that discussion gave me some insight (lesson) that things aren't always what/how we think they are, a post by ~ swim ~ there I think covered the most of it though, more than the main topic (array).
I agree with your last point there, the safer way to go : )
+ 6
Ipang This is a bit different though, because in that case unitialized array elements will be set to zero by default.
On the other side it is quite confusing and not really consistent that variables won't be set to zero by default at every case 🤔
Well at least for all compilers, some do.
Take home message:
Always make sure to initalize your variables 😅
+ 4
Yes, basically free memory space is assigned to this variable. Depending on its datatype it might be an larger area of memory.
For example double needs more space than int.
The value stored in this memory could be anything at this point.
+ 4
I hear uninitialized variables take garbage values in C. On Sololearn
int n;
printf("%d", n);
would output 0, probably because of some implementation as an online compiler. But on a real compiler, the outputs are apparently quite random.
+ 4
In PHP, if you declare variable without assigning any value. It will cause error!!!
+ 2
I'd like to know that myself!
My vague understanding is, that you'll get a memory area that was 'marked for deletion' before; so something might be already/still in it.
+ 2
I think they would assigned as null or nill or something similar according to the language. And if they are used in statements they are replaced with the type's default value(Defaults are 0,"",[],false according to the language)
But in string concatenations in languages like java null will be used.
eg : Stringar s1;
var s2 = "12",
...........print(s1+s2); //null12
+ 2
When we declare a variable but not assigned a value to it ,,it might go various cases in various programming languages ,,,,
Java:
It may give default values based on the data type.
C:
It may give compile time error like it might not be used bcoz there's no value assigned.
When we declare any function or variable,at that time a memory will be allocated to that particular variable or function.,,,whenever we want to use that variable or function we must be define ,,,,,,