Statements and expressions.

M

Martin Johansen

In C,

what do you call that which is separated by semicolon?

what is the difference between an expression and a statement?

Thank you.
 
J

Joona I Palaste

Martin Johansen said:
what do you call that which is separated by semicolon?
what is the difference between an expression and a statement?

Don't you have a C textbook?
"That which is separated by semicolons" can be a lot of things, but in
C, it is usually a statement.
A statement is the minimum unit of executable C code that can be
executed on its own. An expression is anything that has a value.
Some expressions qualify as statements (by adding a semicolon after
them), but not all statements qualify as expressions. For example,
conditionals and loops don't.
 
P

pete

Statements are terminated by semicolons.
Don't you have a C textbook?
"That which is separated by semicolons" can be a lot of things, but in
C, it is usually a statement.
A statement is the minimum unit of executable C code that can be
executed on its own. An expression is anything that has a value.
Some expressions qualify as statements (by adding a semicolon after
them),
but not all statements qualify as expressions. For example,
conditionals and loops don't.

In the term "expression statement", "expression" is an adjective.
An expression statement is a statement,
and no statements are expressions.
a = 0 is an expression.
; is a null statement
a = 0; is an expression statement

You can't do this:
if (a = 0;){}

Expressions have types and optionally, side effects.
 
D

Dan Pop

In said:
Don't you have a C textbook?
"That which is separated by semicolons" can be a lot of things, but in
C, it is usually a statement.

Unless it's a declaration or an expression in a for statement. Both of
which being fairly usual...

Dan
 
M

Martin Dickopp

Joona I Palaste said:
An expression is anything that has a value.

An expression of type `void' is also an expression, but doesn't have
a value.

Martin
 
C

Chris Torek

An expression of type `void' is also an expression, but doesn't have
a value.

It can be argued (by carefully looking past the C standards :) )
that an expression of type "void" has one possible value. If we
compare this with a single unsigned bit -- such as in a bitfield
-- which can have two different values, namely 0 and 1, we can
conclude that the single possible value of type "void" is probably
stored in *no* bits.

The next question that would arise is "what is this single value
of type void that any expression of type void has?" -- and the
answer is "it does not matter". Whatever that value is, all void
expressions have it, so there is no need to print or input them;
all we need to know is the type. Since we never need to printf
or scanf it, we do not need conversion specifiers; and although
C prohibits both:

void a, b; /* error -- objects a and b cannot have type void */
...
a = b; /* error -- cannot assign a value of type void */

one might argue that this *should* be allowed (though since a and b
use no storage, no code would be generated :) ).

Finally, to make it all work, we should initialize b first:

b = (void)0; /* or indeed (void)any_valid_expr */

which has the effect of converting the value to no bits, then assigning
no bits to the no-bit-storage-area named "b". :)

(This is not quite the way C does it, though. It might be nice
just for consistency, but note that sizeof(void) would be 0, which
brings up the whole "zero-sized objects" issue that Doug Gwyn
proposed handling for C89. He was unable to gather sufficient
interest and C ended up prohibiting zero-sized objects.)
 
M

Martin Dickopp

Chris Torek said:
It can be argued (by carefully looking past the C standards :) )
that an expression of type "void" has one possible value. If we
compare this with a single unsigned bit -- such as in a bitfield
-- which can have two different values, namely 0 and 1, we can
conclude that the single possible value of type "void" is probably
stored in *no* bits.

I like your way of thinking along the lines of "an object of n bits can
have 2 to the power n values". :) However, as you most likely are
aware, the standard explicitly states (in 6.2.5#19) that "[t]he void
type comprises an empty set of values; [...]."
The next question that would arise is "what is this single value
of type void that any expression of type void has?" -- and the
answer is "it does not matter".

IMHO, in this hypothetical language where `void' has a single value,
the value should definitely be 0. :)
 
E

E. Robert Tisdale

Martin said:
In C,

what do you call that which is separated by semicolon?

A clause.

According to the American Heritage Dictionary of the English Language:

http://www.bartleby.com/61/16/S0251600.html

semicolon

NOUN: A mark of punctuation ( ; ) used to connect
independent clauses and indicating a closer relationship
between the clauses than a period does.

clause

NOUN:1. Grammar A group of words
containing a subject and a predicate
and forming part of a compound or complex sentence.

> What is the difference between an expression and a statement?

expression

NOUN:3. Mathematics A symbol or combination of symbols
that represents a quantity or a relationship between quantities.

statement

NOUN:2. Something stated; a declaration. 6. Computer Science
An elementary instruction in a programming language.

declaration

NOUN:1. An explicit, formal announcement either oral or written.


In C, the clauses are declarations, definitions, expressions or
imperatives. Imperatives are executable statements (commands)
which change the program state.
 
K

Keith Thompson

Martin Dickopp said:
I like your way of thinking along the lines of "an object of n bits can
have 2 to the power n values". :) However, as you most likely are
aware, the standard explicitly states (in 6.2.5#19) that "[t]he void
type comprises an empty set of values; [...]."

Must be the padding bits.
 
C

Christopher Benson-Manica

Chris Torek said:
It can be argued (by carefully looking past the C standards :) )
that an expression of type "void" has one possible value.
(snipped)

Gee, you make me wish that C had left a place for the eternal
nothingness that is The Void! Although that would probably take it
out of the realm of programming and make C a philisophical
construct...
 
D

Dik T. Winter

>
> Gee, you make me wish that C had left a place for the eternal
> nothingness that is The Void! Although that would probably take it
> out of the realm of programming and make C a philisophical
> construct...

Actually in Algol 68 there was indeed one possible value for objects
of type 'void'. It even had a notation: 'empty'. So:
'void' i = 'empty';
was a perfectly legal declaration. Whether there is any sense in it
is something different.
 
M

MJSR

Dik T. Winter said:
Actually in Algol 68 there was indeed one possible value for objects
of type 'void'. It even had a notation: 'empty'. So:
'void' i = 'empty';
was a perfectly legal declaration. Whether there is any sense in it
is something different.

Not perfectly legal; the Revised Report says in 2.1.3.1(h):

h) The only "void value" is "empty". Its mode is 'void'.

{The elaboration of a construct yields a void value when no more useful
result is needed. Since the syntax does not provide for void-variables,
void-identity-declarations or void-parameters, the programmer cannot
make use of void values, except those arising from uniting {6.4 }.}

http://vestein.arb-phys.uni-dortmund.de/~wb/RR/rrTOC.html

You would have to write something like
'union'('int','void') i = 'empty' instead.

It had occurred to me when I read Chris Torek's post that the odd
treatment of void in C might follow from the odd treatment of void
in Algol 68; the latter, I suspect, may have been a consequence of
coercions (i.e., MORF and COMORF).

A more likely explanation in C is the desire to avoid zero-sized
objects, mentioned in Chris Torek's post, and a good reason for
that is expected behavior of pointer arithmetic; if void a[10];
were a legal declaration, then a+0==a+1==a+2... would seem to hold,
and that would be less desirable (to me) than treating void differently.
(If void *p=&a[0]; void *q=&a[5]; what would q-p be?)
(Or if sizeof(void) is not 0, then what would be the point?)

I suppose complaints from the compiler when you dereference a void *
might also be a good thing.
 
D

Dik T. Winter

>
> Not perfectly legal; the Revised Report says in 2.1.3.1(h):
>
> h) The only "void value" is "empty". Its mode is 'void'.
>
> {The elaboration of a construct yields a void value when no more useful
> result is needed. Since the syntax does not provide for void-variables,
> void-identity-declarations or void-parameters, the programmer cannot
> make use of void values, except those arising from uniting {6.4 }.}
>
> http://vestein.arb-phys.uni-dortmund.de/~wb/RR/rrTOC.html

I will look at the different versions of the report I have (there are at
least 6, of which 4 are preliminary). The Revised Report differs
considerably from the initial Report.
> It had occurred to me when I read Chris Torek's post that the odd
> treatment of void in C might follow from the odd treatment of void
> in Algol 68; the latter, I suspect, may have been a consequence of
> coercions (i.e., MORF and COMORF).

Offhand I think the odd treatment in Algol 68 came in the Revised
Report, but I will have a look.
> A more likely explanation in C is the desire to avoid zero-sized
> objects,

Although I think that C's void comes from the same in Algol 68, the
initial use in C was only to discard values, nothing more. Later
came its dual purpose (which does not come from Algol 68) in pointers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top