0

I'm in an introductory programming class and we're only in lesson two, so try to keep terminology simple, ha.

I'm a bit confused about when you need to and when you don't need to initialize a variable.

My book says initialization is when you assign a value to a variable after it's declared.

I later looked at my language champion pdf, and it only showed initialized values when we weren't prompting a user.

It showed that if we wanted to display our name, age, and dollars, all we'd put is initialized statements.

However, if we wanted to know information from the user concerning values, there wasn't any initialized statement: only declarative ones.

Tell me if I'm misinterpreting this. http://prntscr.com/beu3hw http://prntscr.com/beu3s7

If I'm correct, can someone explain what advantages there are to initializing values and why uninitialized variables commonly cause logic errors?

My book also says in many languages uninitialized values values hold unpredictable values; this is due to those languages setting aside a place in memory for the variable, but not altering the contents of that place in memory.

Could someone perhaps word this in a different way?

1
  • 4
    You might be better off asking your instructor. The answer might be "wait until lesson 4". Jun 10, 2016 at 20:02

3 Answers 3

5

In languages like C, newly-declared variables essentially point to a (more or less) random memory location. If you declare the variable without also initializing it, it will contain whatever random value the memory happens to contain at the location in memory that the new variable points to.* Using the variable in this state will cause unpredictable behavior.

Assigning a value to the variable means that the memory location is set to the value that you have assigned, so that you and your program now have an expectation of what that variable contains.

*This description is deliberately simplified. Your actual mileage may vary.

8
  • Ah, so initializing a variable is the same as naming a variable and attaching an assignment statement to it? " If you declare the variable without also initializing it, it will contain whatever value the memory happens to contain at the location in memory that the new variable points to." That makes sense. Can you explain why in uninitialized variables (like the screenshot I shared) are okay if a user is typing in a value from their keyboard?
    – FBHSIE
    Jun 10, 2016 at 20:17
  • Because the act of typing in a value causes the variable to be assigned an actual value. Jun 10, 2016 at 20:18
  • That makes sense. So, when people type in values when a prompt pops up, their answers are initializing the variables?
    – FBHSIE
    Jun 10, 2016 at 20:23
  • Well yes, the variable that is specified in the cin statement is the one that gets initialized. Jun 10, 2016 at 20:23
  • 3
    @FBHSIE The rule is that reading from an uninitialized variable is undefined behavior. You are allowed to write to an uninitialized variable. This is what happens when you use that variable with cin - it writes a value to the variable (and, technically, that initializes it, so you can read from it from then on).
    – Cort Ammon
    Jun 10, 2016 at 20:33
0

I agree with the selected answer above, to go a little bit further.

You can declare variables - meaning make a variable by giving it a type and a name.

Example: int number_of_cats;

Initializing it can be done at the same time as declaration because you want to start with how many ever cats you have currently.

Example: int number_of_cats = 2;

Say you want to buy a cat every year for the next 10 years. You will initialize your variable to 2 and you will add 1 to it every year for 10 years. This is called an accumulator variable which is very likely that you will learn later. It is a good idea to initialize variables so that you can easily detect if something goes wrong in the code when you get a value in the variable that you do not expect!

0

Until you know better, stick to always initialising variables.

My book also says in many languages uninitialized values values hold unpredictable values; this is due to those languages setting aside a place in memory for the variable, but not altering the contents of that place in memory.

All values are held in memory; little "patches" of semi-conductorism somewhere in the computer's RAM chips. A Variable is a way of giving a "name" to [any] one of those patches. Declaring a variable tells your compiler, "I'm going to use this bit of memory for a [data type] value all of my very own and I will name it George."

In languages like 'C', that has no effect at all on the area of memory in question. That particular piece of memory might have been used for something completely different just a few [milli]seconds before and could contain absolutely anything. Not that 'C' will complain about this, necessarily; when you try to use the variable "George", the 'C' run-time will try to interpret whatever it find at the memory location identified by "George" as whatever data type it thinks it should be. If it's an Integer type, then it will [always] work, but you'll get all sorts of strange numbers as a result.

                      +-------------+ 
(int32) "George"  ->  | 12 34 56 78 | 
                      +-------------+ 
? George 
305419896

Initialising the variable "George" pushes a known value into that piece of memory, so that what you read back, it is what you expect it to be.

                           +-------------+ 
(int32) "George" = 3   ->  | 00 00 00 03 | 
                           +-------------+ 
? George 
3

The more complex your data types get, the more extreme the reaction that the run-time has to these strange, uninitialised values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.