Re: macro expansion

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Matti Linsu <matti.linsu@xxxxxxxxx> writes:

> I have been playing around with gcc Pre-Processor and I cannot explain one 
> behavior.
> The code bellow demonstrate my question.
>
> Input file : 
> --------------------------------------- 
> #define START_WITH_A_NUMBER      0_start_with_a_number
> #define START_WITH_A_LETTER        a_start_with_a_letter
> #define I_LOVE_SPACE                      I       love       space
> START_WITH_A_NUMBER+0
> START_WITH_A_LETTER+0
> I_LOVE_SPACE
> --------------------------------------- 
>
> Pre-processed output (gcc -E -P myfile):
> --------------------------------------- 
> 0_start_with_a_number +0
> a_start_with_a_letter+0
> I love space
> --------------------------------------- 
>
> 1) Why is there a space after '0_start_with_a_number' whereas no space is 
> present after 'a_start_with_a_letter'? 
>
> 2) Why does gcc remove the spaces inside the replacement list of  I_LOVE_SPACE?
> 3) Does it serve a purpose?
> 4) Is there a PP flag to force gcc to pre-process by the standard?

To answer your last question first, the C99 standard describes a
sequence of steps to compile a C program.  gcc is fully standard
compliant in that regard.  The standard does not define the output of
the preprocessor in a textual form.  So there is no standard to which
gcc should conform.

> 1) Why is there a space after '0_start_with_a_number' whereas no space is 
> present after 'a_start_with_a_letter'? 

There is a space after 0_start_with_a_number because gcc emits a space
whenever necessary to prevent tokens from accidentally pasting together.
E.g., a space is required between '+' and '=' in preprocessor output.
The test for whether a space is necessary is conservative.
0_start_with_a_number is not an identifier, so the conservative test
says that a space is required.

> 2) Why does gcc remove the spaces inside the replacement list of  I_LOVE_SPACE?

The spaces in the replacement list of I_LOVE_SPACE are removed because
only one space is required to avoid accidental token pasting.  The
additional spaces serve no purpose and are discarded.

> 3) Does it serve a purpose?

I don't understand this question.


In general the C/C++ preprocessor is not a general purpose text
processing tool.  It is specifically designed as one of the passes in a
C/C++ compiler.  Attempts to use it as a text processing tool are
generally misguided.

Ian


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux