Having problems...

Started by
6 comments, last by GoldenViper_LO 18 years, 7 months ago
Hey there, I'm new to C and I am doing something that my book says to do. I keep getting an error that says "Call to undentifed function "exit" in function main(int,char * *)" Here's where it says the error is... { fprintf(stderr, "\nProper Usage is: " ); fprintf(stderr, "\n\nPRINT_IT filename.ext\n" ); exit(1); } Please lemme know what to do, thanks. -Russ
Advertisement
Are you sure you're including the C standard library "stdlib.h"?

...
"#include "stdlib.h"
I use "#include <stdio.h>" That's what the book says too, I'm so confused...
Quote:Original post by GoldenViper_LO
I use "#include <stdio.h>" That's what the book says too, I'm so confused...


You need to use "stdlib.h", not "stdio.h" as xllx_relient_xllx has said to get access to the exit function. MSDN linky
#include <stdio.h>#include <stdlib.h>int main( int argc ,char** argv ){   fprintf(stderr, "\nProper Usage is: " );   fprintf(stderr, "\n\nPRINT_IT filename.ext\n" );   exit(1);}
Any library functions that you want to call are defined in specific files. You have to include these files. The <stdio.h> gives you the fprintf functions, but not exit. I believe that is defined in <stdlib.h>, so add #include <stdlib.h> right after your #include <stdio.h>

However, exit is unneccessary from within main. That is used to immediately exit the program, no matter where you are. Within main, this functionality is provided by return. exit(5); is identical to return 5;. The difference between the two options is that exit exits the program, but return only exits the current function. Since nothing calls main [that you need to worry about], exiting the current function also exits the program.

CM
Quote:Original post by Conner McCloud
However, exit is unneccessary from within main. That is used to immediately exit the program, no matter where you are. Within main, this functionality is provided by return. exit(5); is identical to return 5;. The difference between the two options is that exit exits the program, but return only exits the current function. Since nothing calls main [that you need to worry about], exiting the current function also exits the program.


There is one case where exit(5); and return 5; are not identical in main(). In C (not C++) you can call main() like a regular function. exit(5); will still exit the program while return 5; will just return from that invocation of main(). However, as Conner McCloud said, you don't have to worry about that (it's always bad form to recursively call main()).
I would also like to add something else..

The diffrence between <stdlib.h> and "stdlib.h" is not such a matter of importance. Because, the former tells the compiler to look for the stdlib.h library in the compilers main library folder (or something of that sort) first, and, the latter tells it to look in the programs current directory (where your compiled files reside) first.

In light of that. I think you should, like most everyone else, use < .. > for standard library's and " .. " for the ones you create.

BTW: I actually meant to use < .. > in my first example =)
relient
Thanks everyone, it's working now.

This topic is closed to new replies.

Advertisement