Thursday, December 24, 2009

C Programming Question: Why is it important to initialize a variable?

Hm....





























Coz we need 2


Ex:


int i;


while (i%26lt;100) printf(';%d';,i);





And run.


Now, can u imagine what is the result?


But if:


int i=10;


while (i%26lt;100) printf(';%d';,i);


we know what the computer do.





Is it important to initialize them?


int long, width;


int A;


scanf(';%d %d';,long,widht);


A=long*width;


printf(';%d';,A);





the var long, width, and A are not important to initialize first use, coz long and width using input. And A, off cors u know that!C Programming Question: Why is it important to initialize a variable?
The reason it is so important to initialize a variable is because of the following.





an uninitialized variable holds the value of the previous memory contents(sometimes).





this becomes a problem when using pointers which should be initialized to null until you are ready to use them because they point at anything.





if you do not initialize a variable and you attempt to use your program if it even compiles you are sitting on a ticking time bomb.





why declare the variable if you don't initialize it. initializing it allows you to do something with it.





the only time you should not initialize a variable is if you are going to set it using a function, but even if you are setting a pointer it still should be set to null before it is set it is just considered good practice





and global variables in C are set to zero usually. that section of the program is called the bss section.C Programming Question: Why is it important to initialize a variable?
Most of the answers to this question are either misleading or just plain incorrect.





Depending on how you declare a variable, it may have an undefined value until you assign it a new one. (Global variables are usually automatically initialized to zero when your application starts). If there exists a path through your logic that allows the variable to be used before you ever set it, you're going to be working with an undefined value. Here are some examples:





int choice, x, y;


scanf(';%d';, %26amp;choice);


Note that choice did not need to be initialized, since you just set it.





if ( choice == 0 )


{


x = 0;


}


else


{


x = 1;


}


printf(';%d';, x);


Note that x need not be initialized, since there's no logical path that would use it before it's set.





if ( x == 1 )


{


y = 5;


}


printf(';%d';, y);


Note in the last printf() call, the value of y could very well still be undefined.





if ( y == 5 )


{


printf(';Since y is 5, we know x is 1, therefore choice was not zero.';);


}


Note that if y was left uninitialized, it's value is undefined, and it could very well be equal to 5. The previous printf() call gives a false statement. The choice could have been 0, which would make x equal 0, and y was never set. But if y's undefined value happened to be 5, you would still see the statement printed out.





Always initializing all your variables is a form of training wheels that are usually taught to beginners as something that you must do or else ';it wont work.'; In most cases it's a very good practice. If you see the following code:


int x = 0;


scanf(';%d';, %26amp;x);


you might find it silly that the author initialized his variable unnecessarily. But on the other hand, imagine you are getting someone to read through your code to help you debug it. In the larger example above, since you didn't initialize x, the reader would have to suspect x could be causing you a problem, and they'd have to trace through all of your logic in order to find out that x need not be initialized. In that case, initializing x would have made your code read a lot more clearly. This is particularly the case in C, where all variables must be declared at the top of a block, as opposed to C++ where you can declare them wherever you need them. And in all cases, write a comment to state what you are meaning to do.





If you are running your application in a debug mode, the debugger will usually initialize your variables for you with noticeable values such as 0xCCDDCCDD and such. You can use this technique in your own production code by writing noticeable signatures across variables that are newly instantiated, or should no longer be used, etc. That way when you're debugging the values can give you clues to what has previously happened in that area of memory.
C variable is stored either in stack or heap memory. That memory is not initialized during run-time. So you have to initialize the local variable which references the stack memory or the allocated variable (by new operator or malloc/calloc functions) which references the heap memory.
You write a software program to run the way you want. The initialization let you know exactly what is the starting point if you use that variable. Assigned to any values (including NULL) will let your software start with known state.
The computer doesn't know if it is 0,1,3,2,534,2324521432563635 that's why you need to initialize it. Aren't you listening to your teacher?
If you never initialize it, whats the point of even having it?
so there is no chance of it being null before being used.
you cant use if it hasn't been initialized

1 comment: