How do I execute a .c file?

I have a .c file with a program (obviously written in C):

#include <stdlib.h>
int main(int argc, char** argv) { printf("Hello World\n"); return 0;
}

I'm having problems running it.

At first, this happened:

$ ./file.c
bash: ./file.c: Permission denied

I then added execute permissions with chmod +x file.c, but it still didn't work:

$ ./file.c
./file.c: line 3: syntax error near unexpected token `('
./file.c: line 3: `int main(int argc, char** argv) {'

However, as far as I know, this C program should be syntactically correct.

How do I execute it?

2

1 Answer

You cannot execute an file ".c" from shell. You must compile it first.

For example: We have an file called "file.c"

  1. Open a terminal
  2. Use gcc for compile the file and make an executable (gcc file.c -o executable)
  3. Now you can open the executable file since shell (just go to the folder and execute ./executable
0

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like