Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any rules about underscores in filenames in C/C++?

Tags:

c++

c

filenames

I know that there are rules for using underscores in identifiers in C/C++. Are there any rules for using them in source code filenames?

For instance, are there any restrictions against beginning or ending a filename with an underscore? Or having an underscore as the last character before the .c or .h extension? Double underscores?

References are appreciated, if there are any.

like image 914
cp.engr Avatar asked Jan 16 '17 19:01

cp.engr


1 Answers

If the source files are subject for preprocessor #include directives, then the C and C++ standards specify a minimum set of requirements on the filename. The C standard says:

6.10.2 Source file inclusion

...

  1. The implementation shall provide unique mappings for sequences consisting of one or more nondigits or digits (6.4.2.1) followed by a period (.) and a single nondigit. The first character shall not be a digit. The implementation may ignore distinctions of alphabetical case and restrict the mapping to eight significant characters before the period.

... where nondigit contains the letters A-Z, a-z and underscore.

The exact same text (except for the paragraph numbers) can also be found in the C++ standard, 16.2 Source file inclusion.

Beyond that, what passes for a valid filename depends on the operating system, file system, compiler, linker and other parts of the compilation tool chain.

These days, I'd expect most modern systems to allow almost anything that isn't directly forbidden by the file system.

References

  • The final public draft of the C11 standard, n1570
  • The final public draft of the C++11 standard, n3337
like image 51
Nisse Engström Avatar answered Oct 14 '22 06:10

Nisse Engström