Click here to Skip to main content
15,895,656 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
what is the Difference between Declaring and defining a variable?
what is variable inilization?
Posted
Comments
AditSheth 17-Sep-11 7:36am    
Hello friend
its a basic concept, c concept. Please understood it very well..
Thanks

It might be helpful,

5. Variables[^]

in the MSDN under section 5 (5. Variables)there are sub sections 5.1,5.2,5.3,5.4,5.5 which has good description about the Variables.

:)
 
Share this answer
 
I think you need to take one step back, and get clear about what the difference is between Value types and Reference Types in .NET, and how they are stored in memory as they are created and used.

You "declare" variables in your code: they may or may not be automatically 'initialized' to some default value, depending on several factors (which you can understand by getting clear about Value and Reference Types).

I find the use of the word "defined" a little ambiguous because: at times it's used to mean the same thing as declared, and at other times it's used to mean the variable has a value assigned to it, or the (reference Type) variable has been instantiated

It may be helpful to think that you "assign values" to Value type variables, but you "create instances" of reference types. In the case of the 'string' type it gets a bit complex, and there's a good discussion of that complexity here[^].

Get a good book on C#, perhaps one from Jesse Liberty, and get "grounded" on what .NET Types are, and what variables are; from there you can consider the use of 'static' vs. non-static Types, and so forth.

A little study now, and you can solve so many problems later.

best, Bill
 
Share this answer
 
v2
C++
There is big deference between declaring and defining a variable

//when you write this then it means declaring a variable
int a;

//or 
float b;


// when you assign any value to that variable is called defining

a = 10;
//Or
b =22.56;
// you can also write this type of statements its called defining at declaration time
int a = 10;
//or 
float b = 22.56;
//or 
float c = a/b;
 
Share this answer
 
Very basic.
"Declare" as word state, we announce a veriable/function with it's datatype
C#
int a;


Defination means to assign/describe veriables/function
C#
a = 10;
 
Share this answer
 
Declaring a variable is just like
string str;

defining means assigning a value to that variable as below
str="Hello";


After declaring a variable if we assign a value then memory is allocated to that variable in stack, this is called initializing a variable.
 
Share this answer
 
v2
A definition gives information about the type of a variable, structure or class, such as:
C++
class MyClass
{
public:
    MyClass();
    func(int i, string& str);
;

MyClass is defined as a class with a constructor and a single method; the implementation will be in a source code file (.cpp).
You declare an object when you create it by declaring its type and scope, thus:
C++
MyClass myObject;

myObject is declared as an object variable of the MyClass class. A very simple example would be the case where an external variable is defined in a header file but declared in a source, thus:
C++
// prog.h
extern int GlobalValue;    // the definition, GlobalValue is a global
                           // integer, but it does not exist yet
...

// prog.cpp
int GlobalValue = 0;   // the declaration, GlobalValue is declared in
                       // this module with a value of zero
...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900