0

I'm just starting to learn C, and I wondered:

  • What is the difference between a compiled filename and a source filename?

For instance, why would I want to have a compiled filename cat and a source filename cat.c?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
TheMathAI
  • 97
  • 6
  • 2
    Because `cat.c` is the source code that is compiled and linked to create the executable file `cat`. The first is text, the second is binary. It's like the difference between a pile of parts and a car - the first is pretty much useless as is, while the second can take you on a trip to the market. Or a cow and a steak - the first can walk around and eat your lawn, while you can actually cook and eat the second. – Ken White Nov 09 '21 at 02:33
  • 1
    You can't run `cat.c` but you can run `cat`. You can't compile `cat` but you can compile `cat.c`. – kaylum Nov 09 '21 at 02:36
  • @KenWhite Ok, so this means that my `cat` file is `a.out`? – TheMathAI Nov 09 '21 at 02:36
  • 1
    No, actually. I should have been more clear. `cat.c` is the source, which is compiled into the binary object file `cat`, which is then linked (along with any necessary libraries or other object files) into `cat.out`, which you can actually run. See the post that @PalLaden gave you, and replace *assembly code* with *c code*; it is explained pretty well there. – Ken White Nov 09 '21 at 02:38

3 Answers3

1

The name of the executable file can be anything, as long as the operating system is okay with it.

By default, some build tools name the executable file a.out. In many common Unix tools, this is easily changed with the command-line switch -o name, which says to put the output file in a file named name.

Executable files automatically become commands because, when a command is typed in a command-line shell and it is not a command built into the shell, the shell looks for a file with that name and, if it is executable, it executes the file. When making use of this, you would like to have the file name be meaningful to the user. a.out is not generic and not particularly meaningful, so programs are usually given other names.

For simple programs, the name of the sole source file or the primary source file is often used as the name of the executable file (or vice-versa) except that the source file as an extension designating its programming language (like .c) and the executable file either has no extension or has some extension like .exe (for “executable”).

Command-line shells might look only in certain directories for executable files. Often they can be told to search in the current directory, but that is not a good idea because it makes it too easy to run one program by accident when another is intended. In particular, an attacker might be able to get a person or program to run a program that the attacker controls if the shell is lax about where it searches for executable files. Because of this, you usually need to run programs in the current directory using ./name, which indicates you deliberately want to run a program in the current directory.

Many people designate a directory in their home folder where they put executable files and add that directory to the path their shell searches for executables.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
0

In compiled languages like C and C++, you can only run a compiled file. In these languages, you cannot run a source file. You compile the text-based source file into the binary compiled file (executable) so you can run the program.

You can name either file almost anything you want, but they must remain separate and distinct files, since one is generated from the other.

Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
0

A programming language is either 'interpreted' or 'compiled' (or both).

An interpreted language is like Python: you can type in individual expressions and they will be evaluated when you press enter.

C and Java are both compiled languages: You cannot enter individual commands. Rather, you have to "compile" the code into an executable, binary format first.

By convention, if we have a C program that we want to call prog, we will have our C source code in prog.c, and compile our executable to the file prog.

In case you are wondering how to compile a C program, gcc -o prog prog.c is good to start with. Feel free to comment if you have questions about compiling.

squidwardsface
  • 389
  • 1
  • 7
  • 1
    Just a side note: Actually, there are compilers for Python, and interpreters for C. Granted, the common way is much more, erm, common. And then there are all kinds of "in-betweens". – the busybee Nov 09 '21 at 07:36
  • @thebusybee Very true! SML and Scala are also examples of languages that can be either interpreted or compiled. Here's a cursed question: could GDB be considered a C interpreter – squidwardsface Nov 09 '21 at 15:08