-2

I am running Ubuntu on a virtual machine with gcc downloaded. I wrote up a code in gedit which contains:

#include <stdio.h>



/* This is a comment. */

int main(int argc, char *argv[])

{

    int distance = 100;



    // this is also a comment

    printf("You are %d miles away.\n", distance);



    return 0;

}

When I do the make Ex1.c it says that my file is 'up to date.' so I type in ./Ex1.c and it gives me these errors:

./Ex1.c: line 3: /bin: Is a directory

./Ex1.c: line 4: syntax error near unexpected token '('

./Ex1.c: line 4: 'int main(int arc, char*argv[])'

I don't understand this, I thought it might be how I am typing the code in but then I pasted the code in from the 'Learn C the Hard Way' GitHub and I still get these errors! I just want to run my dang code!

CinCout
  • 9,486
  • 12
  • 49
  • 67
  • Is there another file in the directory? Called a.out, for example? – Ninj0r Dec 27 '16 at 06:14
  • You don't run the .c file, that's the code file, you need to run the compiled binary. – Ninj0r Dec 27 '16 at 06:15
  • you don't rune the `.c` file you should execute the compiled output. most probably `./a.out` – bansi Dec 27 '16 at 06:15
  • 1
    You can't execute the source file directly in the shell! You have to actually *compile* it. Please read some tutorials, or better yet [find a good beginners book](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) to read. – Some programmer dude Dec 27 '16 at 06:15
  • It compiles on an online compiler. Check it out http://ideone.com/S9zVBB. So problem should be about your virtual machine – cokceken Dec 27 '16 at 06:16
  • Use `ls` to look for other files in the directory. The binary would have been created when you compiled the code file, the .c file. – Ninj0r Dec 27 '16 at 06:18
  • 1
    You have to make an executable file, not your source file (you make a product, not its raw materials; the latter are already there and don't need to be made ). Try `make Ex1`. – n. m. could be an AI Dec 27 '16 at 06:23
  • 1
    @Someprogrammerdude Have you tried to run his .c file? I suggest you do and try to figure out why the error messages are what they are ;) – n. m. could be an AI Dec 27 '16 at 06:26
  • 1
    you should read about C before code in C. [The Definitive C Book Guide and List](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) – Stargateur Dec 27 '16 at 06:30
  • @n.m. That `/*` comment starter is a likely candidate. The line numbering threw me off a little too. I think it time for breakfast... :) – Some programmer dude Dec 27 '16 at 06:32

2 Answers2

0

Do it this way.

  1. Open terminal write gedit ex1.c

  2. In the new gedit window write the code.

  3. Close the gedit window.

  4. In the terminal write gcc ex1.c

  5. In the terminal write ./a.out

lu5er
  • 3,229
  • 2
  • 29
  • 50
-1

You're trying to run the source code file. You have to run the compiled binary.

The compiled binary is often called a.out.

Try doing ./a.out

Ninj0r
  • 189
  • 1
  • 4
  • 1
    Why has this answer been downvoted? It perfectly described the reason for the error message. – Gerhardh Dec 27 '16 at 09:41
  • My guess would be s/he thought I posted this after the accepted, when I actually posted this answer before the accepted one. – Ninj0r Dec 28 '16 at 04:13