[comp.lang.c] Turbo C atexit function.

hocker@enuxha.eas.asu.edu (Charles C. Hocker) (06/16/89)

Hello,
	I am using Turbo C 2.0 and am having trouble getting the
atexit function to work.  I typed in the following program from the
Turbo C reference guide and could not get it to work.  Does anyone 
know of any bugs using this function?

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

atexit_t exit_fn1 (void)
{
  printf("Exit function 1 called\n");
}

atexit_t exit_fn2 (void)
{
  printf("Exit function 2 called\n");
}

main ()
{
  /* posting exit_fn1 */
  atexit (exit_fn1);

  /* posting exit_fn2 */
  atexit (exit_fn2);
  printf ("Main quitting...\n");
}

When the program is compiled the following error messages are given:
   Type mismatch in parameter 'func' in call to 'atexit' in function main

Any assistance will be appreciated.

				-Chuck   hocker@eas.asu.edu

hocker@enuxha.eas.asu.edu (Charles C. Hocker) (06/16/89)

Sorry being new to the net, I posted my address incorrectly.  I can
be reached at

	hocker@enuxha.eas.asu.edu

	-Chuck

hinton@netcom.UUCP (Greg Hinton) (06/20/89)

In article <166@enuxha.eas.asu.edu> hocker@enuxha.eas.asu.edu (Charles C. Hocker) writes:
>	I am using Turbo C 2.0 and am having trouble getting the
>atexit function to work.  I typed in the following program from the
>Turbo C reference guide and could not get it to work....
>....
>
>atexit_t exit_fn1 (void)
>....
>
>atexit_t exit_fn2 (void)
>....
>
>main ()
>{
>  /* posting exit_fn1 */
>  atexit (exit_fn1);
>
>  /* posting exit_fn2 */
>  atexit (exit_fn2);
>  printf ("Main quitting...\n");
>}
>
>When the program is compiled the following error messages are given:
>   Type mismatch in parameter 'func' in call to 'atexit' in function main

This example is taken verbatim from pp. 47-48 of the Reference Manual.

The bug is in the manual, not the compiler or libraries!  Change the type of
exit_fn1() & exit_fn2() to void & all will be well!

bobmon@iuvax.cs.indiana.edu (RAMontante) (06/21/89)

As Greg Hinton says, the error lies in the test program printed in the
manual.  `atexit_t' is typedef'ed to mean 

	typedef void (* atexit_t)(void);

so instead of returning `atexit_t' the two exit functions should
properly be declared as returning nothing:

	void exit_fn1(void);
	void exint_fn2(void);

When the test program is modified this way it runs fine.  As long as
you're at it, Doug Gwyn's comment about main()'s return value can also
be finessed by declaring it as

	void main(void)

[I don't know if this is legitimate, but it silences the warning messages.]

gwyn@smoke.BRL.MIL (Doug Gwyn) (06/22/89)

In article <22420@iuvax.cs.indiana.edu> bobmon@iuvax.cs.indiana.edu (RAMontante) writes:
>... Doug Gwyn's comment about main()'s return value can also
>be finessed by declaring it as
>	void main(void)
>[I don't know if this is legitimate, but it silences the warning messages.]

No, it's not legitimate.  The goal should not be to "silence warning
messages", but to produce a correct program.  main() returns an int
value which is reported as the program's termination status.  You can
also invoke the exit() function to report this status.  A normal
return from main() without a value (such as occurs when the closing }
of the main() function body is "executed") is erroneous, whether or
not the compiler happens to complain about it.  The start-up code that
invoked main() in the first place is expecting a return value.

gwyn@smoke.BRL.MIL (Doug Gwyn) (07/22/89)

In article <166@enuxha.eas.asu.edu> hocker@enuxha.eas.asu.edu (Charles C. Hocker) writes:
>atexit_t exit_fn1 (void)
>{
> [...]
>main ()
>{
>  /* posting exit_fn1 */
>  atexit (exit_fn1);
> [...]
>   Type mismatch in parameter 'func' in call to 'atexit' in function main

I don't know what atexit_t is supposed to be (it's not part of the C
Standard), but I suspect your problem lies in using it for the function
return type.  Functions registered via atexit() have void return type.
Try changing atexit_t to void in your test program.

Also note that you're supposed to return an explicit value for the
result of main(); 0 would be a good one.