Reputation: 13
I want to have an extra function that checks if an input function returns 0. The extra function would exit the program if so and print the number of line where it happened. I wrote this:
void check( int function_return_value, int line_with_error )
{
if(!function_return_value )
{
printf("Error on line %d\n", line_with_error );
exit(EXIT_FAILURE);
}
}
line_with_error recieves a value of LINE macro inside my main code file and it looks like this
check( function(), __LINE__ );
However I don't want "check" function to take 2 arguments, just 1 with function return value. Is there a way to somehow "hide" the second argument so that the function knows that LINE macro will always go there but I don't have to write it every time?
P.S. LINE will take the number of a line where it is written in a code. I can't put it inside "check" function since it will always refer to the line inside the declaration of function
Upvotes: 1
Views: 63
Reputation: 781848
Use a macro:
#define check(x) (check_2args((x), __LINE))
void check_2args( int function_return_value, int line_with_error )
{
if(!function_return_value )
{
printf("Error on line %d\n", line_with_error );
exit(EXIT_FAILURE);
}
}
Upvotes: 2