I have compiled other programs before but, for some reason, I can't work anymore. I have a mac Here's my code. Its purpose is to take three numbers and find the average.
printf ("This program will find the average of 3 numbers.\n");
printf ("Type the first number.\n");
scanf ("%d", &first);
printf ("Type the second number.\n");
scanf ("%d", &second);
printf ("Type the third number.\n");
scanf ("%d", &third);
average = (first+second+third)/2.0;
printf ("The average of %d, %d, and %d is %.3f\n", first, second, third, average);
These are the steps I have taken. I put average.c and average.h on the desktop. I open terminal and type
gcc /Users/reneelion/Desktop/average.c average
Then it says:
average: No such file or directory.
I am a beginner at coding and don't know what to do.
I assume you want the output file to be
average? Then use this:
gcc average.c -o average
compiler is unable to find Your file.You need to do as follows(According to youe environment)
cd /Users/reneelion/Desktop/ gcc average.c
This will give an executable file(In case of no Errors) and on running that will give desired output.
cd Desktop and
gcc /Users/reneelion/Desktop/[filename] is redundant.
Once you have changed your directory to the desktop, you no longer need to write the full filepath. You can write the path to your file as the relation between your current directory and the file. In your case:
/Users/reneelion> //you begin (most likely) in your user directory /Users/reneelion> cd Desktop //change directory to desktop /Users/reneelion/Desktop> gcc average.c //now you are in the desktop folder, //no need to rewrite the full path //to your file
The syntax for using the GCC (GNU Compiler Collection) is as follows:
gcc [options] [source files] [object files] [-o output file]
You can use different options to do things such as hiding error messages and getting debug information.
Are the files (or file) you want to compile. In your case
Object files contain low level instructions and are created when you compile your code. Executables are created by linking these object files together. You may recognize the
See: What's an object file in C?
[-o output file]
-o is a special option in GCC that names the output of a compiled file. In your case it appears you are trying to name your output
average. If you don't use this option, your program will still compile and can be run by using the default
What you typed would most likely be interpreted as an attempt to combine a source file "
average.c" with an object file called "
average". Since there is as of yet no file object or otherwise called "
average" your call is not working.
Putting it all together:
cd Desktop //change directory to desktop gcc average.c -o average //compile average.c into an executable called average. ./average //run the executable