> I've noticed that exit() can take a parameter. I've seen
> exit(0), exit(1),
> exit(2) and even exit(10). What does the parameter mean ? I
> can't find any
> table with its possible values ...
>
exit() can have any value you want. By convention,
exit(0) (same as exit()) means your program exited
without error. Any value other than 0 means your
program had an error. You choose what value you
want, and you can create your own table of error
codes so you know why your program terminated.
Example:
int main(void)
{
char name[16];
printf("Please enter your name: ");
scanf("%s", name);
if(strlen(name) == 0)
{
/* We didn't get input */
exit(1);
}
else if(strlen(name) > 15)
{
/* Name too long. Buffer overflow */
exit(2);
}
else
{
/* Name is fine. Print it out */
printf("Your name is %s", name);
}
/* Program executed fine. Exit */
exit(0);
}
Don't critique the code; I made up programming
practice as I went along to try to arrive at a
point. Look at how the exit() calls have
different values. If you checked the return
code of the program, when the program suddenly
died and returned a 1, you would know input
wasn't received. If it returned a 2, the name
was too long. If it returned 0, everything
was fine.
~Patrick