Chapter Contents

Previous

Next
isnotconst isnumconst isstrconst

isnotconst isnumconst isstrconst



Return Compile-Time Constant

isnotconst: Test for Nonconstant
isnumconst: Test for Numeric Constant
isstrconst: Test for String Literal
Portability: SAS/C extension


SYNOPSIS
DESCRIPTION
EXAMPLES


SYNOPSIS

#include <lcdef.h>
int isnotconst(expression);
int isnumconst(expression);
int isstrconst(expression);


DESCRIPTION

These macros examine expression and return a compile-time constant. If expression is the appropriate type of constant, a nonzero constant is returned; otherwise, 0 is returned. The type tested for is numeric for isnumconst , string literal for isstrconst , and nonconstant for isnotconst . The expression constant can have any type.

expression is never evaluated, and these macros always yield a constant, regardless of the type of expression .

The isnotcons , isnumconst , and isstrconst macros are used primarily to control the generation of code by in-line functions. Because they produce compile-time constants, the macros can be tested at compile time, enabling the compiler to eliminate sections of code that can never be executed.


EXAMPLES

Below are several examples using these nonstandard macros:

Example 1.4

if (isnotconst(argv[]))                 /* true  */
if (isnumconst(100))                    /* true  */
if (isstrconst("XYZ"))                  /* true  */
if (isstrconst(c == 0 ? "A" : "B")      /* false */

Example 1.5

#define MAXLEN 1024

if (isnumconst(MAXLEN) && 500 < MAXLEN) /* true  */

Example 1.6

This example defines the function smemcpy (meaning short memcpy ) that prevents the expansion of the built-in memcpy function unless the length argument is a constant integer less than or equal to 256. If the length argument is greater than 256 or is not a constant integer, a call to the true memcpy function is generated.

The if condition is a constant expression and is evaluated at compile time. The compiler generates code either for the then branch or the else branch, depending on the result of the test. Under no conditions is code for both branches generated.

#include <lcdef.h>
#include <string.h>

#define smemcpy(d, s, len)
        inline_memcpy(d, s, len, isnumconst(len))
_ _inline
void *inline_memcpy(void *d, const void *s,
                    size_t len, int cnst)
{
   if (cnst && len < 257)
      memcpy(d, s, len);
   else
      (memcpy)(d, s, len);
   return d;
}


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.