Statements and expressions.

Discussion in 'C Programming' started by Martin Johansen, Feb 17, 2004.

  1. In C,

    what do you call that which is separated by semicolon?

    what is the difference between an expression and a statement?

    Thank you.
    Martin Johansen, Feb 17, 2004
    #1
    1. Advertising

  2. Martin Johansen <> scribbled the following:
    > In C,


    > 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.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "Roses are red, violets are blue, I'm a schitzophrenic and so am I."
    - Bob Wiley
    Joona I Palaste, Feb 17, 2004
    #2
    1. Advertising

  3. Martin Johansen

    pete Guest

    Joona I Palaste wrote:
    >
    > Martin Johansen <> scribbled the following:
    > > In C,

    >
    > > what do you call that which is separated by semicolon?


    Statements are terminated by semicolons.

    > > 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.


    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.

    --
    pete
    pete, Feb 17, 2004
    #3
  4. Martin Johansen

    Dan Pop Guest

    In <c0t7n3$2c6$> Joona I Palaste <> writes:

    >Martin Johansen <> scribbled the following:
    >> In C,

    >
    >> 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.


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

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Dan Pop, Feb 17, 2004
    #4
  5. Joona I Palaste <> writes:

    > An expression is anything that has a value.


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

    Martin
    Martin Dickopp, Feb 17, 2004
    #5
  6. Martin Johansen

    Chris Torek Guest

    >Joona I Palaste <> writes:
    >> An expression is anything that has a value.


    In article <>
    Martin Dickopp <> writes:
    >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.)
    --
    In-Real-Life: Chris Torek, Wind River Systems
    Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
    email: forget about it http://web.torek.net/torek/index.html
    Reading email is like searching for food in the garbage, thanks to spammers.
    Chris Torek, Feb 17, 2004
    #6
  7. Chris Torek <> writes:

    >>Joona I Palaste <> writes:
    >>> An expression is anything that has a value.

    >
    > In article <>
    > Martin Dickopp <> writes:
    >>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.


    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. :)


    --
    ,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
    / ,- ) http://www.zero-based.org/ ((_/)o o(\_))
    \ `-' `-'(. .)`-'
    `-. Debian, a variant of the GNU operating system. \_/
    Martin Dickopp, Feb 17, 2004
    #7
  8. Martin Johansen

    Jack Klein Guest

    On 17 Feb 2004 19:17:15 GMT, Chris Torek <> wrote in
    comp.lang.c:

    > >Joona I Palaste <> writes:
    > >> An expression is anything that has a value.

    >
    > In article <>
    > Martin Dickopp <> writes:
    > >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 :) )


    Gee, Chris, this is what I would call much ado about nothing, to coin
    a phrase.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Feb 18, 2004
    #8
  9. Martin Johansen wrote:

    > 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.
    E. Robert Tisdale, Feb 18, 2004
    #9
  10. Martin Dickopp <> writes:
    [...]
    > 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.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
    Schroedinger does Shakespeare: "To be *and* not to be"
    Keith Thompson, Feb 18, 2004
    #10
  11. Chris Torek <> spoke thus:

    > 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...

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Feb 19, 2004
    #11
  12. In article <c12ek9$jmj$> Christopher Benson-Manica <> writes:
    > Chris Torek <> spoke thus:
    > > 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...


    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.
    --
    dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
    home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
    Dik T. Winter, Feb 20, 2004
    #12
  13. Martin Johansen

    MJSR Guest

    "Dik T. Winter" <> wrote in message news:<>...
    > In article <c12ek9$jmj$> Christopher Benson-Manica <> writes:
    > > Chris Torek <> spoke thus:
    > > > 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...

    >
    > 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.

    --
    MJSR
    MJSR, Feb 23, 2004
    #13
  14. In article <> (MJSR) writes:
    > "Dik T. Winter" <> wrote in message news:<>...

    ....
    > > 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


    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.
    --
    dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
    home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
    Dik T. Winter, Feb 24, 2004
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neil Zanella
    Replies:
    8
    Views:
    1,169
    mfmehdi
    Oct 20, 2006
  2. Harry George
    Replies:
    6
    Views:
    363
    Bart Nessux
    Feb 23, 2004
  3. Neroku

    Concept of Statements and Expressions

    Neroku, May 10, 2006, in forum: C Programming
    Replies:
    20
    Views:
    619
  4. Vince
    Replies:
    12
    Views:
    737
    Martin Gregorie
    Jan 21, 2008
  5. John Crichton
    Replies:
    6
    Views:
    253
    John Crichton
    Jul 12, 2010
Loading...

Share This Page