delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/03/13/13:11:33

Date: Thu, 13 Mar 1997 13:00:38 -0500 (EST)
From: "art s. kagel IFMX x2697" <kagel AT dg1 DOT bloomberg DOT com>
To: "J. van Eijnsbergen" <hanvane AT worldonline DOT nl>
Cc: djgpp AT delorie DOT com
Subject: Re: Main + Void
In-Reply-To: <33286E94.5435@worldonline.nl>
Message-Id: <Pine.D-G.3.91.970313123917.13611A-100000@dg1>
Mime-Version: 1.0

Almost.  The meaning of 'void' depends on the context in which it is used:

void *fred;    /* Declared fred to be a pointer to undefined memory this is
                 useful for arguments to generic functions which must handle
                 multiple data types such as qsort(), bsearch(), etc. */

void barney( void *betty ); /* Declares function barney which takes a single
                              void pointer argument, which might be called
                              'betty' in the implementation but need not be,
                              and which has a return type 'void' meaning, as
                              van E. points out returns nothing. */

wilma( void );   /* Declares a function wilma with a void argument list 
                   meaning that it takes no arguments.  However, this 
                   function declares no return type which makes it default
                   to a K&R syntax return type 'int'. */

The functions van E. relates are miss-identified.  Routine1 is not a void
function but as for wilma() returns an int which contains an undefined 
value because it does not contain a return statement.  Routine1 probably 
should have been defined void like barney().  The function main() 
provided has no explicit return type which also defines it as returning 
int, which is correct, all main() functions MUST RETURN int!  You 
therefore should explicitely define them that way, towit:

int main( int argc, char **argv )
{.....
    return 0;
}

The operating system and/or "C" language startup code expect that main() 
will indeed return an explicit integer value through a return statement 
or an implicit value through an exit( value ) function call.  Any main() 
which can "fall off the end" must contain a return statement returning a 
value along all exit paths.

I hope that this is clear now!?

Art S. Kagel, kagel AT ts1 DOT bloomberg DOT com

On Thu, 13 Mar 1997, J. van Eijnsbergen wrote:

> MoD wrote:
> > 
> > just a quick question.. the "Main" part... is that the main body of the
> > code?
> > and what is "void" used for? =)
> > 
> > PS
> > hope everyone is patient enuff to get me through these beggining stages =)
> 
> 
> Hello MoD,
> 
> I am only a beginner too, so maybe we can help each other.
> 
> MAIN() is the main part of a c/c++ program (it has to be there).
> So in main you can call other routines for instance routine1 and
> routine2.
> The term VOID means that there are no variables to return.
> In the following example main calls 2 routines (1 and 2).
> Routine1 only prints something on the screen and routine2 calculates
> something.
> In routine1 you can use VOID (cause no numbers have to be returned to
> the main program).
> In routine2 the program calculates something (an addition) so VOID
> cannot be used. Numbers are returned (in this case the answer). 
> 
> The example:
> ======== this is just an example (no true c-code)=======
> main(void)
> {
> var1=1
> var2=1
> routine1();                 // call routine1
> answer=routine2(var1,var2); // call routine2
> printf ("1+1=%d\n",answer); // or in C++ you can use COUT
> }
> /********
> routine1(void)
> {
>  printf("This program calculates 1 + 1\n");
> }
> /********
> int
> routine2(A,B)
> {
> result=A+B
> RETURN(result)
> }
> ======== this is just an example (no true c-code)=======
> 
> As I said the above is no true (to be compiled) c-code. I did not
> declare any variables and routines; this to keep it short (if you need
> the real working c-code, e-mail me, I will send it to you).
> 
> I hope the VOID thing is clear now.
> Greetings from Holland,
> -- 
> Han van Eijnsbergen
> hanvane AT worldonline DOT nl
> http://www.worldonline.nl/~hanvane/index.html
> http://www.worldonline.nl/~hanvane/zin.htm
> 

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019