How to escape a leading # within the C preprocessor

This is probably a no-brainer for most people, but for some reason I banged my head against the wall for about an hour trying to figure this one out.


Oftentimes you have source files (or in our case aspx files) that contain common headers, footers, sections, etc. Instead of paying the runtime hit of dynamically including things within the page, we just use the C preprocessor to #define macros that get expanded at build time. For example, do you ever get sick of writing the <!DOCTYPE> at the top of the page or want to make sure that it's consistent across all of your pages? Well, it's easy to do if you use a C preprocessor macro:

<html xmlns="">

Then in your .aspx page simply do this:


and voilà.


Well, what if you have something like this in your .aspx.pp page (we use a .pp extension to indicate that it's a C preprocessor file that will get renamed after the preprocessor runs):

<style type="text/css">
#element {
background-color: white;

The C preprocessor will barf at you and say something like the following:

fatal error C1021: invalid preprocessor command 'element'


There are a couple different ways of fixing this, although I think the simplest way is to just define a macro that just passes through its argument, like this:

#define IGNORE_HASH(x) x

Then you change the code to this:

<style type="text/css">
IGNORE_HASH(#element) {
background-color: white;

Now the C preprocessor ignores the #element and everything works as expected.

Comments (2)

  1. Ever heard of server side includes?

    They look like this:

    <!–#include virtual="/path_doctype_def" –>

    SSI have been around for something like 12 years, and last time I checked were still supported in

  2. Thanks for the comment, Dimitar. Yes, server side includes are great for some things. However, when you want to use macros, then they’re not quite as handy. It would be a lot harder (and sometimes not possible) to do something like this:

    REGISTER_DIRECTIVE(prefix, namespace, assembly)

    That macro will put all of the same assembly junk (like version, strong name, etc.) so you don’t have to repeat it on every page and it changes with every build. Granted, you could also do it with a server-side include, but why incur the extra runtime overhead and the extra files that you need to deploy on your web server?

    Don’t get me wrong, server side includes have their place. It’s just that for these scenarios they seem like they’re a little overkill.

Skip to main content