Is There Any Difference Between Declared and Defined Variable

What is the difference between a definition and a declaration?

A declaration introduces an identifier and describes its type, be it a type, object, or function. A declaration is what the compiler needs to accept references to that identifier. These are declarations:

extern int bar;
extern int g(int, int);
double f(int, double); // extern can be omitted for function declarations
class foo; // no extern allowed for type declarations

A definition actually instantiates/implements this identifier. It's what the linker needs in order to link references to those entities. These are definitions corresponding to the above declarations:

int bar;
int g(int lhs, int rhs) {return lhs*rhs;}
double f(int i, double d) {return i+d;}
class foo {};

A definition can be used in the place of a declaration.

An identifier can be declared as often as you want. Thus, the following is legal in C and C++:

double f(int, double);
double f(int, double);
extern double f(int, double); // the same as the two above
extern double f(int, double);

However, it must be defined exactly once. If you forget to define something that's been declared and referenced somewhere, then the linker doesn't know what to link references to and complains about a missing symbols. If you define something more than once, then the linker doesn't know which of the definitions to link references to and complains about duplicated symbols.


Since the debate what is a class declaration vs. a class definition in C++ keeps coming up (in answers and comments to other questions) , I'll paste a quote from the C++ standard here.

At 3.1/2, C++03 says:

A declaration is a definition unless it [...] is a class name declaration [...].

3.1/3 then gives a few examples. Amongst them:


[Example: [...]
struct S { int a; int b; }; // defines S, S::a, and S::b [...]
struct S; // declares S
—end example

To sum it up: The C++ standard considers struct x; to be a declaration and struct x {}; a definition. (In other words, "forward declaration" a misnomer, since there are no other forms of class declarations in C++.)

Thanks to litb (Johannes Schaub) who dug out the actual chapter and verse in one of his answers.

Is there any difference between declared and defined variable

A let or const variable can only be declared once - that is, when you have let <variableName> in a scope, you have declared <variableName> in that scope, and cannot declare it again in that scope.

From the previously linked question:

When there's assignment, the right-hand side is parsed first; if the right-hand side throws an error, it never gets to the left-hand side, and the variable declared with let never gets properly initialized; it'll stay in the demilitarized zone / temporal dead zone forever

You can't re-declare a variable that's already been declared, even though the attempted assignment during initialization threw an error.

But on line 4, x=3 should do a proper assignment and it should remove x from TDZ. But that also fails. I fail to understand that

After a variable has been initialized (for example, the let x runs), it can be assigned to. But just like you can't assign to a variable before its let initialization, you also can't assign to a variable later, when its initialization did not complete successfully:

x = 'foo';let x = 'bar';

What is the difference between declaration and definition in Java?

The conceptual difference is simple:

  • Declaration: You are declaring that something exists, such as a class, function or variable. You don't say anything about what that class or function looks like, you just say that it exists.

  • Definition: You define how something is implemented, such as a class, function or variable, i.e. you say what it actually is.

In Java, there is little difference between the two, and formally speaking, a declaration includes not only the identifier, but also it's definition. Here is how I personally interpret the terms in detail:

  • Classes: Java doesn't really separate declarations and definitions as C++ does (in header and cpp files). You define them at the point where you declare them.

  • Functions: When you're writing an interface (or an abstract class), you could say that you're declaring a function, without defining it. Ordinary functions however, are always defined right where they are declared. See the body of the function as its definition if you like.

  • Variables: A variable declaration could look like this:

    int x;

    (you're declaring that a variable x exists and has type int) either if it's a local variable or member field. In Java, there's no information left about x to define, except possible what values it shall hold, which is determined by the assignments to it.

Here's a rough summary of how I use the terms:

abstract class SomeClass {                // class decl.
// \
int x; // variable decl. |
// |
public abstract void someMethod(); // function decl. |
// |
public int someOtherMethod() { // function decl. |
// | class
if (Math.random() > .5) // \ | def.
return x; // | function definition |
else // | |
return -x; // / |
// |
} // |
} // /

Difference between variable declaration and definition in C. When does the memory gets allocated?

Like in some articles or forums I read they say
Int x; ( x is declared)
Somewhere it's written
Int x; ( x is defined).

Actually the int x; declares and defines it at the same time so both are valid but incomplete.

The declaration shows the compiler the type of variable without creating it.

extern int x; // <- this is declaration

Definition creates the the object, if the object was declared before its definition must match the declaration.

extern int x;
int x;

Is valid but

extern int x;
double x;

is not.

Variable declaration vs definition

Basically, yes you are right.

extern int x;  // declares x, without defining it

extern int x = 42; // not frequent, declares AND defines it

int x; // at block scope, declares and defines x

int x = 42; // at file scope, declares and defines x

int x; // at file scope, declares and "tentatively" defines x

As written in C Standard, a declaration specifies the interpretation and attributes of a set of identifiers and a definition for an object, causes storage to be reserved for that object. Also a definition of an identifier is a declaration for that identifier.

Difference between variable declaration and definition in Swift

After doing much searching across the web for legitimate explanations, I have seemed to have found an answer:

The problem is that the two terms overlap to some extent. Definitions also serve as declarations, because they inject an identifier of a certain type to a scope. However, a declaration isn't a definition because it doesn't entail storage allocation for the declared object. To add to the confusion, the semantics of definitions and declarations is slightly different when applied to types and functions, as I will show momentarily. So let's look at a more detailed analysis of these two terms.

Here is the article: Declarations and Definitions.

The article gives further explanation and examples.

Confusion between declaration and definition of a variable in C

This isn't something you see too much in C, but it works like this:

In a header file, you can have a line like this:

extern int x; //declaration

Because of the extern modifier, this tells the compiler that there is an int named x somewhere. The compiler doesn't allocate space for it - it just adds int x to the list of variables you can use. It'll only allocate space for x when it sees a line like this:

int x; //definition

You can see that because only the int x; line changes your executable, you can have as many extern int x; lines as you feel like. As long as there's only int x; line, everything will work like you want it to - having multiple declarations doesn't change a thing.

A better example comes from C++ (sorry if this is a C-only question - this applies to structs as well, but I don't know the syntax off the top of my head):

class Pineapple; //declaration

Pineapple* ptr; //this works
Pineapple pine; //this DOES NOT work

This declaration tells the compiler that there's a class called "Pineapple". It doesn't tell us anything about the class (how big it is, what its members are). We can use pointers to Pineapples now, but we can't yet have instances - we don't know what makes up a Pineapple, so we don't know how much space an instance takes up.

class Pineapple
{
public:
int ounces;
char* name;
}; //definition

Pineapple* ptr; //still works
Pineapple pine; //this works now too!
//we can even get at member variables, 'cause we know what they are now:
pine.ounces = 17;

After a definition, we know everything about the class, so we can have instances, too. And like the C example, you can have multiple declarations, but only one definition.

Hope this helps!



Related Topics



Leave a reply



Submit