Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of a single pound/hash sign (#) on its own line in the C/C++ preprocessor?

People also ask

What is the use of hash symbol?

The word hashtag, used to refer to the symbol (#) in Twitter, is a combination of the word hash from hash mark and the word tag, a way to mark something as belonging to a specific category.

What does a pound sign represent?

pound sign in American English 1. a symbol (£) for “pound” or “pounds” as a monetary unit of the United Kingdom. 2. a symbol (#) for “pound” or “pounds” as a unit of weight or mass.


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.


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.


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).