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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With