11

I am new to C programming. When I tried to compile it, I got an a.out file (I don't know where it came from) and it somehow disappeared. What is happening here?

Here are the commands I ran:

$ gcc hello.c
$ ls
a.out hello hello.c

$ a.out
a.out: command not found

$ gcc a.out
a.out: file not recognized: File truncated
collect2: error: ld returned 1 exit status

$ ./hello
Hello, world!$ ./a.out
bash: a.out: No such file or directory

$ ls
hello hello.c
6
  • 8
    a.out is the default name given to the built executable. You can change it with the appropriate command-line option for your C compiler. Jan 27, 2017 at 17:57
  • As for why it's disappearing, we can only guess (which is bad, and makes your question off-topic btw), but I've had cases where an overprotective antivirus would do that.
    – Siguza
    Jan 27, 2017 at 17:59
  • This a.out: file not recognized: File truncated is done by gcc itself. No mistery there.
    – alvits
    Jan 30, 2017 at 23:11
  • This must be a mega dupe. This is one of the first things a beginner runs into when compiling a C program on Unix-like systems (or only with older versions of compilers?). Feb 19, 2022 at 11:02
  • a.out is claimed to have been deprecated since the early 2000s. Is this in a Turbo C++ environment? Feb 19, 2022 at 11:54

3 Answers 3

28

a.out is the default executable name generated by the gcc. Once you invoke gcc a.out (which you really shouldn't - as it is passing a.out as an input to gcc), it is trying to create a new a.out, but failing as it is trying to read the existing a.out as a source file, which it is not. This is what making it "disappear".

If you want gcc to generate an executable with specific name, use the -o switch followed with the desired name:

gcc hello.c -o hello

will generate the executable named hello, which can be run using

./hello
9
  • 4
    Might be a good idea to mention that the proper usage is gcc -o hello hello.c to get an executable named hello.
    – unwind
    Jan 27, 2017 at 18:06
  • @unwind Sure. Will do
    – Eugene Sh.
    Jan 27, 2017 at 18:06
  • 1
    Maybe add that gcc a.out tries to create a a.out file using a.out as a C source file.
    – Adrien
    Jan 27, 2017 at 18:06
  • 1
    @Yatin The explanation is following this remark, but added the same into it, check it out.
    – Eugene Sh.
    Aug 20, 2020 at 13:51
  • 1
    @Honey stackoverflow.com/questions/6331075/…
    – Eugene Sh.
    Mar 15, 2021 at 13:33
4

a.out is the default executable, since you didn't tell GCC what to name it (by using -o). Just typing a.out won't run it, because it's in the current directory, and Linux doesn't put the current directory in the path like other broken OSes do, so you have to type ./a.out to run it.

You later typed gcc a.out, which erased it, and tells gcc to try to compile the executable as if it were a C program, which makes no sense, so the a.out it produced is empty.

What you meant to do was:

gcc hello.c
./a.out

Or even better:

gcc -o hello hello.c
./hello
2

In addition to the previous answer, I'd suggest you use

gcc -o hello hello.c

This will create an executable called "hello" rather than the default "a.out". Of course you can call it whatever you want.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.