Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

jamesapp

macrumors 6502a
Original poster
just wondering about compiling c programs
i typed in and ran a program which i called hello.c
the program compiled and ran successfully, i saved this program to a folder on my computer. it created an executable file called a.out. when i tried to compile another program which i called fahr.c it compiled, but when i listed the contents of my folder there were only 3 files. a.out, fahr.c and hello.c.
when i ran a.out it ran the file which i had last compiled fahr.c.
just wondering what i should do if i want to have multiple files in the same directory?
one thing i will add is that when i compiled my second program like this

Code:
gcc fahr.c -o a1.out

it created an executable file called a1.out
and when i listed the contents of the folder where i am storing my files,
there were four files in the folder.
just wondering if this is how i should be compiling my programs?
 
Yes, use the -o option to set names. But don't call them a1.out a2.out or whatever, call them something reasonable and rememberable.
 
It's normally easiest to call your binary the same thing as your source file that contains int main. That makes it much easier to identify. So for example:
gcc -o hello hello.c
./hello

gcc -o fahr fahr.c
./fahr

It's not strictly necessary, but you may want the output to go into a separate bin folder to keep your source and output a bit tidier, but this certainly isn't necessary.

-Lee
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.