Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't run C program: "a.out: command not found"

Tags:

terminal

sh

I have written my first program in C. I compiled it and it put a file on the desktop called a.out(as the book i am reading told me it should), but when i type the command a.out into terminal and run it it says -bash: a.out: command not found. Why is it saying this?

According to the book "Programming in C" by Stephen Kochan, what i am doing is correct because i am in the right directory(desktop), i believe. It also says that if the file is not in the correct path i can either add it to the path or run ./a.out, this method works and runs the program, why is this?

like image 851
Justin Avatar asked Apr 11 '14 23:04

Justin


People also ask

How do you run C file a out?

Run the command chmod a+x a. out to give the user the right to run the file. After that you can execute the file by running ./a. out in a terminal.

What is the A Out command in C?

7. 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.

Why is my C program not showing output?

1) After compilation and execution using ctrl + F9, go to menu window->output. You'll see the latest output window. 2) Just add getch() or getche() function at the end of your main(). It will hold the window for user input ( Specifically waits for character) and you can see your output.

What does command not found mean in C?

The error “Command not found” means that the command isn't in your search path.


1 Answers

When you type a command name (a.out is no different from any other command name in this respect), the shell searches for an executable file with that name. It performs this search using a list of directory names stored in your $PATH environment variable.

You can see your current $PATH by typing

echo $PATH

at your shell prompt. A typical value might be something like

/usr/bin:/bin

though you'll probably have some additional directories as well.

Since a.out is in your current working directory (type pwd to see what directory that is), and your current working directory probably isn't in your $PATH, you can't execute it by typing just a.out.

Since you can refer to your current directory as ., you can (and should) execute the command by typing

./a.out

NOTE: You can have . in your $PATH, but it's considered a bad idea to do so, since it makes it too easy to execute random commands accidentally. If . is at the front of your $PATH, imagine that I ask you to cd to my directory and type ls -- but I've installed a file called ls that does something nasty. Putting . at the end of your $PATH alleviates that risk, but it doesn't eliminate it entirely. It's best to cultivate the habit of preceding a file name with ./ if you want to execute it from the current directory.

(I've ignored the fact that aliases, functions, and shell built-in commands can also be executed this way.)

like image 57
Keith Thompson Avatar answered Oct 19 '22 07:10

Keith Thompson