1

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.

#include <stdio.h>
int main(){
int first;
int second;
int third;
float average=0.0;
    printf ("This program will find the average of 3 numbers.\n");
    delay(1000);
    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);
return (0);
}

These are the steps I have taken. I put average.c and average.h on the desktop. I open terminal and type

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

jw013
  • 1,718
  • 17
  • 22
user2607534
  • 211
  • 3
  • 4
  • 9

3 Answers3

9

I assume you want the output file to be average? Then use this:

gcc average.c -o average
orlp
  • 112,504
  • 36
  • 218
  • 315
0

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.

Dayal rai
  • 6,548
  • 22
  • 29
0

Using 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]

[Options]
You can use different options to do things such as hiding error messages and getting debug information.

[source files]
Are the files (or file) you want to compile. In your case average.c.

[object files]
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 .o extension.
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 a.out executable.

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 
Community
  • 1
  • 1
Enigmadan
  • 3,398
  • 2
  • 23
  • 35