C has two different notions: expressions and statements. Check by reading some C standard like n1570. Notice that expressions are statements, but some statements (e.g. while
, switch
, goto
...) are not expressions.
Some languages (such as Ocaml, Scheme, Lisp, ....) have only expressions (and don't have any kind of statements). In such languages the sequence needs only one construct. Notice for example that in Ocaml, its ;
is like C comma operator, and that in Scheme begin
is for sequence constructions (but accepts many operands). Of course you should read SICP if you didn't yet.
The comma operator is making (like the ternary conditional operator, or the binary +
) a composite expression (made of operands, which are simpler sub-expressions).
The semi-colon separates two statements inside statement sequences (which appear in blocks). Of course you could (but this is not readable) separate expressions by commas to make them a statement; but you could (and this is more readable) make a sequence of expression statements.
Both the comma operator and the semicolon can be used to separate statements.
This is wrong: while(i<10) {i++;}, i
is not a valid C statement -but both left and right of ,
are valid statements (the right one, i
, being an expression statement). The GCC and Clang compilers accept as an extension statement expressions (for some reason, that useful feature exists since more than a dozen of years but has not been standardized).
(I am not sure of that, but I heard that B, the ancestor of C, had just expressions; so its while
"statement" evaluated into -1 when used as an expression; I could be wrong ....)
I would prefer C to have only expressions, with some of them (like while
) being of void
type. But this is not how the programming language evolved and is now defined. I would also prefer statement expressions and computed goto
-s (and some other extensions provided by GCC) to be part of the C standard. I don't know why it is not so! Part of the reason is that being member of the C standardization committee is a lot of work (nearly a full time job) and lobbying and costs a lot -notably in travels- (and I guess that GCC folks had not enough money for that, when -in the previous century- they introduced their C extensions).
BTW, after reading the Dragon Book and studying deeply the C11 standard, you might design a C-like language which has only expressions (that is, which mixes statements into expressions like I wish C did) and implement it in your toy compiler (using some existing code generation library like libgccjit or LLVM or compiling it into C). That could make an interesting semester student project (at master's level). Look into academic languages like Terra or Cyclone for inspiration. You could also quite easily (semester of work) patch GCC or Clang to add a new construct: -let call it "the void statement expression"- where void
followed by some simple non-expression statement or block is a new valid expression of type void
(so void while(i<10) {i++;}, i
would be a valid expression). Having that extension accepted by the GCC or Clang community is a different story (today, both GCC and Clang communities are unfriendly to new language extensions).