what is the definition of variable

Viewed 419

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?

4 Answers

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.

This question is tagged C, but the C standard does not define “variable.” In the sense asked about, the 2018 standard uses the word mostly in non-normative notes, examples, and footnotes. The few places where it is used in normative text are in supplementary language features added later (such as threads and atomic objects), and I do not regard their uses there as firm information about the meaning.

In the standard, what are commonly thought of variables are indeed described via “objects” (regions of storage) and “identifiers” (names). In this regard, your answer is correct.

However, we do not know the context of your class. It may have included discussion of the term “variable,” and that discussion may have defined it as your teacher required for the test.

Historically, variables represented things whose values could be changed, and they were named because of that. The evolution of programming languages to have constant objects came later, giving rise to some conflict between the historic name and the current use. I expect this is part of why the C standard moved away from “variable” and toward “object” and “identifier.” Insisting on use of the term only in its historic meaning is unduly rigid unless it was an explicit definition presented in the course.

ISO/IEC9899:2017 (N2176) §6.7/5:

A declaration specifies the interpretation and attributes of a set of identifiers. A definition of an identifier is a declaration for that identifier that:
— for an object, causes storage to be reserved for that object;

So for

Give a definition of a variable

answering

int foo;

would be sufficive.

From the Wikipedia about variable & its definition :

In computer programming, a variable or scalar is a storage location (identified by a memory address) paired with an associated symbolic name (an identifier), which contains some known or unknown quantity of information referred to as a value. The variable name is the usual way to reference the stored value, in addition to referring to the variable itself, depending on the context.

This separation of name and content allows the name to be used independently of the exact information it represents. The identifier in computer source code can be bound to a value during run time, and the value of the variable may thus change during the course of program execution.

For e.g Here

int input = 10; /* definition of variable input */ 

In above initialization(declaration with assignment) of variable input, there is some memory is reserved for input at compile time itself somewhere in stack section of primary memory RAM. And Here

const int input = 10; /* definition of variable input */ 

input is a constant variable i.e can't change it directly but still it's a variable i.e it can contain something, hence it occupies memory space in the primary memory RAM..

Related