COHERENT manpages

This page displays the COHERENT manpage for # [String-ize operator].

List of available manpages
Index


# -- Preprocessing Operator

String-ize operator

The preprocessing operator  # can be used within the  replacement list of a
function-like macro.  It and its  operand are replaced by a string literal,
which names the sequence  of preprocessing tokens that replaces the operand
throughout the macro.

For example, the consider the macro:

    #define display(x) show((long)(x), #x)

When the preprocessor reads the following line

    display(abs(-5));

it replaces it with the following:

    show((long)(abs(-5)), "abs(-5)");

Here, the  preprocessor replaced #x  with  a string literal  that gives the
sequence of token that replaces x.

The following rules apply to interpreting the # operator:

1. If a sequence  of white-space characters occurs within the preprocessing
   tokens  that  replace  the  argument,  it  is replaced  with  one  space
   character.

2. All  white-space characters  that occur  before the  first preprocessing
   token and after the last preprocessing token are deleted.

3. The original  spelling of the  preprocessing tokens is  preserved.  This
   means  that  you  must  take  care  to preserve  certain  characters:  a
   backslash `\'  should be inserted  before every quotation  mark `"' that
   marks a  string literal,  and before  every backslash that  introduces a
   character constant.

Example

The  following  uses  the operator  #  to  display  the  result of  several
mathematics routines.

#include <errno.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

void show(value, name)
double value, char *name;
{
    if (errno)
        perror(name);
    else
        printf("%10g %s\n", value, name);
    errno = 0;
}

#define display(x) show((double)(x), #x)

main()
{
    extern char *gets();
    double x;
    char string[64];

    for(;;) {
        printf("Enter a number: ");
        fflush(stdout);
        if(gets(string) == NULL)
            break;

        x = atof(string);
        display(x);
        display(cos(x));
        display(sin(x));
        display(tan(x));
        display(acos(cos(x)));
    }
}

See Also

##
#define,
C preprocessor
ANSI Standard, §6.8.3.2