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;
}
3
  • 10
    stackoverflow.com/questions/35207515/…
    – Lanting
    Commented Aug 11, 2017 at 7: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
    Commented Aug 11, 2017 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
    Commented Aug 12, 2017 at 5:02

3 Answers 3

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.

10
  • 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? Commented Aug 11, 2017 at 7: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....
    – user2371524
    Commented Aug 11, 2017 at 7:31
  • 5
    It was not a requirement when I started programming in C (1980). ANSI C wasn't standardized until 1989.
    – pojo-guy
    Commented Aug 12, 2017 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
    Commented Aug 13, 2017 at 1:51
  • 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
    Commented Aug 15, 2017 at 6:37
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.

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

1
  • 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
    Commented Feb 17, 2021 at 7:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.