Reputation: 165
I would like to ask if this short code:
int i = 0;
has 1 operand or 2? The i
is an operand, but is 0
too? According to wikipedia, 0 shouldn't (or maybe I misunderstand). If 0 isn't operand, is it a constant or something?
If it is important, the code is in C99.
Upvotes: 0
Views: 275
Reputation: 180201
The
i
is an operand, but is0
too? According to wikipedia, 0 shouldn't (or maybe I misunderstand).
The question links to the Wikipedia page describing the term "operand" in a mathematical context. This likely factors in to your confusion about whether the 0 on the right-hand side of the =
is an operand, because in a mathematical statement of equality such as appears in the article, it is in no way conventional to consider =
an operator. It does not express a computation, so the expressions it relates are not operated upon. I.e. they do not function as operands.
You have placed the question in C context, however, and in C, =
can be used as an assignment operator. This is a bona fide operator in that it expresses a (simple) operation that produces a result from two operands, and also has a side effect on the left-hand operand. In an assignment statement such as
i = 0;
, then, both i
and 0
are operands.
With respect to ...
If 0 isn't operand, is it a constant or something?
... 0
standing on its own is a constant in C, but that has little to do with whether it serves as an operand in any given context. Being an operand is a way an expression, including simply 0
, can be used. More on that in a moment.
Now it's unclear whether it was the intent, but the code actually presented in the question is very importantly different from the assignment statement above. As the other answers also observe,
int i = 0;
is a declaration, not a statement. In that context, the i
is the identifier being declared, the 0
is used as its initializer, and just as the =
in a mathematical equation is not an operator, the =
introducing an initializer in a C declaration is not an operator either. No operation is performed here, in that no value computation is associated with this =
, as opposed to when the same symbol is used as an assignment operator. There being no operator and no operation being performed, there also are no operands in this particular line of code.
Upvotes: 1
Reputation: 42129
Since you've tagged the question as "C", one way to look at it is by reading the C standard. As pointed out by this answer, initialization (such as int i = 0
) is not an expression in itself, and by a strict reading of the standard, the =
here is not an operator according to the usage of those terms in the standard.
It is not as clear whether i
and 0
are operands, however. On one hand, the C standard does not seem to refer to the parts of the initialization as operands. On the other hand, it doesn't define the term "operand" exhaustively. For example, one could interpret section 6.3 as calling almost any non-operator part of an expression an "operand", and therefore at least the 0
would qualify as one.
(Also note that if the code was int i; i = 0;
instead, the latter i
and the 0
would definitely both be operands of the assignment operator =
. It remains unclear whether the intent of the question was to make a distinction between assignment and initialization.)
Apart from the C standard, you also refer to Wikipedia, in particular:
In computing, an operand is the part of a computer instruction which specifies what data is to be manipulated or operated on, while at the same time representing the data itself.
If we consider the context of a "computer instruction", the C code might naively be translate to assembly code like mov [ebp-4], 0
where the two operands would clearly be the [ebp-4]
(a location where the variable called i
is stored) and the 0
, which would make both i
and 0
operands by this definition. Yet, in reality the code is likely to be optimized by the compiler into a different form, such as only storing i
in a register, in which case zeroing it might become xor eax, eax
where the 0
no longer exists as an explicit operand (but is the result of the operation). Or, the whole 0
might be optimized away and replaced by some different value that inevitably gets assigned. Or, the whole variable might end up being removed, e.g., if it is used as a loop counter and the loop is unrolled.
So, in the end, either it is something of a philosophical question ("does the zero exist as an operand if it gets optimized away"), or just a matter of deciding on the desired usage of the terms (perhaps depending on the context of discussion).
Upvotes: 2
Reputation: 96166
In int i = 0;
, =
is not an operator. It's simply a part of the variable initializaton syntax. On the other hand, in int i; i = 0;
it would be an operator.
Since =
here is not an operator, there are no operands. Instead, 0
is the initializer.
Upvotes: 7