Home > OS >  What is the difference between a name and a variable in C
What is the difference between a name and a variable in C

Time:10-16

According to C ISO Draft (2020) 6.1 (Basics) :

A name is a use of an identifier (5.10), operator-function-id (12.6), literal-operator-id (12.6.8), conversion-function-id (11.4.7.2), or template-id (13.3) that denotes an entity or label (8.7.5, 8.2).

Every name that denotes an entity is introduced by a declaration. Every name that denotes a label is introduced either by a goto statement (8.7.5) or a labeled-statement (8.2).

A variable is introduced by the declaration of a reference other than a non-static data member or of an object. The variable’s name, if any, denotes the reference or object

A name can denote a entity, so it can denote an object and is introduced by a declaration in this case. A variable may be introduced by the declaration of an object, and in this case denotes the object. If I'm not misunderstading, the definitions of name and variable look very similar. What is the difference between a name and a variable? (Particularly in the case that there is an intersection, e.g when they denote an object)

CodePudding user response:

What is the difference between a name and a variable?

The most obvious (based upon your first quote) difference is that a name is more general than a variable. Every variable has a name, but not every name is of a variable. There are also names of functions, classes, operators, and templates. (This is not intended to be a complete list.)

Less obviously, a name is more specific than a variable, in the sense that a variable's name is only one aspect of the variable. I think your confusion comes from the fact that the name of a variable is necessarily introduced at the same time as the variable itself (your second and third quotes), and they go out of scope at the same time. There is no temporal separation of the two.

(Particularly in the case that there is an intersection, e.g when they denote an object)

I see this as somewhat philosophical. Let's take a similar situation: what is the difference between your name and yourself? Are you more than just a name? And yet, to identify you, others use your name.

It's not that different for variables. A variable has a name. That name is used to refer to the variable, often treated as being the variable. And yet, a variable is more than just a name; it also has a type and a value. The name does not intrinsically determine the type and value, yet out of convenience it is often used that way when talking about code. (That's not a bad thing, in the vast majority of cases.)

CodePudding user response:

I would say that a variable has two things: a name and a value.
As far as names are concerned: variables can have names, but also functions, classes, ...

So a variable is an element, belonging to the C world (and lots of other programming languages).
A name is a property of much elements, such as variables, but also other ones.

  • Related