exit()

D

Darklight

if the exit() function is contained in stdlid.h
why if i remove the above header file the exit
function still works and i can still compile
program without any error messages using cc file.c
if i use cc -Wall file.c i get error message
file.c:18: warning: implicit declaration of function `exit'
but program will still run and function properly
 
J

Jens.Toerring

Darklight said:
if the exit() function is contained in stdlid.h
why if i remove the above header file the exit
function still works and i can still compile
program without any error messages using cc file.c

Without appropriate flags telling the compiler to output warnings
many compilers only output messages for errors. Having no declaration
in scope for a function isn't an error, the compiler will assume by
default that that function is going to return an int (at least if you
use a non-C99 compiler).
if i use cc -Wall file.c i get error message
file.c:18: warning: implicit declaration of function `exit'
but program will still run and function properly

You're lucky. Even though the compiler had no information on what
arguments exit() takes and it had to make a mistake in the assumption
on what exit() returns (but, due to the nature of exit(), it's rather
unimportant) it seems to have been able to produce a correct input
file for the linker. And the linker doesn't care about declarations
anymore, it just finds exit() in the C standard library of your
system and makes the final executable use that.

Regards, Jens
 
F

Flash Gordon

if the exit() function is contained in stdlid.h
why if i remove the above header file the exit
function still works and i can still compile
program without any error messages using cc file.c
if i use cc -Wall file.c i get error message
file.c:18: warning: implicit declaration of function `exit'
but program will still run and function properly

The exit function isn't contained in stdlib.h it is only declared there.
The actual function is either compiler magic of contained in the
standard library.

The warning when using -Wall is the compiler being helpful.

Please note that with some functions you invoke undefined behaviour if
you don't have an appropriate declaration in scope and there are real
systems where this can cause the software to fail. Even for the
functions where it does not automatically invoke undefined behaviour it
prevents the compiler from warning you if the parameters are incorrect
(such as not passing any to exit) so you should always include the
relevant header even if you can get away without it.

Also, if using gcc (which -Wall suggests to me you might be) you might
want to consider:
gcc -ansi -pedantic -Wall -O file.c
This will make gcc comply with the C89 (or possibly C95) standard and
generate additional helpful warnings.
 
E

Emmanuel Delahaye

Darklight wrote on 03/09/04 :
if the exit() function is contained in stdlid.h

It's not. A header only contains function declaration (in prototype
forms since 1989). Please reread your C-book.
why if i remove the above header file the exit
function still works and i can still compile
program without any error messages using cc file.c
if i use cc -Wall file.c i get error message
file.c:18: warning: implicit declaration of function `exit'
but program will still run and function properly

This is because the body of the function is elswere (probably in some
library)

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"C is a sharp tool"
 
S

SM Ryan

# if the exit() function is contained in stdlid.h
# why if i remove the above header file the exit

If you don't define or declare a function, it is implicitly declared as
int functionname();
(Returns int, takes an indefinite number of parameters converted with some
default promotions.)

If the function definition is compatiable with the implicit declaration,
it will work.
 
D

Dan Pop

In said:
I use "gcc -W -Wall -ansi -pedantic -Wwrite-strings -O1"

I don't. As the man page says, there are very good reasons -W and
-Wwrite-strings have not been included into -Wall.

If someone wants to bring gcc even closer to ANSI C conformance on x86,
-ffloat-store should be added to the original set of options. But it
slows down floating point code.

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top