Hello, i would like to know what the serious definition of statements
and expressions is:
i know an expression are evaluated to a value, i.e:
1 == 2
5+7
foo( 1,2)
Yes exactly.
and a statement is executed:
break;
5+7;
if( foo(1,2) ){ ... }
[...]
This is a good question. One way to think of the difference is in terms
of "functional" versus "imperative" programming.
An expression is, in a sense, an implicit formulation of the result you
want, a statement is more like an explicit instruction to the computer to
do something.
int a = b + c / d;
This is a statement, that initializes a with the result of evaluating
the expression "b + c / d".
We tell the computer explicitly to initialize a. We tell it that what we
want to initialize a with is "whatever b + c / d" comes to. The computer
can work out b + c / d however it wants to. This is a much more
"implicit" way of talking to the computer.
We could achieve the same result with a series of statements:
int a = c;
a /= d;
a += b;
You could say this is a "more imperative" and "less functional"
version of the program.
Expressions sometimes have side-effects.
printf("Hello\n")
is an expression. It evaluates to 5 (provided there were no errors). But
it also has a side-effect-- while the machine is evaluating it, it also
writes the string "Hello" to the terminal.
Usually with an expression like this we aren't interested in the value,
only in the side-effect. In C you can even define "void" valued
functions. A void valued function would be useless if it weren't for the
side-effects.
In "pure functional" (or nearly pure anyway) languages, like Scheme for
example, there are no statements, only expressions.
Assembly language on the other hand is much closer to a "pure"
imperative language-- the program is a series of statements each of
which tells the computer to do something.
These are just some thoughts on the concepts of expression and
statement. You could say that an assembly language program was a series
of void-valued functions with side-effects, but that would be a slightly
odd way to look at it. The underlying idea seems to be, are we telling
the machine what to do step by step, or are we expressing the result we
want and telling the machine to go away and work it out. We see this
kind of duality reflected all over the place whenever we go near a
computer. What's known as a "function" in C is called a "procedure" in
other languages, or sometimes a "subroutine". "Function" is an implicit,
expression type of word, "procedure" and "routine" are explicit,
statement type of words. But they come to mean pretty much the same
thing. All this is probably a consequence of the "Church-Turing Thesis".
Anyway, C, like many languages, is a mix of both imperative and
functional styles; it's also a property of C that any expression can be
used as a statement (this is true of some other popular languages, but
untrue of others-- in Python for example you cannot use any expression
as a statement). But every statement is not an expression:
break;
for example is a statement but not an expression. Most of the time in C
you will see ; between statements, so that's an easy way to spot them.
In terms of the grammar of C, a program is a series of statements, and
some kinds of statements may be made up of expressions. Expressions
themselves may be recursively defined (an "arithmetic expression" for
example might be defined as something like "an expression followed by an
operator followed by another expression"). Really for the serious
definitions of what statement and expression mean in C, the C grammar is
the place to start.