I come from a C# background, but I'm learning C at the moment. In C#, when one wants to signal that an error has occurred, you throw an exception. But what do you do in C?
Say for example you have a stack with push
and pop
functions. What is the best way to signal that the stack is empty during a pop
? What do you return from that function?
double pop(void)
{
if(sp > 0)
return val[--sp];
else {
printf("error: stack empty\n");
return 0.0;
}
}
K&R's example from page 77 (code above) returns a 0.0
. But what if the user pushed a 0.0
earlier on the stack, how do you know whether the stack is empty or whether a correct value was returned?