Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

It is convention and can be indication for compiler, For any given input file, the file name suffix determines what kind of compilation is done:

man gcc:

file.c: C source code which must be preprocessed.
file.i: C source code which should not be preprocessed.
file.h: C, C ++ , Objective-C or Objective-C ++ header file to be turned into a precompiled header.

If you give invalid extension then GCC will give you an error even for valid code:

@:~$ gedit x.l
@:~$ cat x.l
#include<stdio.h>
int main(){
    printf("grijesh");
    return 0;
}
@:~$ gcc x.l
x.l: file not recognized: File format not recognized
collect2: ld returned 1 exit status
@:~$ 

I didn't try other compiler.

file extension is also useful for editor to apply theme.

A filename extension is a suffix (separated from the base filename by a dot or space) to the name of a computer file applied to indicate the encoding (file format) of its contents or usage.

Filename extensions can be considered a type of metadata. They are commonly used to imply information about the data that might be stored in the file.

Thus, it implies the compiler about the type of data stored (sometimes, how data is stored).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top