Is the value of x just some random bits?
In C, when a variable is initialized without a specific value (e.g. int x instead of int x=0)...?
Yes, usually to a random location in heap memory.
Reply:A variable in C that has not been initialized can be in one of two states depending on the version of C you are using:
In one version, it is assigned a memory address that is cleared of any previous info. In this case, the variable is "null".
In other versions, the memory address may NOT be cleaned out, and in that case the variable is "undefined".
Most of the C programming languages I've used were of the latter variety, and you can get strange results if you try to use the undefined value accidentally.
Reply:The value you get is what ever was in that part of memory before. So yes this is usually a random piece of information.
But if you define a string and then display the data, remembering that there will be some non-printable characters, you may find that you always get the same information.
This is all dependent on the compiler and the computer on which the program is running.
Reply:Yes, it picks a memory location to store the variable in, so the variable will assume the value of whatever data was already in that memory location.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment