what is the definition of variable [closed]

  • A+
Category:Languages

On a test I was asked the question

Give a definition of a variable

I answered:

A named location in memory.

The teacher marked my answer as wrong and said I need to add that variables can change.

I don’t think the teacher is correct here, as “constants” are an example of a variable that cannot be changed, and the question only asked for a general definition of the term.

What is the real definition?

 


The C language does not define such a concept as "variable", so in the context of C, the answer is that it's undefined, or defined by whoever is writing about/discussing C and using it to mean something specific to their context.

The closest thing to "variable" in C is an "object", and it is defined in 3.5:

1 object
region of data storage in the execution environment, the contents of which can represent values

2 NOTE When referenced, an object may be interpreted as having a particular type; see 6.3.2.1.

In C some objects are const-qualified, which may disqualify them from being "variables" in the eyes of some/many people using the language. On the other hand, const-qualified objects do not behave as constants that can be used in constant expressions; rather, they're like all other objects, except that any attempt to modify their values produces undefined behavior.

Note that the word "variable" does appear a number of times in the standard, mostly in the context of "variable arguments" (stdarg.h) or "variable length arrays"/"variably modified types". However it is also used informally in a number of examples and notes, and some normative text, presumably to refer to objects, but without ever being defined.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: