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?
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.
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.
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.
The error “Command not found” means that the command isn't in your search path.
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With