[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]
Re: [LI] C Programming problem
SHRIDHAR N. DAITHANKAR forced the electrons to say:
> Does a C program need to return a value from main? C++ forces it, that's
> OK, but so far I have not experienced any problem for main(){}(i.e.
> without any return parameters). Do I need those return parameters in a C
> program?
>
> Bye
> Shridhar
Yes. It does.
main() works because ANSI C currently assumes a function to by default return
an int. And the empty parameter list also works because ANSI C is compatible
with the old K&R C.
Here is an excerpt from the C FAQ:
Question 11.12
Can I declare main as void, to shut off these annoying ``main returns
no value'' messages?
_________________________________________________________________
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: ANSI Sec. 2.1.2.2.1, Sec. F.5.1
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
Binand
--
#include <stdio.h> | Binand Raj S.
char *p = "#include <stdio.h>%cchar *p = %c%s%c; | This is a self-
int main(){printf(p,10,34,p,34,10);return 0;}%c"; | printing program.
int main(){printf(p,10,34,p,34,10);return 0;} | Try it!!
--------------------------------------------------------------------
The Linux India Mailing List Archives are now available. Please search
the archive at http://lists.linux-india.org/ before posting your question
to avoid repetition and save bandwidth.