LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   elementary help !! (https://www.linuxquestions.org/questions/linux-newbie-8/elementary-help-622777/)

matlin 02-21-2008 08:16 AM

elementary help !!
 
while running a C programs in GNU compiler,
if i write return type of main() to be void then it gives me error (not warning ) that return type of main() can't be void..ok !
it should be int...
but
why not void ?


timnp 02-21-2008 08:38 AM

I think because it has to return an exit code when it terminates so that any calling program will know if your program managed to do it's job or if it met an error.

tronayne 02-21-2008 08:56 AM

The standard changed (I think) -- you can't define a void main() in C++ but you can (or could, I'm not completely clear on this) in C; there's a lot of discussion about this all over the place. You can tell GCC to shut up about it, and you can define your main as void and it'll work just fine (Sun C, for example, doesn't give a hoot if you define main as void). One of the things you should do in any event is exit (status), rather than letting the program fall off the end; here's the template I use to begin C programs with in Linux or Solaris that demonstrates this:
Code:

#ident  "$Id$"

/*
 *      Copyright (C) 2008 Your Name or Organization Goes Here
 *
 *      This program is free software; you can redistribute it and/or
 *      modify it under the terms of version 2 of the GNU General
 *      Public License as published by the Free Software Foundation.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *      General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public
 *      License along with this program; if not, write to the Free
 *      Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 *      MA 02111-1307, USA.
 *
 *      Name:          $Source$
 *      Purpose:        brief description goes here
 *      Version:        $Revision$
 *      Modified:      $Date$
 *      Author:        Your Name Goes Here
 *      Date:          Today's Date
 *      $Log$
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#ifndef TRUE
#      define  TRUE    1
#endif
#ifndef FALSE
#      define  FALSE  0
#endif

void    main    (int argc, char *argv [])
{
        int    c;                      /* general-purpose              */
        int    error = FALSE;          /* error flag                  */
        int    vopt = FALSE;          /* verbose option              */
        time_t  t0 = (time_t) 0;        /* start time                  */
        time_t  t1 = (time_t) 0;        /* finish time                  */
        FILE    *in;

        /*      process the command line arguments                      */
        while ((c = getopt (argc, argv, "?v")) != EOF) {
                switch (c) {
                case '?':
                        error = TRUE;
                        break;
                case 'v':
                        vopt = TRUE;
                        break;
                default:
                        (void) fprintf (stderr, "getopt() bug\n");
                        exit (EXIT_FAILURE);
                }
        }
        /*      any errors in the arguments, or a '?' entered...*/
        if (error) {
                (void) fprintf (stderr, "usage: %s [-v] argument...\n",
                    argv [0]);
                exit (EXIT_FAILURE);
        }
        /*      get a start time                                */
        if (time (&t0) < (time_t) 0)
                (void) fprintf (stderr,
                    "%s:\tcan't read system clock\n", argv [0]);
        /*      now process any arguments supplied...          */
        while (optind != argc) {
                (void) fprintf (stderr, "Processing %s...\n", argv [optind]);
                /*      open the input file            */
                if ((in = fopen (argv [optind], "r")) == (FILE *) NULL) {
                        (void) fprintf (stderr,
                            "%s:\tcan't open %s\n",
                            argv [0], argv [optind]);
                        exit (EXIT_FAILURE);
                }
                /*      your program code goes here      */
                /*      close the input file            */
                if (fclose (in))
                        (void) fprintf (stderr,
                            "%s:\tcan't close %s\n",
                            argv [0], argv [optind]);
                optind++;
        }
        /*      get a finish time                      */
        if (time (&t1) < (time_t) 0)
                (void) fprintf (stderr,
                    "%s:\tcan't read system clock\n", argv [0]);
        if (vopt)
                (void) fprintf (stderr,
                    "%s duration %g seconds\n",
                    argv [0], difftime (t1, t0));
        exit (EXIT_SUCCESS);
}

And, yes, I define them as void until I get sick of GCC yammering at me about it.

Hope this helps some.


All times are GMT -5. The time now is 10:01 PM.