Is variable assignment a statement or expression?
Asked Answered
J

2

10

I am familiar that statements do something and that expressions are a "collection of symbols that make up a quantity" (What is the difference between an expression and a statement in Python?). My question is: when you assign a value to a variable is that assignment a statement or an expression?

For example (in C):

int x = 5;
Jessabell answered 23/9, 2017 at 1:25 Comment(3)
Note that if you have z = y + (x = 5 + j) * 3; (which could be valid C), then it is clearer that the assignment to x is part of a bigger assignment expression, which is also a statement by virtue of the semicolon terminating it.Inoffensive
That is a definition with initialization. It is neither an assignment nor a statement.Venus
I think this confusion comes from the fact that people learn mathematics terminology before they learn programming terminology.Tenth
S
14

Well, when you say

int x = 5;

that's a declaration, that happens to include an initialization.

But if you say

x = 5

that's an expression. And if you put a semicolon after it:

x = 5;

now it's a statement.

Expression statements are probably the most common type of statement in C programs. An expression statement is simply any expression, with a semicolon after it. So there are plenty of things that might look like some other kind of statement, that are really just expression statements. Perhaps the best example is the classic

printf("Hello, world!\n");

Many people probably think of this as a "print statement". But it's actually just another expression statement: a simple expression consisting of a single function call

printf("Hello, world!\n")

again followed by a semicolon.

Syzygy answered 23/9, 2017 at 1:30 Comment(3)
So if what separates a statement and an expression is just a semi-colon, then what is the purpose in differentiating the two?Jessabell
@DarienSpringer For ordinary users of the language, there may not be much purpose. The distinction is very intersting to compiler writers and to students of programming language theory. It's also important to understand that expressions crop up in places other than expression statements. For example, the initializer in a declaration is an expression, the condition in while() and other loops is an expression, and subexpressions crop up in bigger expressions, as discussed in this question.Syzygy
@DarienSpringer For such questions it is good to read the standard first.Plantagenet
I
0

Roughly speaking, in C;

  • A statement is a section of code that produces an observable effect;
  • An expression is a code construct which accesses at least one data item, performs some operation of the result of that access (or those accesses), and produces at least one result. An expression may be composed of (smaller) expressions.

An expression statement is a statement that has the single purpose of evaluating an expression - although that expression may be arbitrarily complex (including composed from other expressions). There are other types of statements such as jump statements (e.g. goto), label statements (e.g. the target of a goto), selection statements which make decisions (e.g an switch, if), and iteration statements (e.g. loops), etc

A declaration like;

 int x = 5;

defines the variable named x and initialises it with the expression 5. 5 is a literal value.

If x has been previously declared,

 x = 5;

is a simple expression statement - its sole purpose is evaluating an assignment expression. The 5, again, is a literal with value 5. x = 5 is therefore an expression, which assigns x to have the value 5. The result is the value 5, which is why the expression statement

 printf("%d\n", (x = 5));

both assigns x to have the value 5 and prints the value 5

Illume answered 23/9, 2017 at 5:34 Comment(1)
Counterexamples: 1; is a statement. It does not produce an observable effect. Hence an implementation can remove it according to the abstract machine. Which is mostly about observable effects. sizeof(int) is an expression. It does not access anything. (void)5 is an expression which does not produce a result. Similar for all calls to void functions. And labels are not statements (see 6.8.1).Plantagenet

© 2022 - 2024 — McMap. All rights reserved.