Having problems...

This topic is 4552 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

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 on other sites
Are you sure you're including the C standard library "stdlib.h"?

...
"#include "stdlib.h"

Share on other sites
I use "#include <stdio.h>" That's what the book says too, I'm so confused...

Share on other sites
Quote:
 Original post by GoldenViper_LOI use "#include " 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 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 on other sites
Quote:
 Original post by Conner McCloudHowever, 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 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 on other sites
Thanks everyone, it's working now.