Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the .c extension mandatory to source files?

If a file is not saved as .c extension what type of error is generated?

I have been saving the source file as .c and are working properly, but why is .c necessary?

like image 999
Aamir Altaf Kathu Avatar asked Jan 04 '14 10:01

Aamir Altaf Kathu


2 Answers

NO

But that was a brutally short answer. The problem is context of word 'necessary' or 'mandatory'.

As soon as you ask "is the extension '.c' necessary for compilation by 'X' compiler on 'Y' platform?", the answer will depend on X and Y.

As explained by Girjesh gcc on linux does "expect" the file extension to be '.c' for source files. But it can be avoided with options during compilation.

gcc -x c x.l

will compile the x.l file considering it a c source. [if it indeed is a c source]

hence for gcc on *nix '.c' is just a naming convention.

Check remyabel's answer

for more info.

a description from wikipedia on file extension conventions and restrictions

Filename extensions can be considered a type of metadata. They are commonly used to imply information about the way data might be stored in the file. The exact definition, giving the criteria for deciding what part of the file name is its extension, belongs to the rules of the specific filesystem used; usually the extension is the substring which follows the last occurrence, if any, of the dot character (example: txt is the extension of the filename readme.txt, and html the extension of mysite.index.html). On file systems of mainframe systems such as MVS, VMS, and PC systems such as CP/M and derivative systems such as MS-DOS, the extension is a separate namespace from the filename. Under Microsoft's DOS and Windows, extensions such as EXE, COM or BAT indicate that a file is a program executable.

The UNIX-like filesystems use a different model without the segregated extension metadata. The dot character is just another character in the main filename, and filenames can have multiple extensions, usually representing nested transformations, such as files.tar.gz. This model generally requires the full filename to be provided in commands, where the metadata approach often allows the extension to be omitted.

like image 127
Mohit Avatar answered Sep 24 '22 22:09

Mohit


The content of a C source in a .c file is only normal text, like it can be in .txt, .log ...
The only real reason is that we can recognice quickly what files are C code and what are not.

like image 40
deviantfan Avatar answered Sep 21 '22 22:09

deviantfan