Sign in to follow this  
GoldenViper_LO

Having problems...

Recommended Posts

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

Share this post


Link to post
Share on other sites
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);
}


Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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()).

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this