Example 2
This program demonstrates how to determine the C numeric data type
for Clipper numeric parameters for use within C. This version does
all the data comparisons in the file for illustrative purposes.
Example 3 uses extend.h style pseudo-functions defined in Number.h to
hide the details of these comparisons. This allows a much cleaner
source file.
/***
* Testnum.prg
*
* By: Scott McIntosh, Nantucket Corp.
*
* Clipper program to test the C function Number()
*/
#include "set.ch"
PROCEDURE Main
LOCAL nVar1 := 123.456 // a float
LOCAL nVar2 := 123456 // a long
LOCAL nVar3 := 12345 // an int
SET( _SET_DECIMALS, 5 )
SCROLL()
// the C function Number() displays the type, we then re-display
// the value returned from Number() value to confirm that Number()
// converted it to a C variable and back correctly.
//
? nVar1, Number( nVar1 ) // should be a float, value = 123.456
? nVar2, Number( nVar2 ) // should be a long, value = 123456
? nVar3, Number( nVar3 ) // should be an int, value = 12345
QUIT
/***
* Number.c
*
* By: Scott McIntosh, Nantucket Corp.
*
* Demonstrates how to determine the type of a numeric parameter
* received from Clipper. Normally, once we stored the number
* into the proper data type we would do something useful with
* it.
*
* This program just displays the parameters numeric type, and
* then returns the value back to Clipper to show that we haven't
* truncated or corrupted its value.
*
* Compile: cl /AL /c /FPa /Gs /Oalt /Zl number.c
* Link: RTLINK FI numtest, number LIB clipper, llibca /NOE
*/
#include "extend.h"
#include <limits.h>
CLIPPER Number( void )
{
double dNum;
long lNum;
int iNum;
dNum = _parnd(1);
lNum = _parnl(1);
// by subtracting the long value of the parameter from the double
// value of the same parameter, we can determine if it is a floating
// point number (i.e. if we have any decimal places)
//
if( ( dNum - (double) lNum ) != 0 )
{
cprintf( "\r\n\n Double " );
_retnd( dNum );
}
else
{
if( (lNum >= INT_MIN) && (lNum <= INT_MAX) )
{
// we're small enough to be an int, so we could safely
// use iNum and be sure its value is correct
iNum = _parni( 1 );
cprintf( "\r\n\n Integer " );
_retni( iNum );
}
else
{
// we're too big to be an int, so we stay as a long
cprintf( "\r\n\n Long " );
_retnl( lNum );
}
}
return;
}
/* EOF: Example 2 */