[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[connect24h:6750] Re: プログラムの稼動監視ツール



おまけ

http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c Answers to Frequently Asked Questions (FAQ List) より:

1.25b:	What's the right declaration for main()?
	Is void main() correct?

A:	See questions 11.12a to 11.15.  (But no, it's not correct.)

- snip -

11.12a:	What's the correct declaration of main()?

A:	Either int main(), int main(void), or int main(int argc,
	char *argv[]) (with alternate spellings of argc and *argv[]
	obviously allowed).  See also questions 11.12b to 11.15 below.

	References: ISO Sec. 5.1.2.2.1, Sec. G.5.1; H&S Sec. 20.1 p.
	416; CT&P Sec. 3.10 pp. 50-51.

11.12b:	Can I declare main() as void, to shut off these annoying
	"main returns no value" messages?

A:	No.  main() must be declared as returning an int, and as
	taking either zero or two arguments, of the appropriate types.
	If you're calling exit() but still getting warnings, you may
	have to insert a redundant return statement (or use some kind
	of "not reached" directive, if available).

	Declaring a function as void does not merely shut off or
	rearrange warnings: it may also result in a different function
	call/return sequence, incompatible with what the caller (in
	main's case, the C run-time startup code) expects.

	(Note that this discussion of main() pertains only to "hosted"
	implementations; none of it applies to "freestanding"
	implementations, which may not even have main().  However,
	freestanding implementations are comparatively rare, and if
	you're using one, you probably know it.  If you've never heard
	of the distinction, you're probably using a hosted
	implementation, and the above rules apply.)

	References: ISO Sec. 5.1.2.2.1, Sec. G.5.1; H&S Sec. 20.1 p.
	416; CT&P Sec. 3.10 pp. 50-51.

11.13:	But what about main's third argument, envp?

A:	It's a non-standard (though common) extension.  If you really
	need to access the environment in ways beyond what the standard
	getenv() function provides, though, the global variable environ
	is probably a better avenue (though it's equally non-standard).

	References: ISO Sec. G.5.1; H&S Sec. 20.1 pp. 416-7.

11.14:	I believe that declaring void main() can't fail, since I'm
	calling exit() instead of returning, and anyway my operating
	system ignores a program's exit/return status.

A:	It doesn't matter whether main() returns or not, or whether
	anyone looks at the status; the problem is that when main() is
	misdeclared, its caller (the runtime startup code) may not even
	be able to *call* it correctly (due to the potential clash of
	calling conventions; see question 11.12b).

	It has been reported that programs using void main() and
	compiled using BC++ 4.5 can crash.  Some compilers (including
	DEC C V4.1 and gcc with certain warnings enabled) will complain
	about void main().

	Your operating system may ignore the exit status, and
	void main() may work for you, but it is not portable and not
	correct.

11.15:	The book I've been using, _C Programing for the Compleat Idiot_,
	always uses void main().

A:	Perhaps its author counts himself among the target audience.
	Many books unaccountably use void main() in examples, and assert
	that it's correct.  They're wrong.

11.16:	Is exit(status) truly equivalent to returning the same status
	from main()?

A:	Yes and no.  The Standard says that they are equivalent.
	However, a return from main() cannot be expected to work if
	data local to main() might be needed during cleanup; see also
	question 16.4.  A few very old, nonconforming systems may once
	have had problems with one or the other form.  (Finally, the
	two forms are obviously not equivalent in a recursive call to
	main().)

	References: K&R2 Sec. 7.6 pp. 163-4; ISO Sec. 5.1.2.2.3.

----
こがよういちろう

--[PR]------------------------------------------------------------------
■□ 蠍座・・・恋愛運 10 好みの異性との出会いがありそうな日。 □■
■□ かに座・・仕事運  6 金運は波乱の様相を呈しそうな一日。  □■
■□ おとめ座・金銭運  9 突然のお小遣いにラッキーな驚き!   □■
 
 mypopで毎日お届け => http://click.freeml.com/ad.php?id=673165
------------------------------------------------------------------[PR]--
<GMO GROUP> Global Media Online  www.gmo.jp