1

I've been making some school project and I've used this function for error outputs:

void errorMsg(char* msg)
{
    fprintf(stderr, msg);
    exit(EXIT_FAILURE);
}

So I can do something like this:

if (condition)
    errorMsg("Error, Wrong parameters.");

Message will be shown to user and program will exit with error code.

But I've recieved minus points for using exit function.

So I was just wondering how should I do it? Is there some another way than writing this for several times in main function?

if (condition)
{
    fprintf(stderr, "Error, Wrong parameters.");
    return(EXIT_FAILURE);
}

Thanks for you time.

sczdavos
  • 2,035
  • 11
  • 37
  • 71
  • 2
    You should ask whoever marks your work what they are expecting. We could advise you, but what you really need is to know what the assessor wants. If our advise doesn't match theirs, then you'll be no better off. – David Heffernan Nov 21 '14 at 20:11
  • 1
    Are you sure it's for `exit` and not for `char * - fprintf`? – edmz Nov 21 '14 at 20:21
  • Whether or not you do it via a function, your method of sharp exit is still the same. – Weather Vane Nov 21 '14 at 20:22
  • @black what is wrong with `fprintf(stderr, msg);` when `*msg` is text only and requires no other arguments? – Weather Vane Nov 21 '14 at 20:30
  • 1
    @WeatherVane The compiler cannot check the presence of format specifiers since the string is not a literal. Use `%s, msg` or `fputs`. – edmz Nov 21 '14 at 20:48
  • @WeatherVane It isn't. Read more [here](http://stackoverflow.com/questions/4419293/warning-format-not-a-string-literal-and-no-format-arguments) – edmz Nov 21 '14 at 20:53
  • @black the strings passed do not have any format specifiers. And even when I compile `fprintf(stderr, "Error %d");` there is no compiler error or warning - it just prints garbage. Nor in the first case does the compiler issue any warning about not using a literal. – Weather Vane Nov 21 '14 at 20:53
  • @black Good notice, I'll remember this. Thanks :) – sczdavos Nov 21 '14 at 20:59
  • @black I probably deleted my comment "It is (a literal)" as you replied to it. Yes, the argument `char*` is not literal, but MS Visual C does not object in the case I mentioned, when it is. – Weather Vane Nov 21 '14 at 21:05
  • 1
    Normally, a later question is closed as a duplicate of an earlier question. This time, I've reversed the process, because the later question has better answers than this one, though the basic question nominally covers the same territory. FWIW: I use functions equivalent to `errorMsg()` in my code routinely. Unless there's a design criterion that rules it out (such as "the code will go into a library and therefore should not exit unilaterally"), it is perfectly reasonable to design your code to call exit in case of emergency. – Jonathan Leffler Jul 19 '15 at 19:03

3 Answers3

2

It really depends on how you program, or in this case, how your teacher wants you to program.
The best answer at this case, is to go to your teacher and ask for an explanation why this is wrong.

When I program in C, all my functions always return int (or a typedef of int) and then I can return errors from functions. Then my main function returns an error as it returns. Something like this template:

int func(void)
{
    int return_value = 0;
    if (/* Some condition */) {
        return_value = 1
    }
    /* Some code */
    return return_value;
}

int main(void)
{
    int return_value = 0;
    return_value = func();
    return return_value
}

Using this template, if there is an error, 1 would be returned from the main function, and in case of success, 0 would be returned. As well, you can add an if condition in the main function, to check the return value of func. This gives you the option to handle errors in your code.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Idan
  • 333
  • 2
  • 8
1

One important reaons:
If you have some code which has opened files, but not yet finished to write in them, and this piece of code delegates some work to a function that will end the program, what happens?
Right, you´ll get a broken file.
Same reasoning is valid for allocated memory, open handles of any kind, etc.etc.

Instead of thinking everytime if the function could end the program, don´t write such functions in the first place. It doesn´t matter if everything is ok or not, but return the "control" after a function to the caller everytime. The only place where exit could be ok is main, because it will basically do the same thing as return there. And in main, there´s no reason to not write return in the first place.

deviantfan
  • 11,268
  • 3
  • 32
  • 49
0

My opinion is that exit functions are harder to test. I struggled a lot while testing a function that has exit call.

SS Sid
  • 421
  • 6
  • 12