aliasing single quotes in macros 
Author Message
 aliasing single quotes in macros

To all:

Using double quotes in macros is easy to do using the # operator.  Now how
about single quotes.  Suppose you want preprocessor output to look like
this:

        char a = '\012';

given source of:

        #define SPECIAL_CHAR \012
        #define M(chr)  /* unknown macro definition here */

        char a = M(SPECIAL_CHAR);

How would you define the macro M to do the job?

Please try your solution before responding.

/cc



Sat, 25 Mar 2000 03:00:00 GMT  
 aliasing single quotes in macros



Quote:
>To all:

>Using double quotes in macros is easy to do using the # operator.  Now how
>about single quotes.  Suppose you want preprocessor output to look like
>this:

>        char a = '\012';

>given source of:

>        #define SPECIAL_CHAR \012
>        #define M(chr)  /* unknown macro definition here */

>        char a = M(SPECIAL_CHAR);

>How would you define the macro M to do the job?

You wouldn't use a character constant. Remember that character constants
are simply integer values. You can write simply:

         #define SPECIAL_CHAR 012    /* Octal integer constant */

         char a = SPECIAL_CHAR;

--
-----------------------------------------


-----------------------------------------



Sat, 25 Mar 2000 03:00:00 GMT  
 aliasing single quotes in macros

Quote:

>Using double quotes in macros is easy to do using the # operator.  Now how
>about single quotes.

We intentionally did not provide a "charizing" operator analogous to the #
stringizing operator.  Depending on the context, one way to charize is to
stringize and derefernce the resulting string (e.g. by appending [0]).


Sat, 25 Mar 2000 03:00:00 GMT  
 aliasing single quotes in macros



Quote:
>To all:

>Using double quotes in macros is easy to do using the # operator.  Now how
>about single quotes.  Suppose you want preprocessor output to look like
>this:

>    char a = '\012';

>given source of:

>    #define SPECIAL_CHAR \012
>    #define M(chr)  /* unknown macro definition here */

>    char a = M(SPECIAL_CHAR);

>How would you define the macro M to do the job?

hmm. do you need a macro, or can you simply declare a constant
Special_char variable?


Sat, 25 Mar 2000 03:00:00 GMT  
 aliasing single quotes in macros

Quote:

> You wouldn't use a character constant. Remember that character constants
> are simply integer values. You can write simply:
>          #define SPECIAL_CHAR 012    /* Octal integer constant */
>          char a = SPECIAL_CHAR;

This is the best solution in the current context.  I should have mentioned
that I also want to embed SPECIAL_CHAR into constant strings so here is
the complete problem I face.  I need to get preprocessor output of:

        #define SPECIAL_CHAR    012  /* or char constant? */

        char a = 012;           /* or a = '\012' */
        const char *b = "some text with a char \012";

from source similar to:

        #define SPECIAL_CHAR    012  /* or char constant? */
        #define M(text,char)    /* unknown macro definition here */

        char a = 012;           /* or a = '\012' */
        const char *b = M("some text with a char ",SPECIAL_CHAR);

I've been only able to do one or the other assignment but not both.  It
seems to boil down to being able to produce the character \ or ' in a
macro without the preprocessor complaining or producing wrong output.

Thanks,

/cc



Sun, 26 Mar 2000 03:00:00 GMT  
 aliasing single quotes in macros



Quote:
>To all:

>Ok, I finally found a solution which allows the character '\' to be used
>in a macro. If you can enable trigraph expansion on your compiler, you can
>do the following:

>    #define SPECIAL_CHAR    012

>    #define M(a,b) M1(a,??/b)

This is no different from

    #define M(a,b) M1(a,\b)

Trigraph expansion is done every early, in translation phase 1. Backslash
processing is performed in translation phase 2. The following is legal C:

    #define FOO {       ??/
        puts("hello");        ??/
    }

The ??/ have the same effect as backslashes would, causing physical lines to
be folded into logical lines.

Quote:
>which will result in preprocessor output of:

>    a = 012;
>    const char *b = "Special char ""\012";

The output of a preprocessor is, at least conceptually, a sequence of tokens,
not program text.
--


Sun, 26 Mar 2000 03:00:00 GMT  
 aliasing single quotes in macros



Quote:
>To all:

>Ok, I finally found a solution which allows the character '\' to be used
>in a macro. If you can enable trigraph expansion on your compiler, you can
>do the following:

>    #define SPECIAL_CHAR    012

>    #define M(a,b) M1(a,??/b)
>    #define M1(a,b) a ## #b

By the way, your compiler is broken if it behaves differently here with ??/
and /.

The following is a perfectly valid macro definition:

    #define M(a,b) M1(a,\b)

The replacement text is a sequence of tokens, as required. Since the backslash
is not followed immediately by a newline, it is not subject to any special
processing in translation phase 2. A subsequent macro call M("foo ",012)
should result in the single string literal preprocessor token "foo \012".

What compiler are you using? The HP-UX c89 compiler complains about an illegal
backslash if it is not operated in ANSI C mode, but does the right thing when
asked to conform.
--



Sun, 26 Mar 2000 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. aliasing quotes in macros?

2. Single Quote in SQL Expression

3. Stringize with single quotes

4. Single vs. Double Quotes

5. testing for a single of double quote

6. characters enclosed in single-quotes

7. Single quotes around an evaluated #define?

8. Token pasting qith single quotes question

9. Storing single quote in a MSSQL database using JDBC

10. Inserting data containing single quotes into the table

11. Frustrating editor behavior w/ single-quotes

12. handling single quotes in database statements

 

 
Powered by phpBB® Forum Software