441

I'm going through some C course notes, and every C program source file begins with a single # on the first line of the program.

Then there are blank lines, and following that other stuff followed by the main function.

What is the reason for the #?

(It's out of term now and I can't really ask the chap.)

Here's an example:

#

#include <stdio.h>
int main() {
   printf("Hello, World!");
   return 0;
}
muru
  • 4,723
  • 1
  • 34
  • 78
The Main Man
  • 2,433
  • 2
  • 9
  • 8
  • 10
    https://stackoverflow.com/questions/35207515/what-is-the-purpose-of-a-single-pound-hash-sign-on-its-own-line-in-the-c-c/35207652 – Lanting Aug 11 '17 at 07:19
  • 8
    @Bathsheba The time stamps on that last one show that you gave a reasonably detailed answer only one minute after the question was asked, complete with a relevant quote from the C99 reference standard. You're definitely a conspiracy person. Possibly in the Illuminati. How'd both questions end up being so popular though? – Nat Aug 11 '17 at 21:15
  • 3
    For me the popularity is the weird bit. With the edits you do have five minutes of grace to get the edit correct and the edit history up to that point is not shown in the history - just the draft at five minutes and the time of the first attempt. The fact that this question is now linked with the other one will only serve to increase the voting on both. – Bathsheba Aug 12 '17 at 05:02

3 Answers3

624

Wow, this requirement goes way back to the 1970s.

In the very early days of pre-standardised C, if you wanted to invoke the preprocessor, then you had to write a # as the first thing in the first line of a source file. Writing only a # at the top of the file affords flexibility in the placement of the other preprocessor directives.

From an original C draft by the great Dennis Ritchie himself:

12. Compiler control lines

[...] In order to cause [the] preprocessor to be invoked, it is necessary that the very first line of the program begin with #. Since null lines are ignored by the preprocessor, this line need contain no other information.

That document makes for great reading (and allowed me to jump on this question like a mad cat).

I suspect it's the lecturer simply being sentimental - it hasn't been required certainly since ANSI C.

Community
  • 1
  • 1
Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • 22
    As I understand it, the requirement is not that the very first character must be a `#` on a line by itself, just that it starts with a `#`, so why not going directly for a `#include`? Or am I understanding it wrong? – Federico klez Culloca Aug 11 '17 at 07:28
  • 14
    @Bathsheba "*In order to cause this preprocessor to be invoked, it is necessary that the very first line of the program begin with #. Since null lines are ignored by the preprocessor, this line need contain no other information.*" <- so it **can** already contain a preprocessor directive, but it isn't necessary.... –  Aug 11 '17 at 07:31
  • 2
    And they already stated this behavior has to be _fixed_ on (see B.9 on Appendix 2)... – Adriano Repetti Aug 11 '17 at 10:07
  • 5
    It was not a requirement when I started programming in C (1980). ANSI C wasn't standardized until 1989. – pojo-guy Aug 12 '17 at 13:35
  • 29
    @federico-klez-culloca If it becomes an include, the file may end up without a leading # because in the future somebody deletes an unneeded include while unaware of the side effects of leading #. – Fadeway Aug 13 '17 at 01:51
  • Am I understanding correctly that this was simply a bug in the C compiler, which they already knew they had to fix, but they still released it and added these notes to the end user? Well, what can I say, Hm. – Mr Lister Aug 13 '17 at 10:51
  • 3
    @Angew - you're missing the point. By starting with a # on an empty line, you could then follow it with various comment lines (individually or collectively in valid comment form) and *then* follow *those* with the first #include. No one suggested that a # was ever comment symbol in C, though obviously there are other languages where it is – Chris Stratton Aug 13 '17 at 22:59
  • Just wondering, did you use a fake account to ask this question? – Casanova Aug 14 '17 at 23:45
  • 5
    @Casanova - No. Doing that and accepting such an answer contravenes the site rules. Asking and answering under different accounts is against the spirit of the rules of the site even without mutual voting, if you get my meaning. There's no harm in answering your own question and accepting that answer though with the same account - I've done that in the past. – Bathsheba Aug 15 '17 at 06:37
  • 1
    @MrLister I would assume, it was about performance. The C compiler checks for the first byte to be '#' and if it is, it starts the preprocessor. If not, the preprocessing step is skipped entirely, saving some time during compilation. Remember, it was the 1970s. – Bodo Thiesen Mar 03 '19 at 12:43
3

It Does Nothing

As of the ISO standard of C/C++:

A preprocessing directive of the form

# new-line

has no effect.

So in today's compilers, that empty hash does not do anything (like- new-line ; has no functionality).


PS: In * pre-standardized C*, # new-line had an important role, it was used to invoke the C Pre-Processor (as pointed out by @Bathsheba). So, the code here was either written within that time period, or came from the habit of the programmer.


Edit: recently I have come across code like this-

#ifdef ANDROID
#
#define DEVICE_TAG "ANDROID"
#define DEBUG_ENABLED
#
#else
#
#define DEVICE_TAG "NOT_ANDROID"
#
#endif /* ANDROID */

Here, those empty hashes are there only for making the code look good. It also improves readability by indicating that it is a preprocessor block.

Minhas Kamal
  • 20,752
  • 7
  • 62
  • 64
-8

You need to know about the Compilation process of C. Because that is "must know" how the Source code converting into Executable binary code (file).

From the Compilation Process, the C source code has to Cross the pre-processor Section. But how to tell the Compiler to pre-process the code?... That the time # Symbol was introduced to the indicator of Preprocess to the compiler.

For Example #define PI 3.141 is in the Source code. Then it will be change after the Preprocessing session. Means, all the PI will be changed into 3.141.

This like #include <stdio.h>, the standard I/O Functions will be added into your Source code.

If you have a Linux machine, compile like gcc -save-temps source_code.c. And see the compiler outputs.

Meraj al Maksud
  • 1,528
  • 2
  • 22
  • 36
  • 1
    The question isn't about `#` as a prefix. It's asking why you would put a single `#` at the top of each file. – George Feb 17 '21 at 07:43