Newbie: declaring the "main" function 
Author Message
 Newbie: declaring the "main" function

Hi!  I've seen a couple of different basic layouts for the "main"
function in C, including this one:

______

#include <stdio.h>

int main()
{

/* Stuff that does something. */

return;

Quote:
}

______

...but I've also seen just "main()" (less the "int" designation).  As
a general rule of thumb, is it a good programming practice to include
"int" -- or completely optional.

Thanks in advance for your responses,

--Jay



Fri, 07 Oct 2005 07:09:43 GMT  
 Newbie: declaring the "main" function

Quote:

> int main()
> {

> /* Stuff that does something. */

> return;

You should actually return a value, probably 0 or EXIT_SUCCESS.

Quote:
> }
> ...but I've also seen just "main()" (less the "int" designation).  As
> a general rule of thumb, is it a good programming practice to include
> "int" -- or completely optional.

The `int' keyword is required in the latest C standard, so I
recommend that you include it.
--
"...what folly I commit, I dedicate to you."
--William Shakespeare, _Troilus and Cressida_


Fri, 07 Oct 2005 07:16:58 GMT  
 Newbie: declaring the "main" function

wrote in comp.lang.c:

Quote:
> Hi!  I've seen a couple of different basic layouts for the "main"
> function in C, including this one:

> ______

> #include <stdio.h>

> int main()
> {

> /* Stuff that does something. */

> return;

Actually it is an extremely bad idea to define a program with a return
type of int and then not return an int.

Quote:
> }
> ______

> ...but I've also seen just "main()" (less the "int" designation).  As
> a general rule of thumb, is it a good programming practice to include
> "int" -- or completely optional.

> Thanks in advance for your responses,

> --Jay

Once upon a time C had something called "implicit int".  That meant
writing:

   main()

...meant exactly the same thing as writing:

   int main()

The latest version of the C standard, approved in October 1999, has
removed implicit int from the language.  Even though there are not
many of them available yet, a conforming C compiler must issue a
diagnostic for the definition of main() or any other function without
an explicitly stated return type.

Also, although not really topical here, implicit int is illegal in C++
as well.

So for proper coding, and particularly to avoid problems with newer
compilers, always implicitly define main() as returning an int.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq



Fri, 07 Oct 2005 07:25:39 GMT  
 Newbie: declaring the "main" function

Quote:

> I've seen a couple of different basic layouts
> for the "main" function in C, including this one:

>    int main(int argc, char* argv[]) {

>      /* Stuff that does something. */

>      return 0;
>      }

> But I've also seen just "main()" (less the "int" designation).
> As a general rule of thumb,
> is it a good programming practice to include "int" --
> or completely optional.

Function main(int, char**) *always* returns an int!


Fri, 07 Oct 2005 07:19:48 GMT  
 Newbie: declaring the "main" function
On Sun, 20 Apr 2003 16:19:48 -0700, "E. Robert Tisdale"

Quote:

> > I've seen a couple of different basic layouts
> > for the "main" function in C, including this one:

> >       int main(int argc, char* argv[]) {

> >         /* Stuff that does something. */

> >         return 0;
> >         }

> > But I've also seen just "main()" (less the "int" designation).
> > As a general rule of thumb,
> > is it a good programming practice to include "int" --
> > or completely optional.

> Function main(int, char**) *always* returns an int!

Good answer, wrong question.

The OP asked whether there was a reason to code "int main()" as
opposed to just "main()", not what the return type of main() is.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq



Fri, 07 Oct 2005 08:04:38 GMT  
 Newbie: declaring the "main" function

Quote:


>> I've seen a couple of different basic layouts
>> for the "main" function in C, including this one:

>>     int main(int argc, char* argv[]) {

>>       /* Stuff that does something. */

>>       return 0;
>>       }

>> But I've also seen just "main()" (less the "int" designation).
>> As a general rule of thumb,
>> is it a good programming practice to include "int" --
>> or completely optional.

> Function main(int, char**) *always* returns an int!

I've been wondering about the ** for a while and think now is a good
time to ask.

What exactly does it do? char pointer to the contents of an array (as in
the above example)?

When would you use it?



Fri, 07 Oct 2005 07:59:57 GMT  
 Newbie: declaring the "main" function

in comp.lang.c:

Quote:



> >> I've seen a couple of different basic layouts
> >> for the "main" function in C, including this one:

> >>     int main(int argc, char* argv[]) {

> >>       /* Stuff that does something. */

> >>       return 0;
> >>       }

> >> But I've also seen just "main()" (less the "int" designation).
> >> As a general rule of thumb,
> >> is it a good programming practice to include "int" --
> >> or completely optional.

> > Function main(int, char**) *always* returns an int!

> I've been wondering about the ** for a while and think now is a good
> time to ask.

> What exactly does it do? char pointer to the contents of an array (as in
> the above example)?

> When would you use it?

char 'x';
char *cp = &x;
char **cpp = &cp;

A pointer to character or a pointer to any other type is an object in
its own right.  It occupies storage and it has an address.  Therefore
you can define a pointer value and initialize it to that address,
creating a pointer to pointer to type.

As for when one would use it, two examples in the following program:

========
#include <stdio.h>
#include <stdlib.h>

char *some_strings [] =
{
   "String One",
   "Strint Two",
   "string Three"

Quote:
};

void show_pointer_to_pointer(char **cpp, int n)
{
   printf("%s\n", cpp [n]);

Quote:
}

void other_notation(char *cp[], int n)
{
        printf("%s\n", cp[n]);

Quote:
}

void alloc_mem(char **cpp, size_t n)
{
        *cpp = malloc(n);

Quote:
}

int main(void)
{
        char *my_ptr = NULL;
        show_pointer_to_pointer(some_strings, 1);
        other_notation(some_strings, 2);
        alloc_mem(&my_ptr, 10);
        printf("my_ptr = %p\n", (void *)my_ptr);
        free(my_ptr);
        return 0;

Quote:
}

========

Notice that C (and C++) allows the use of [] in function prototypes,
implying that you can pass arrays, but that is deceptive.  You can't
really pass an array, its name is converted to a pointer to its first
element.

That is why the first two functions above are actually equivalent,
because although some_strings is defined as a array of pointers to
character, what gets passed is a pointer to the first element of
some_strings.  Since the elements of some_strings are pointers to
char, what gets passed is a pointer to pointer to char.  This is true
regardless of whether the function prototype and/or definition are
written with char ** or char *[] notation.

I personally do not like the [] notation for passing to functions (for
the first level of [], that is).

The other reason for passing a pointer to pointer to some type to a
function is because you want the function to modify the caller's
pointer.  Pointers themselves are passed by value the same as anything
else.  If alloc_mem() had been written:

void alloc_mem(char *cp, size_t n)
{
        cp = malloc(n);

Quote:
}

...and called:

        alloc_mem(my_ptr, 10);

...then alloc_mem() would have received a copy of main's my_ptr.  It
would have allocated memory to its copy, which would then disappear
when the function returned, creating a memory leak as there would no
longer be a pointer to the allocated memory to free it with.  And in
any case, alloc_mem's change to its copy of the pointer would not have
had any effect at all on the original my_ptr in main().  main's my_ptr
would still be NULL.

So if you want a function to be able to modify a caller's int, you
pass the function a pointer to that int.

And if you want a function to be able to modify a caller's pointer,
you pass the function a pointer to that pointer.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq



Fri, 07 Oct 2005 09:22:39 GMT  
 Newbie: declaring the "main" function
Quote:

>    #include<stdio.h>
>    #include<stdlib.h>

>    const char* some_strings[] = {
>      "String One",
>      "String Two",
>      "String Three"
>      };

>    int show_pointer_to_pointer(const char** cpp, int n) {
>      return printf("%s\n", cpp[n]);
>      }

>    int other_notation(const char* cp[], int n) {
>      return printf("%s\n", cp[n]);
>      }

>    char* alloc_mem(size_t n) {
>      return (char*)malloc(n);
>      }

>    int main(int argc, char* argv[[]) {
>      show_pointer_to_pointer(some_strings, 1);
>      other_notation(some_strings, 2);
>      { char* my_ptr = alloc_mem(10);
>        printf("my_ptr = %p\n", (void*)my_ptr);
>        free(my_ptr);
>        }  
>       return 0;
>      }



Fri, 07 Oct 2005 10:11:19 GMT  
 Newbie: declaring the "main" function

Quote:
> #include <stdio.h>

> int main()
> {

> /* Stuff that does something. */

> return;
> }

> ...but I've also seen just "main()" (less the "int" designation).  As
> a general rule of thumb, is it a good programming practice to include
> "int" -- or completely optional.

It has always been a good rule, specially since the new release of the
language published in 1999, that now says that it is mandatory.

--
-ed- emdel at noos.fr
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-library: http://www.dinkumware.com/htm_cl/index.html
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/



Fri, 07 Oct 2005 18:20:22 GMT  
 Newbie: declaring the "main" function

Quote:
>> Function main(int, char**) *always* returns an int!

> I've been wondering about the ** for a while and think now is a good
> time to ask.

> What exactly does it do? char pointer to the contents of an array (as in
> the above example)?

It holds the address of an array of pointers to char

say

char *array[10] =
{
   "Hello",
   "World",
   "SSDD",
   /* the seven other pointers are NULL */

Quote:
};

When you pass 'array' to a function, you actually just pass a value that is
tha address of the first element of the array. You need such a parameter:

void f(char **a)

or

void f(char *a[])

The call is:

   f(&array[0]);

which is a complicated way of writing

   f(array + 0);

which is a complicated way of writing

   f(array);

Quote:
> When would you use it?

To access to the command line parameters.

I suggest you read a C book. I recommend

The C programing language
by Brian Kernighan & Dennis Ritchie
ed.2 (aka 'ANSI')
Prentice Hall

--
-ed- emdel at noos.fr
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-library: http://www.dinkumware.com/htm_cl/index.html
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/



Fri, 07 Oct 2005 18:31:46 GMT  
 Newbie: declaring the "main" function

Quote:

> Actually it is an extremely bad idea to define a program with a return
> type of int and then not return an int. [snip]
> The latest version of the C standard, approved in October 1999, has
> removed implicit int from the language.  Even though there are not
> many of them available yet, a conforming C compiler must issue a
> diagnostic for the definition of main() or any other function without
> an explicitly stated return type.

> Also, although not really topical here, implicit int is illegal in C++
> as well.

> So for proper coding, and particularly to avoid problems with newer
> compilers, always implicitly define main() as returning an int.

Thanks to Jack, Ben, and everyone who replied!  If I boil down all the
answers, it seems the consensus is that I can't go wrong with the
following "main" function declaration:
______

int main()
{

  /* Stuff that does something. */

  return 0;  /* Program completes successfully. */

Quote:
}

______

If that's the case, then I'll take this and run.

Thanks again,

--Jay



Sat, 08 Oct 2005 14:29:08 GMT  
 Newbie: declaring the "main" function

Quote:

> Hi!  I've seen a couple of different basic layouts for the "main"
> function in C, including this one:

> ______

> #include <stdio.h>

> int main()
> {

> /* Stuff that does something. */

> return;
> }
> ______

> ...but I've also seen just "main()" (less the "int" designation).  As
> a general rule of thumb, is it a good programming practice to include
> "int" -- or completely optional.

> Thanks in advance for your responses,

> --Jay

Most of the programmers are not bothered about whats happening outside
main.
Even when we specify a return type to main, we usually return a 0. so
it should not be a compulsion for a programmer to return a value from
main. If the
compiler forces u to specify a return type, use void.


Sat, 08 Oct 2005 22:17:14 GMT  
 Newbie: declaring the "main" function

Quote:

> [re: main declaration]

> Most of the programmers are not bothered about whats happening outside
> main.
> Even when we specify a return type to main, we usually return a 0. so
> it should not be a compulsion for a programmer to return a value from
> main. If the
> compiler forces u to specify a return type, use void.

This advice is completely and utterly invalid.  Please ignore it.

-Arthur



Sat, 08 Oct 2005 22:30:42 GMT  
 Newbie: declaring the "main" function
"

Quote:

> This advice is completely and utterly invalid.  Please ignore it.

> -Arthur

We are here to discuss a topic and learn something. Please try to give
an explanation about what u write.


Sun, 09 Oct 2005 13:12:51 GMT  
 Newbie: declaring the "main" function

(About declaring main() as void)

Quote:
>> This advice is completely and utterly invalid.  Please ignore it.
> We are here to discuss a topic and learn something. Please try to give
> an explanation about what u write.

Please see the comp.lang.c FAQ why void main() is invalid. And please
start saying "you" like everyone else.

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/
"Insanity is to be shared."
   - Tailgunner



Sun, 09 Oct 2005 14:07:35 GMT  
 
 [ 25 post ]  Go to page: [1] [2]

 Relevant Pages 

1. declaring "mailto:" fields

2. Scope of variable declared in "for"

3. Message processing on components declare "withevents"

4. "void main" is a lifestyle choice

5. Override "X" in main frame

6. What's "main application window"?

7. Passing "callback" function to a function

8. remove() vrs fopen("""w")

9. Displaying binary data as ascii "1"'s and "0"'s

10. Looking for "Shroud"/"Obfus"

11. ""help with TSR""

12. Parse trees and "("")"

 

 
Powered by phpBB® Forum Software