Why the need to initialize before using the variable?

Contribute code & patches
Forum rules
No support questions here please

Why the need to initialize before using the variable?

Postby karuga on Tue Mar 15, 2011 4:34 am

I was recently using the Fibonacci series(iteratively not recursively) and it left me with a question.

int a=0;
int b=1;
int c=1; *
System.out.print ln(a);
System.out.print ln(b);

while(c>=89)
{
c=a+b;


System.out.print ln(c+",");
a=b;
b=c;
}
* the code could not run before initializing the variable c first to something like 1 or 0.
What is the explanation behind this?It is not clear why i need to do this initialization first.
karuga
Level 3
Level 3
 
Posts: 120
Joined: Wed May 12, 2010 1:49 am

Linux Mint is funded by ads and donations.
 

Re: Why the need to initialize before using the variable?

Postby bitmason on Tue Mar 15, 2011 6:31 pm

the main reason is that the instance of the variable "c" later in the code would have been undefined - the compiler would not have been able to allocate storage for it, since c could be anything - an int, a char, float, or even an instance of a class.

sorry, missed the point - initializing a variable is important at establishing an initial value. when compiled, a variable that is declared but not defined would have an "arbitrary" value. declaration only allocates storage for an object, and does not apply a value.
"The surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us."
- Calvin & Hobbes
User avatar
bitmason
Level 2
Level 2
 
Posts: 70
Joined: Mon Feb 21, 2011 12:27 am
Location: Arizona

Re: Why the need to initialize before using the variable?

Postby AlbertP on Wed Mar 16, 2011 12:31 pm

Just use:
Code: Select all
int c;

This code initializes the variable without giving it any value.
Registered Linux User #528502
Image
Feel free to correct me if I'm trying to write in Spanish, French or German.
AlbertP
Level 16
Level 16
 
Posts: 6504
Joined: Sun Jan 30, 2011 12:38 pm
Location: The Netherlands

Re: Why the need to initialize before using the variable?

Postby eiver on Wed Mar 16, 2011 12:57 pm

In fact it is possible to compile "int c;" depending on the compiler. The computer will know it is an int and not a float, but its value will be proably some random garbage from memory. Try creating an array and print its contents without initializing any values and you will get a nice memory dump :P I was sometimes even able to pick up some memory structures from my previous runs of the program. This all depends on the language and compiler. Typically higher level languages will automatically initialize variables to their default values.
User avatar
eiver
Level 5
Level 5
 
Posts: 564
Joined: Fri Jan 23, 2009 5:51 am

Re: Why the need to initialize before using the variable?

Postby AlbertP on Wed Mar 16, 2011 2:09 pm

Oh yes, I am used to VB and C# .NET which are quite different from C.

But if you assign your own value to the variable, it doesn't really matter that there is some memory garbage in it: that will become overwritten.
Registered Linux User #528502
Image
Feel free to correct me if I'm trying to write in Spanish, French or German.
AlbertP
Level 16
Level 16
 
Posts: 6504
Joined: Sun Jan 30, 2011 12:38 pm
Location: The Netherlands

Re: Why the need to initialize before using the variable?

Postby cliste on Fri Mar 18, 2011 7:04 pm

The reason being that the variable c would otherwise be used before it is defined.

ie the program when run will do the following:
1- create variable 'a', assign the value 0 to it
2- create variable 'b', assign the value 1 to it
3- create variable 'c', assign the value 1 to it
4- print the value of 'a' (namely 0)
5- print the value of 'b' (namely 1)
6- check if c > 98
7- set c = a+b
...

On step six it would break down as it cannot compare an empty variable to a number. The problem being that you ar actually using the variable as part of the while loop before it is initialised.

(On a side note you *may* be able to get away without assigning a value to the variable at step 3 if you use a do{ ... }while() loop, but I may be wrong on that)
http://riomhaireacht.webs.com/ (On the off-chance that there are some Irish speakers reading my sig :) )
If anyone knows anything about Laptop VGA output I would be much obliged!
User avatar
cliste
Level 2
Level 2
 
Posts: 65
Joined: Wed Apr 21, 2010 5:44 pm

Re: Why the need to initialize before using the variable?

Postby AlbertP on Sat Mar 19, 2011 8:19 am

With a do { ... } while() instead of while() { ... } it works 100% OK.
Registered Linux User #528502
Image
Feel free to correct me if I'm trying to write in Spanish, French or German.
AlbertP
Level 16
Level 16
 
Posts: 6504
Joined: Sun Jan 30, 2011 12:38 pm
Location: The Netherlands

Linux Mint is funded by ads and donations.
 

Return to Code & Patches

Who is online

Users browsing this forum: No registered users and 0 guests