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
z = y + (x = 5 + j) * 3;
(which could be valid C), then it is clearer that the assignment tox
is part of a bigger assignment expression, which is also a statement by virtue of the semicolon terminating it. – Inoffensive