totn C Language

C Language: File Naming

This C tutorial explains file naming in the C language.

Description

When programming in the C language, these are some of the file extensions that you will encounter:

File ExtensionType of FileExample
.cC source filetechonthenet.c
.ccC++ source filetechonthenet.cc
.cppC++ source filetechonthenet.cpp
.oC/C++ object filetechonthenet.o
.hC/C++ header filetechonthenet.h
.exeMicrosoft Windows executabletechonthenet.exe
.comMicrosoft Windows executabletechonthenet.com

Now let's take a moment to explain some of these file extensions.

Source File Naming

It is common practice across most platforms (ie: UNIX, Microsoft Windows, etc) that C source code files end with the ".c" extension.

The following is an example of what you may see in UNIX:

$ ls
techonthenet.c

This is in contrast to C++ source code files which can and do vary in ending from ".cc" to".cpp".

For example, you may see the following C++ naming in Microsoft Windows:

> dir
techonthenet.cpp

Object File Naming

In C, you can compile source file into non-executable object files that end in ".o" extension. This is commonly done so that the object files may be linked together at a later time.

To tell gcc to generate an object file instead of an executable, you provide gcc with the -c option as follows:

$ gcc techonthenet.c -c
$ ls
techonthenet.c	techonthenet.o

In this example, gcc created an object file called techonthenet.o.

Executable File Naming

While naming of linked executables does vary by platform, most compilers save compiled program code into a file called "a.out" unless otherwise told to.

For example:

$ gcc techonthenet.c
$ ls
a.out		techonthenet.c

In this C example, gcc created an executable called a.out.

If you want the compiled and linked program to be named something other than a.out, you can provide the compiler with a -o option. This tells the compiler to output the program into the provided file name.

In this example, we are telling gcc to output the program into the file called techonthenet.

$ gcc techonthenet.c -o techonthenet
$ ls
techonthenet	techonthenet.c

Programs in UNIX typically don't have a file extension whereas Microsoft Windows applications will have either ".com" or ".exe" as their extensions.

For example, this is what you may see in Microsoft Windows:

> dir
techonthenet.exe

In this example, the C executable file is called techonthenet.exe.