[comp.lang.c] "exit 0;" - or - "exit

braner@batcomputer.tn.cornell.edu (braner) (11/17/86)

[]

While K&R explicitly say that exit() is a function, I commonly see
the parens dropped.  Is that "legal"? Does it work on existing compilers?
And while I am asking:  how's "if (foo) {...}" vs. "if foo {...}" ?
(in the case where "foo" is a variable, say an int used as a flag.)

- Moshe Braner, Cornell Univ.

ballou@brahms (Kenneth R. Ballou) (11/17/86)

In article <1512@batcomputer.tn.cornell.edu> braner@batcomputer.UUCP (braner) writes:
>While K&R explicitly say that exit() is a function, I commonly see
>the parens dropped.  Is that "legal"? Does it work on existing compilers?

	Note that 'exit' is not a reserved word in (K&R) C.  Therefore, the 
compiler should reject the construct  exit 0;  on at least two grounds.  If 
you have not previously declared exit to be a function, it should complain 
that exit is an undefined variable.  Then it should complain because you have
two rvalues with no operator in between.

>And while I am asking:  how's "if (foo) {...}" vs. "if foo {...}" ?
>(in the case where "foo" is a variable, say an int used as a flag.)

	Perhaps I should be charitable and assume that you simply do not have 
access to either K&R or Harbison and Steele to look this up yourself, although
I find it hard to believe that Cornell does not have libraries.

K&R, page 201:  Near the bottom of the page we see

9.3 Conditional statement
	The two forms of the conditional statement are

		if ( expression ) statement
		if ( expression ) statement else statement

Harbison and Steele, page 202:  In the section titled "Conditional Statement,"
we have:

	conditional-statement ::= if-statement | if-else-statement
	if-statement ::= if '(' expression ')' statement
	if-else-statement ::=
		if '(' expression ')' statement else statement

	This very same information can be found on page 331 of H&S in the last
three lines. For a more rigorous description, you might turn to page 339 in
Appendix C, "LALR(1) grammar for C".

	I think I have made my point.  People have written books which specify
the grammar of C.  I sometimes wonder why they go through all the bother ...

--------
Kenneth R. Ballou		...!ucbvax!brahms!ballou
Department of Mathematics	ballou@brahms.berkeley.edu
University of California
Berkeley, California  94720

ark@alice.UUCP (11/18/86)

Open reply to Moshe Braner:

	exit 0;

is illegal -- exit is a function just like any other.  I am sure
you are mistaken when you say you've seen it may times.  In fact,
I strongly suspect you are confusing it with

	return 0;

which is legal C.

ron@brl-sem.ARPA (Ron Natalie <ron>) (11/18/86)

In article <331@cartan.Berkeley.EDU>, ballou@brahms (Kenneth R. Ballou) writes:
> In article <1512@batcomputer.tn.cornell.edu> braner@batcomputer.UUCP (braner) writes:
> >While K&R explicitly say that exit() is a function, I commonly see
> >the parens dropped.  Is that "legal"? Does it work on existing compilers?
> 
> 	Note that 'exit' is not a reserved word in (K&R) C.  Therefore, the 
> compiler should reject the construct  exit 0;  on at least two grounds. 

Perahps this poor soul is getting exit confused with return.  Return
does not require parentheses.  Exit in every implementation that I have
seen is indeed a function and requires "()" as a minimum.

-Ron

thomps@gitpyr.gatech.EDU (Ken Thompson) (11/18/86)

In article <1512@batcomputer.tn.cornell.edu>, braner@batcomputer.tn.cornell.edu (braner) writes:
> While K&R explicitly say that exit() is a function, I commonly see
> the parens dropped.  Is that "legal"? Does it work on existing compilers?

exit must be a function in C. However,

                             exit 0;

is legitimate Bourne shell syntax. Is it possible that you have seen this
in a shell script not a C program ?


-- 
Ken Thompson  Phone : (404) 894-7089
Georgia Tech Research Institute
Georgia Insitute of Technology, Atlanta Georgia, 30332
...!{akgua,allegra,amd,hplabs,ihnp4,seismo,ut-ngp}!gatech!gitpyr!thomps

karl@haddock.UUCP (Karl Heuer) (11/19/86)

In article <6355@alice.UUCP> ark@alice.UUCP writes:
>"exit 0;" is illegal -- exit is a function just like any other.  I am sure
>you are mistaken when you say you've seen it many times.  In fact, I
>strongly suspect you are confusing it with "return 0;" which is legal C.

Or perhaps with awk or shell scripts, where "exit 0" is legal.

Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint

braner@batcomputer.tn.cornell.edu (braner) (11/20/86)

In article <6355@alice.UUCP> ark@alice.UUCP writes:
>"exit 0;" is illegal -- exit is a function just like any other.  I am sure
>you are mistaken when you say you've seen it many times.  In fact, I
>strongly suspect you are confusing it with "return 0;" which is legal C.

Actually, I now think that what I've seen was "return n;" in main(),
which I guess has the same effect as exit(n); - or does it?

BTW: Is it necessary to free() all the malloc()ed stuff before you exit()?
(I know that is OS-dependent.  What are peoples' experiences?)

- Moshe Braner

kimcm@olamb.UUCP (Kim Chr. Madsen) (11/24/86)

In article <1512@batcomputer.tn.cornell.edu>, braner@batcomputer.tn.cornell.edu (braner) writes:
> []
> 
> While K&R explicitly say that exit() is a function, I commonly see
> the parens dropped.  Is that "legal"? Does it work on existing compilers?
> And while I am asking:  how's "if (foo) {...}" vs. "if foo {...}" ?
> (in the case where "foo" is a variable, say an int used as a flag.)
> 
> - Moshe Braner, Cornell Univ.

WHY ?

If your C-compiler (or someone else's) have the feature to make these non-standard
features - DON'T USE THEM - DON'T RELY ON THEM....

If you use all these nasty little undocumented features a specific programming
language, operating system, ...etc... has you're getting yourself into troubles!

	1) Your code will be hard to understand by others (and yourself after
	   some time)
	2) Your code is NON-STANDARD!!!
	3) Your code will be non-portable between machines/compilers.
	4) The next release of the compiler have no obligation to follow
	   previous non-documented features. So your fine 10,000 lines
	   program will not compile (in best case, worst case it will compile
	   but yields unpredictable results).

SO - stick to the standard and what is approved to be portable/documented.

						<Kim Chr. Madsen>

kimcm@olamb.UUCP (Kim Chr. Madsen) (11/24/86)

In article <6355@alice.UUCP>, ark@alice.UUCP writes:
> ....	return 0; which is legal C.

Maybe it's legal C but it's a bad habit of not using parentheses in
return and sizeof constructs! It confuses the reading of the code!

				<Kim Chr. Madsen>