decchk() — Check for valid decimal types

Standards

Standards / Extensions C or C++ Dependencies
C Library C only  

Format

#include <decimal.h>

int decchk(decimal(n,p) pdec);

General description

The built-in function decchk() accepts a decimal type expression as an argument and returns a status value of type int.

The status can be interpreted as follows:
DEC_VALUE_OK
A valid decimal representation value (including the less-preferred but valid sign, A-F).
DEC_BAD_NIBBLE
The leftmost half-byte is not 0 in a decimal type number that has an even number of digits. For example, 123 is stored in decimal(2,0). If such a number is packed, then it is used.
DEC_BAD_DIGIT
Digits not allowed (not 0-9). If such a number is packed, then it is used.
DEC_BAD_SIGN
Sign not allowed (not A-F). If such a number is packed, then it is used.

The function return status can be masked to return multiple status.

The parameter n can be any integral value between 1 and DEC_DIG. The parameter p can be any integral value between 0 and DEC_PRECISION, although it must be less than or equal to n. DEC_DIG and DEC_PRECISION are defined inside decimal.h.

If the content of the given argument is not in native packed decimal format, the behavior is undefined.

Example

#include <decimal.h>

decimal(10,2) p1;
char mem2[3] = { 0x12, 0x34, 0x5c };   /* bad half-byte */
char mem3[3] = { 0x02, 0xa4, 0x5c };   /* bad digit */
char mem4[3] = { 0x02, 0x34, 0x56 };   /* bad sign */
char mem5[3] = { 0x12, 0xa4, 0x56 };   /* bad half-byte, digit and sign */
decimal(4,0) *pp2;
decimal(4,0) *pp3;
decimal(4,0) *pp4;
decimal(4,0) *pp5;
int main(void) {
    p1 = 123456.78d;
    pp2 = (decimal(4,0) *) mem2;
    pp3 = (decimal(4,0) *) mem3;
    pp4 = (decimal(4,0) *) mem4;
    pp5 = (decimal(4,0) *) mem5;

    if (decchk(p1) == DEC_VALUE_OK) {
        printf("p1 is a valid decimal representation value.\n");
    }
    if (decchk(*pp2) == DEC_BAD_NIBBLE) {
        printf("pp2 points to a bad half-byte value!\n");
    }
    if (decchk(*pp3) == DEC_BAD_DIGIT) {
        printf("pp3 points to an illegal digit!\n");
    }
    if (decchk(*pp4) == DEC_BAD_SIGN) {
        printf("pp4 points to an illegal sign!\n");
    }
    /* The wrong way ----- */
    if (decchk(*pp5) == DEC_BAD_SIGN) {
        printf("YOU SHOULD NOT GET THIS!!!!!\n");
    }
    /* The right way ----- */
    if ((decchk(*pp5) & DEC_BAD_SIGN) == DEC_BAD_SIGN) {
        printf("pp5 points to an illegal sign!\n");
    }
    return(0);
}
Output
p1 is a valid decimal representation value.
pp2 points to a bad half-byte value!
pp3 points to an illegal digit!
pp4 points to an illegal sign!
pp5 points to an illegal sign!

Related information