[comp.lang.c] COMMA separated expresses

dchen@oliveb.UUCP (06/25/87)

Why did my C compiler(came with 4.3bsd) complaint the "syntax error"
with the following COMMA separated express statements in a function:

funct()
{
	int i = 0;

	if ( i == 0 )
		printf("A\n"), return; /* syntax error here */
	printf("END\n");
}

But it works fine with

	if ( i == 0 )
		printf("A\n"), exit(1);	/* compile OK */

Could someone point this out ? It seems to me that rule 7.15 Comma
operator, on page 192 of C Programming Language doesn't state much
on this.

Dennis Chen
{allegra, hplabs, idi, ihnp4, sun}!oliveb!dchen

guy%gorodish@Sun.COM (Guy Harris) (06/25/87)

> Why did my C compiler(came with 4.3bsd) complaint the "syntax error"
> with the following COMMA separated express statements in a function:
...
> 		printf("A\n"), return; /* syntax error here */
> 
> Could someone point this out ? It seems to me that rule 7.15 Comma
> operator, on page 192 of C Programming Language doesn't state much
> on this.

Yes, it does.  It states that a "comma-expression" is an
"expression", a comma, and another "expression".  "return", however,
is NOT an "expression", it's a "statement".  See p. 203.
	Guy Harris
	{ihnp4, decvax, seismo, decwrl, ...}!sun!guy
	guy@sun.com

ron@topaz.rutgers.edu (Ron Natalie) (06/25/87)

The syntax of comma operator is
	expr , expr

RETURN is a control keyword  not an expression.  You can't say things like

	x = return;

Exit is a function (it never returns, but C doesn't know that generally).
Functions are a simple form of expression, hence you can say

	x = exit(19)

or use it in either operand of a comman operator.

-Ron

nagel@iris.ucdavis.edu (Mark Nagel) (06/25/87)

In article <1601@oliveb.UUCP> dchen@oliveb.UUCP (Dennis T. Chen) writes:
>Why did my C compiler(came with 4.3bsd) complaint the "syntax error"
>with the following COMMA separated express statements in a function:
>
>funct()
>{
>	int i = 0;
>
>	if ( i == 0 )
>		printf("A\n"), return; /* syntax error here */
>	printf("END\n");
>}
>
>But it works fine with
>
>	if ( i == 0 )
>		printf("A\n"), exit(1);	/* compile OK */
>
>Could someone point this out ? It seems to me that rule 7.15 Comma
>operator, on page 192 of C Programming Language doesn't state much
>on this.
>
>Dennis Chen

The first example has an expression (a function) comma separated with a
statement.  The comma operator is supposed to evaluate to the rightmost
expression.  Since the rightmost item here is not an expression, you get
the problem.  The second example illustrates this -- exit is a function,
thus an expression.




--- Mark Nagel
>>> nagel@iris.ucdavis.edu					ARPA
>>> mdnagel@ucdavis						BITNET
>>> ..!{ucbvax,sdcsvax,lll-crg}!ucdavis!iris!nagel		UUCP

michaud@decvax.UUCP (Jeff Michaud) (06/26/87)

> foo()
> {
>     if( x == 0 )
> 	func(), return;
> }

	The compiler should complain about the following because
	the "," operator works with expressions, and "return"
	is NOT an expression (ie. it doesn't have a value).

> ...
>	func(), exit();
> ...

	is OK because "exit()" is a function call and therefore
	has a value.  (though I am willing to guess that it would
	be invalid if "exit()" was defined as a function returning
	"void".

				Jeff Michaud
				michaud@decvax.dec.com
				michaud@decvax.UUCP

	Disclaimer....

smvorkoetter@watmum.UUCP (06/26/87)

In article <1601@oliveb.UUCP> dchen@oliveb.UUCP (Dennis T. Chen) writes:
]Why did my C compiler(came with 4.3bsd) complaint the "syntax error"
]with the following COMMA separated express statements in a function:
]
]funct()
]{
]	int i = 0;
]
]	if ( i == 0 )
]		printf("A\n"), return; /* syntax error here */
]	printf("END\n");
]}
]
]But it works fine with
]
]	if ( i == 0 )
]		printf("A\n"), exit(1);	/* compile OK */
]
]Could someone point this out ? It seems to me that rule 7.15 Comma
]operator, on page 192 of C Programming Language doesn't state much
]on this.
]
]Dennis Chen
]{allegra, hplabs, idi, ihnp4, sun}!oliveb!dchen


`return' is not an expression, it is a statement.  `exit' on the other
hand is a function (which never returns, but that is a different story),
and is thus an expression.

crowl@rochester.arpa (Lawrence Crowl) (06/30/87)

In article <1601@oliveb.UUCP> dchen@oliveb.UUCP (Dennis T. Chen) writes:
>Why did my C compiler(came with 4.3bsd) complaint the "syntax error" ...

Dozens of people respond:
>Because `return' is a statement and not an expression.

PLEASE MAIL RESPONSES LIKE THIS.

There must be a thousand readers of this newsgroup that can answer questions
like this.  If everyone posted an answer to an individual instead of replying
through mail, this news group would be useless.  Post only that which is of
general interest to everyone.  The answers to this type of question are of
interest to those that asked, and few others.
-- 
  Lawrence Crowl		716-275-5766	University of Rochester
			crowl@rochester.arpa	Computer Science Department
 ...!{allegra,decvax,seismo}!rochester!crowl	Rochester, New York,  14627

guy%gorodish@Sun.COM (Guy Harris) (06/30/87)

> There must be a thousand readers of this newsgroup that can answer questions
> like this.

Unfortunately, there may also be a thousand readers of this newsgroup
who might *ask* questions like this; if posting rather than mailing
the answer can give somebody else the answer *before* they ask the
question, then it may be worth it.
	Guy Harris
	{ihnp4, decvax, seismo, decwrl, ...}!sun!guy
	guy@sun.com