Purpose of void main (void) and Functions 
Author Message
 Purpose of void main (void) and Functions

Before someone else responds {*filter*}ly, let me add to the discussion already
given,

void main(void) {}

is an incorrect function definition. Standard C requires that every C
program have a function named "main" which is the function that is executed
first when the program runs. It further specifies that the function main
returns a value of type "int" so a correct definition is:

int main(void) {
  return 0;

Quote:
}

The term void has two meanings in C, the first is that a return type can be
void meaning it returns no value. Second, a function which has no arguments
is declared with void as the sole argument type.

Lest you wonder what the fuss is about regarding "void main", it should be
noted that if you declare main as a function with a void return type, the
behavior of the program is undefined. Depending on what your compiler
implements, the program MAY run as you expect, but it MAY NOT. If you ever
have to port to another platform/compiler, a program that worked previously
may quit working for apparently inexplicable reasons. Furthermore, an ANSI C
compliant compiler will NOT compile a program with a "void" return type for
main.

The "void main" incorrect idiom has become well established. Some authors of
popular books on C have used "void main" extensively, but it is wrong. The
FAQ has an extensive discussion of this issue and it is well worth reading.

http://www.*-*-*.com/ ~scs/C-faq/top.html

Regards,
Sam



Sun, 14 Jul 2002 03:00:00 GMT  
 Purpose of void main (void) and Functions
I am just starting to learn C and don't quite understand the
following:

1.   The purpose of void main (void).

Can someone please explain the purpose and
explain each word included in this function,
i.e. void, main, (void).

2.   Is this how a function works:

You call a function in your source code which is a
header file and that header file goes to a library
to get code.

Thanks

Sent via Deja.com http://www.deja.com/
Before you buy.



Mon, 15 Jul 2002 03:00:00 GMT  
 Purpose of void main (void) and Functions
When used as a function return type, the void means that the function does
not return a value. This is the first void in your example.

When used for a function's parameter list, the void means that the function
takes no parameters.  This is the second void in your example.

Keith.


Quote:
> I am just starting to learn C and don't quite understand the
> following:

> 1.   The purpose of void main (void).

> Can someone please explain the purpose and
> explain each word included in this function,
> i.e. void, main, (void).

> 2.   Is this how a function works:

> You call a function in your source code which is a
> header file and that header file goes to a library
> to get code.

> Thanks

> Sent via Deja.com http://www.deja.com/
> Before you buy.



Mon, 15 Jul 2002 03:00:00 GMT  
 Purpose of void main (void) and Functions

Quote:

>I am just starting to learn C and don't quite understand the
>following:

>1.   The purpose of void main (void).

>Can someone please explain the purpose and
>explain each word included in this function,
>i.e. void, main, (void).

The purpose of void main (void) is to provide a quick rejection test
for bad textbooks on C programming.

Lesson one: the correct declaration for the main function in a C program has
one of the following two forms, or forms equivalent to these:

    int main(void) { /*...*/ }

    int main(int argc, char **argv) { /*...*/ }

Anything else is incorrect and results in undefined behavior.

In the function definition.

    int main (void)  { /*...*/ }

The ``int'' indicates that the function returns a value of type int, which
is the basic integral data type in C. The ``void'' indicates that the
function takes no arguments. A function whose return type is specified
as ``void'' returns nothing---however the main function must not be
defined this way.

The return value of the main function allows the program to indicate whether
it terminated successfully or whether it failed. A zero return value
indicates success. Failure is indicated by returning the value of
the symbolic constant EXIT_FAILURE which is defined in the header <stdlib.h>.

Quote:
>2.   Is this how a function works:

>You call a function in your source code which is a
>header file and that header file goes to a library
>to get code.

The standard headers like <stdlib.h> and <stdio.h> are not necessarily files.
They are features of the C language. The private headers that you create in your
own program are files, of course.

Headers simply contain program text.  By convention, well behaved headers
headers usually contain only declarations of various entities: functions,
types, and static objects.  A declaration essentially specifies the existence
of something, announcing its name and other characteristics known to the rest
of the translation unit.

For example, the header <stdio.h> makes it known that there exists
a function  of the form:

    int printf(const char *, ...);

After including this declaration in your program, you can write calls to that
function. Because the correct declaration of the function has been announced in
the declaration, the calls can be translated into the correct form that is
compatible with the library.

The calls to the function are somehow resolved in the translation and execution
process. How this is done varies from one language implementation to another,
and the international standard which defines the C language doesn't say how
this is done.

Conceptually, a C program consists of one or more translation units which can
be translated separately into images. These ``freeze dried'' images are not
complete programs; they are retained for later linkage. The images are capable
of indicating a yearning for missing pieces of information; for example, the
translated image of your program can somehow indicate that it is not complete
without a a function called printf. Images may be combined together by a
process called linking. The symbolic references among the units are resolved,
and an executable program is produced.  The standard libraries supplied by a
language implementation can take the form of such pre-translated images.  

C language implementations most often translate programs directly into machine
code; the translated images consist of incomplete machine code with some
additional information and are called ``object files''. During linking,
symbolic references among the object files are resolved by assigning an address
to every external object and function, and filling in missing addresses in all
the right places in the machine code to make it complete. Thus the call to
printf in your C program ultimately becomes a machine language branch
instruction in the final executable image. The details vary:  for example in
many modern environments, at least some aspects of the linking process are
delayed until run time.  Programs are linked to ``dynamic'' libraries each they
are loaded and run; this allows libraries to be replaced with new versions
without having to replace existing programs or do anything special to make them
work with the new libraries.



Mon, 15 Jul 2002 03:00:00 GMT  
 Purpose of void main (void) and Functions


<snip several excellent points>

Quote:

> Furthermore, an ANSI C
> compliant compiler will NOT compile a program with a "void" return type
for
> main.

Why not? It's not a syntax error, and (as far as I can tell), it's not a
constraint violation. Therefore, it's up to the compiler. bcc32 -A foo.c
rejects it. cl /Za foo.c accepts it.

I think I'd better add, for the sake of the OP, that Sam is quite correct
in saying void main is wrong. I'm just nitpicking one of Sam's statements,
not his general argument.

--
Richard Heathfield

"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.

comp.lang.c FAQ: http://www.eskimo.com/~scs/C-faq/top.html



Mon, 15 Jul 2002 03:00:00 GMT  
 Purpose of void main (void) and Functions

Quote:

> I am just starting to learn C and don't quite understand the
> following:

> 1.   The purpose of void main (void).

> Can someone please explain the purpose and
> explain each word included in this function,
> i.e. void, main, (void).

Its purpose is to give your program undefined behavior.  It is an
illegal definition of main().

One (of the two) legal forms is
int main(void) { /* ... */ }

This names the main function of the program, taking no arguments and
returning an int.

Quote:

> 2.   Is this how a function works:

> You call a function in your source code which is a
> header file and that header file goes to a library
> to get code.

In the case of external functions (those not in this translation unit)
...
Except for some special case (templates, inlines, and similar), the
header (it need not be a file) contains a prototype which specifies the
number and types of arguments and the return type of the function.  

The linker will have already linked either the compiled code for the
function (from a different translation unit, which may be in an archive
called a library) or sufficient infomation to load it from a dynamic
library.  Exceprt for needing to know how to compile and link, you need
not bother with the particulars of how this is implemented until you
have learned a good deak more.

--

What one knows is, in youth, of little moment; they know enough who
know how to learn. - Henry Adams

A thick skin is a gift from God. - Konrad Adenauer
__________________________________________________________
Fight spam now!
Get your free anti-spam service: http://www.brightmail.com



Mon, 15 Jul 2002 03:00:00 GMT  
 Purpose of void main (void) and Functions

Quote:

>I am just starting to learn C and don't quite understand the
>following:

>1.   The purpose of void main (void).

>Can someone please explain the purpose and
>explain each word included in this function,
>i.e. void, main, (void).

As others have said it is incorrect to define main as returning void.
However a general function declaration of the form

   void func(void)

means that func is a function that takes no arguments (the second void)
and has no return value (the first void).

Quote:
>2.   Is this how a function works:

>You call a function in your source code which is a
>header file and that header file goes to a library
>to get code.

Consider

int printf(const char *, ...);

int main(void)
{
    printf("Hello, world\n");
    return 0;

Quote:
}

This is a valid C program (try it) that calls a standard library function
printf() however no headers have been included. Your C compiler should
be able to compile this without any problem. The line

int printf(const char *, ...);

provides a declaration for printf() i.e. it tells the compiler about
the type of the function and gives it the information it needs to
generate the correct code when it comes to call it within main(). Clearly
the code above doesn't contain the internal workings of printf(). C compilers
usually have a default set of libraries that they will search for otherwise
unresolved function and variable references (that have external linkage).
The standard library functions like printf() and often system functions
are contained within these so the compiler is able to link against them
without any extra prompting. Headers usually just contain declarations,
such as for printf() above, type definitions (such as FILE in <stdio.h>)
and macro definitions (such as EOF and NULL in <stdio.h>). Headers
can provide information about the contents of particular libraries so
that you can create correct code that uses the libraries. However
headers don't usually affect what is and isn't linked. In the example
above the printf() function along with many others is declared in
<stdio.h>. Therefore it is much simpler to include the header than
to declare them yourself. Also some things can't be declared yourself,
e.g. the exact form of the FILE type used by many I/O functions is
compiler-specific, although its *behaviour* is standardised so you
can use it in portable code.

So it would be more normal to write the program above as

#include <stdio.h>

int main(void)
{
    printf("Hello, world\n");
    return 0;

Quote:
}

--
-----------------------------------------


-----------------------------------------


Mon, 15 Jul 2002 03:00:00 GMT  
 Purpose of void main (void) and Functions
On 27 Jan 2000 08:58:35 GMT, "Richard Heathfield"

Quote:



><snip several excellent points>

>> Furthermore, an ANSI C
>> compliant compiler will NOT compile a program with a "void" return type
>for
>> main.

>Why not? It's not a syntax error, and (as far as I can tell), it's not a
>constraint violation. Therefore, it's up to the compiler. bcc32 -A foo.c
>rejects it. cl /Za foo.c accepts it.

Perhaps he should have said "an ANSI C compiler hould complain and
emit a diagnostic since its an illegal form for ANSI C". Personally
I;d like it to also pass 240v through the keyboard but...

Mark McIntyre

C- FAQ: http://www.eskimo.com/~scs/C-faq/top.html



Mon, 15 Jul 2002 03:00:00 GMT  
 Purpose of void main (void) and Functions

Quote:

>Perhaps he should have said "an ANSI C compiler hould complain and
>emit a diagnostic since its an illegal form for ANSI C". Personally
>I;d like it to also pass 240v through the keyboard but...

Although "void main" is not supported by the ANSI C standards, it
also does not violate a "shall" in a constraints clause.  That
means that any given compiler is not required to issue a diagnostic.

I suspect most compilers that can be told to compile "freestanding"
code and that do emit a warning for "void main" can also be told
to suppress this warning, since in a freestanding environment, you
*can* have "void main".  Of course, in a freestanding environment,
the system might start your program by calling the "grinkle"
function; "main" might be an ordinary name, available to the
programmer for any purpose whatsoever.

        char *getstr(void);
        int zonk(void);
        double main(char *, int);

        /* program begins here */
        double grinkle(void) {
                return main(getstr(), zonk()) / 3.14159;
        }

This might even be a complete freestanding program -- perhaps
getstr(), zonk(), and main() are provided in the freestanding
implementation's library.
--
In-Real-Life: Chris Torek, Berkeley Software Design Inc




Mon, 15 Jul 2002 03:00:00 GMT  
 Purpose of void main (void) and Functions


Quote:
>On 27 Jan 2000 08:58:35 GMT, "Richard Heathfield"



>><snip several excellent points>

>>> Furthermore, an ANSI C
>>> compliant compiler will NOT compile a program with a "void" return type
>>for
>>> main.

>>Why not? It's not a syntax error, and (as far as I can tell), it's not a
>>constraint violation. Therefore, it's up to the compiler. bcc32 -A foo.c
>>rejects it. cl /Za foo.c accepts it.
>Perhaps he should have said "an ANSI C compiler hould complain and
>emit a diagnostic since its an illegal form for ANSI C".

However an ANSI C compiler is *always* allowed to emit a diagmostic. The
significant thing here is that the compiler is allowed to eject the program
if it wishes. Emitting a diagnostc and rejecting a program are two different
things.

--
-----------------------------------------


-----------------------------------------



Tue, 16 Jul 2002 03:00:00 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. what's the difference between void main(void) and int main(void)

2. main(void) vs void main ()

3. void main(void) or int main() ??

4. from void (void*) to void (_cdecl*) (void*)

5. Is void main(void) a standard or not?

6. void main(void) ????

7. void main(void) illegal??

8. void main(void)

9. Is void main(void) ANSI standard??

10. void main(void)

11. void main(void)....revisited!

12. void main(void)

 

 
Powered by phpBB® Forum Software