Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 02-21-2008, 09:16 AM   #1
LQ Newbie
Registered: Feb 2008
Posts: 2

Rep: Reputation: 0
Unhappy 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...
why not void ?

Old 02-21-2008, 09:38 AM   #2
Registered: Feb 2008
Location: London
Distribution: FC8, FC9, Centos 4, Centos 5, Knoppix
Posts: 52

Rep: Reputation: 16
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.
Old 02-21-2008, 09:56 AM   #3
Senior Member
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,268

Rep: Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888
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:
#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
 *      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
#ifndef FALSE
#       define  FALSE   0

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;
                case 'v':
                        vopt = TRUE;
                        (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]);
        /*      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.

Last edited by tronayne; 02-21-2008 at 10:47 AM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Elementary security issues: hopefully useful to all eoinrua Linux - Newbie 4 11-04-2007 12:05 PM
[SOLVED] Elementary Bash Scripting Question Eternal_Newbie Slackware 2 08-25-2005 07:40 PM
Elementary Question - Can't Edit File - Need Root rrrssssss Linux - Newbie 2 04-12-2005 04:05 AM
Does anyone have hardware for my elementary school classroom? tuxthelinuxpeng Linux - Hardware 0 04-05-2004 07:48 PM
Elementary IP question Rotwang Linux - Networking 14 03-11-2004 05:44 PM

All times are GMT -5. The time now is 01:29 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration