147

I have been looking at the Boost libraries source code, and I have noticed that often there are single pound signs without any preprocessor directives attached to them. I read through the GCC preprocessor manual and specification guide and can't find anything about it.

(1) #ifndef BOOST_CONFIG_HPP
(2) #  include <boost/config.hpp>
(3) #endif
(4) #
(5) #if defined(BOOST_HAS_PRAGMA_ONCE)
(6) #  pragma once
(7) #endif

On line 4, there is nothing after the pound sign. What effect does this have? Is it defined in the C preprocessor (CPP) specification?

As Boost is a cross-platform library, I would assume that any CPP should parse it correctly. What would the effect/side-effects be of having random pound/hash signs throughout the code?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
callyalater
  • 3,102
  • 8
  • 20
  • 27

3 Answers3

189

A # on its own on a line has no effect at all. I assume it's being used for aesthetic value.

The C standard says:

6.10.7 Null directive

Semantics

A preprocessing directive of the form

# new-line

has no effect.

The C++ standard says the same thing:

16.7 Null directive [cpp.null]

A preprocessing directive of the form

# new-line

has no effect.

Community
  • 1
  • 1
Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
  • 4
    This doesn't explain the purpose of using it, though, nor give the rationale for its existence. – StellarVortex Feb 09 '16 at 19:23
  • 10
    _"What effect does this have? Is it defined in the C preprocessor (CPP) specification? ... What would the effect/side-effects be of having random pound/hash signs throughout the code?"_ That's what I answered. It has no effect, but I didn't want to speculate on the author's reason for using it. I have done so now. – Jonathan Wakely Feb 09 '16 at 19:47
  • You have been given correct answers that it means nothing to the preprocessor; I'm going to speculate that it may help some other program (such as an IDE or LINT) keep a block of directives together as a logical unit. Some IDEs let programmers expand or collapse blocks of text to help them keep track of the logical structure of the file. – Spencer Aug 12 '17 at 01:50
110

It makes the source code look pretty, that's all.

Highlights the fact that the whole block is a preprocessor section.

And indeed, both the C and C++ preprocessors must ignore # on a line.

P45 Imminent
  • 8,319
  • 4
  • 35
  • 78
  • 16
    And also makes navigating easier in some text editors (e.g., `{` or `}` in vim). – wchargin Feb 04 '16 at 19:43
  • @WChargin, that depends on how you look at it. If you want to navigate to between the two preprocessor blocks, adding `#` would prevent you from using `{` or `}`. In fact, it may be easier to press `}` twice to jump over the block (in the OP's example) than not be able to jump to the middle of the two blocks. – Shahbaz Feb 07 '16 at 07:29
  • 3
    @Shahbaz Certainly! My rule of thumb is "keep logical units together," so that "paragraph" really means "idea." I would follow this rule with preprocessor declarations, too. Of course, it is a matter of personal style to a large degree. – wchargin Feb 07 '16 at 14:08
47

Always check an authoritative source instead of relying on other resources. C is standardised as ISO 9899::2011, C++ also has an ISO standard. Both are well accepted and the final drafts available by a short search. The C standard states in 6.10.7 (C++ has much the same text):

A preprocessing directive of the form

# new-line

has no effect.

This is a null directive, as much as an ; without a preceeding expression in the core-language is a null statement .

For the preprocessor it is just for formatting/readability to highlight that the lines belong semantically together. (the semicolon OTOH is semantically relevant).

Community
  • 1
  • 1
too honest for this site
  • 12,050
  • 4
  • 30
  • 52