I have found this project on Github, a simple snake game with SDL. Inside this project the person seems to be forward declaring a bunch of functions inside a file with the .p extension.
What is this extension? I have never come across this before. They call this file just before the main function, why does C read this as a valid format and seemingly have no issue with it? Is the .p extension an actual part of the language that I have not encountered? If not, is it possible to put any extension on the end of a file and have it compile as long as it is valid C?
Best How To :
snake.p included in that project is a C header file.
C compilers typically care about the name of the source files that they're given on the command line. For example, a C source file might be called
gcc -c foo.c
will cause gcc to assume that it contains C source code. Other suffixes indicate other languages (
.cpp for C,
.adb for Ada, etc.). (There's also an option to specify the language explicitly, but it's so rarely used that I don't even remember what it is, and it's very likely to vary for different compilers.)
Header files (also known as include files), however, are not normally passed as arguments on the compiler's command line, so the same requirements don't apply. There's a nearly universal convention of using a
.h extension for C header files, but when the preprocessor sees a
#include directive, all it really cares about is the contents of the file, not the form of its name.
I've seen a
.inc extension used for unusual include files (that contain function definitions rather than the usual declarations seen in most header files).
In this particular project, the file
snake.p is generated from the
snake.c file using a tool called
cproto. The relevant command in the
@$(CPROTO) $(CPROTOFLAGS) $(INCLUDES) $< -o [email protected] &>/dev/null
which says that
snake.p can be generated from
snake.c by running the
cproto command, which generates C prototypes (function declarations) from C source code containing function definitions. Probably the
.p stands for "prototypes".
It would be more usual to name the
snake.h, since it's just a header file. It would also be more usual not to include that file in the distribution, since it's generated automatically by the build process -- but perhaps the author wanted users who haven't installed
cproto to be able to build the project.
Bottom line: It's just an unusual choice by the author of the project. I can't think of any good reason to use a
.p extension rather than the conventional
.h extension, but it doesn't break anything.