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

Re: Hello World doesn't work



On Sat, 28 Nov 1998, Hamish Moffatt wrote:

> On Sat, Nov 28, 1998 at 09:03:39AM +0100, Ole J. Tetlie wrote:
> > It can also be 'int main(void)', which is equivalent to 'main()'.
> 
> Hmmm. I was told that 'int func(void)' means it takes no parameters,
> while 'int func()' means it could take any.

So if your function takes no arguments (as in the original example), then
it is better to prototype it as (void), so the compiler flags any attempt
to pass it arguments.
 
> Some example source is below. gcc complains about the call to test2
> (too many arguments) but not to test1.
> 
> 
> #include <stdio.h>

int test1(int i);
int test2(void);

are the correct prototypes

> int test1()
int test1(int i)
> {
>     printf("test1\n");
>     return 0;
> }
> 
> int test2(void)
> {
>     printf("test2\n");
>     return 0;
> }
> 
> int main(void)
> {
>     int i = 3;
> 
>     test1(i);
>     test2(i);
> 
>     return 0;
> }

gcc will now warn that i is not used in test1, and that you are passing
too many arguments to test2. This is correct.

Matthew

-- 
Elen sila lumenn' omentielvo

Steward of the Cambridge Tolkien Society
Selwyn College Computer Support
http://www.geocities.com/Area51/Chamber/8841/
http://www.cam.ac.uk/CambUniv/Societies/tolkien/
http://pick.sel.cam.ac.uk/


Reply to: