Programming examples

Programming examples CCNGDC3 and CCNGDC4 are shipped with the compiler.

Figure 1 (CCNGDC3) shows an example of using the decimal type.

Figure 2 shows the output produced by Figure 1.
Figure 2. Output produced by CCNGDC3
pd01 = -789.45
pd02 =           1234.56000
pd06.m = 27, pd07->m = 27
pd06.pd03 = 6.0000000000, pd07->pd03 = 6.0000000000
pd06.pd04[0] = 445.11,             pd07->pd04[0] = 445.11
pd06.pd04[1] = 12017.97,            pd07->pd04[1] = 12017.97
pd06.pd04[2] = 5348886.87,           pd07->pd04[2] = 5348886.87
*(pd06.pd05) = -789.45, *(pd07->pd05) = -789.45
pd08[0] = 0.333333333333333333333000000000
pd08[1] = 0.333333333333333333333333333333

Figure 3 shows another sample program (CCNGDC4) that also uses decimal type.

Figure 3. Decimal type — example 2
/* this example demonstrates the use of the decimal type */

#include <decimal.h>

decimal(31,4) pd01 = 1234.5678d;
decimal(29,4) pd02 = 1234.5678d;

int main(void)
{
  /* The results are different in the next two statements */
  pd01 = pd01 + 1d;
  pd02 = pd02 + 1d;

  printf("pd01 = %D(31,4)\n", pd01);
  printf("pd02 = %D(29,4)\n", pd02);

  /* Warning: The decimal variable with size 31 should not be      */
  /*          used in arithmetic operation.                        */
  /*          In the above example: (31,4) + (1,0) ==> (31,3)      */
  /*                                (29,4) + (1,0) ==> (30,4)      */

  return(0);
}
Figure 4 shows the output produced by Figure 3. See Intermediate results to understand the output from this example and to see why decimal variables with size 31 should be used with caution in arithmetic operations.
Figure 4. Output produced by CCNGDC4
pd01 = 1235.5670
pd02 = 1235.5678