View Full Version : beginner help in C compiling

Jun 12, 2009, 01:50 PM

I was writing a simple hello world program in C, i have compiling the program in terminal using the code

$ gcc project.c

i got the file a.out

now i want to run it i use the code

$ a.out

but i get this error:

-bash: a.out: command not found

here is the source code for hello world program:

#include <stdio.h>
int main (void)
printf ("Programming is fun.\n");
return 0;

Jun 12, 2009, 02:09 PM
Try this:


The current directory normally isn't in the path for security reasons. Imagine if someone created a virus and named the executable "ls".

Jun 12, 2009, 03:26 PM
it worked but i didn't understand what was wrong


Jun 12, 2009, 03:53 PM
You gave no full path for the file you wanted to run, so the system searched through the directories in your PATH environment variable. The current directory, referred to as ., is not in PATH by default for security reasons. This means that if you want to run a program that is in your current directory, you need to give an explicit path.
Is a path to the file a.out in the current directory.


Jun 12, 2009, 04:02 PM
To make "security reasons" a bit more concrete, the idea is that if the current directory would be in the path, there could be malicious programs in certain directories which would have the name of a popular unix command, but misspelled (for instance, "mroe" instead of "more"). I you happened to be in that directory and you'd type in "mroe", instead of getting "-bash: mroe: command not found", the malicious program would run.

Even worse would be to have the current working directory as one of the first directories in the path, since even correctly spelled unix commands could be "hijacked" that way.

Jun 12, 2009, 04:08 PM
On UNIX, every command is a file somewhere. This is different on DOS/Windows, where some commands (like DIR) are built in to the shell. When you type a command on UNIX, the system has to know where to look for the file. Try typing this in the terminal:

echo $PATH

You will get a list of directories where the system will look if you type a command without a full path.


For example, when you type "ls", it will try "/usr/bin/ls" and find that that file does not exist. Then it will try "/bin/ls" and find that that file does exist, so it will run it.

The current directory is always represented by the dot. Since there isn't a "." in the $PATH variable, the system doesn't know where to find your file. The reason that there is not "." in the $PATH is that you always want to know exactly what command you are running. Consider what would happen if you set your path to this:


Now, since the first directory in the path is ".", you could just type "a.out" to run your program. However, I could create a program that erases your entire hard drive, and call the file "ls". Then if you happened to be in the directory where I put that file and tried to get a directory listing, my program would run instead of the system "ls" that you were expecting.

(I just noticed that a couple others replied as I was typing this, so most of this has been said already in one way or another.)

Jun 13, 2009, 04:04 AM
ok now i got it, thanks for the help :)