There are no notfications.

Reporting error conditions

The errno.h header provides macros for reporting error conditions in function calls of the standard library and for any program that includes it. In many cases, it may be more appropriate to report error conditions by returning particular values from functions, such as negative values from functions that ordinarily return positive values, instead of utilising this header.

Code

main.c

#include <errno.h> // "EDOM", "ERANGE" and "errno".
#include <stdio.h> // "printf", "puts" and "scanf".
#include "perform_operation.h" // "EINVALID" and "perform_operation".

int main(void)
{
	int number;

	printf("%s", "Enter number: ");

	scanf("%d", &number);

	errno = 0; // If not first use.

	number = perform_operation(number);

	switch (errno)
	{
		case EDOM:
			puts("Error: Domain");

			break;
		case ERANGE:
			puts("Error: Range");

			break;
		case EINVALID:
			puts("Error: Invalid");

			break;
		default:
			printf("Result: %d\n", number);

			return 0;
	}

	errno = 0; // If not last use.

	return 1;
}

perform_operation.c

#include <errno.h> // "EDOM", "ERANGE" and "errno".
#include "perform_operation.h" // "EINVALID".

int perform_operation(int number)
{
	if (number <= 0)
	{
		errno = EDOM;
	}
	else if (number > 1000)
	{
		errno = ERANGE;
	}
	else if (number == 100)
	{
		errno = EINVALID;
	}

	return (((((number * 9)) + 7) / 5) - 3);
}

perform_operation.h

#ifndef PERFORM_OPERATION
#define PERFORM_OPERATION

#define EINVALID 47

int perform_operation(int number);

#endif

Execution

Example 1

Standard output stream:

Enter number: 

Standard input stream:

50

Standard output stream:

Result: 88

Example 2

Standard output stream:

Enter number: 

Standard input stream:

100

Standard output stream:

Error: Invalid

Example 3

Standard output stream:

Enter number: 

Standard input stream:

1234

Standard output stream:

Error: Range

Example 4

Standard output stream:

Enter number: 

Standard input stream:

-50

Standard output stream:

Error: Domain

Explanation

main.c

The main function requests a number from the standard input stream and then passes it to the perform_operation function. Once the modified number is returned, the errno value is matched against the error number macros in the switch-statement.

It should be noted that the first time errno is checked after program startup, it will always be 0 but will have to be reset to 0 before each subsequent use.

perform_operation.c

The perform_operation.c file contains a function that takes an int value and performs a contrived operation on it. If the number passed to the function does not conform with the arbitrary rules laid out in the if-statement, then errno is assigned the appropriate error number.

perform_operation.h

The perform_operation.h file contains a custom error number (EINVALID) that expands to an arbitrary int value. The value is unique in that it is not the same as any of the possible errno values that can appear in the context in which the error number will be used.