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 deniedI 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?
21 Answer
You cannot execute an file ".c" from shell. You must compile it first.
For example: We have an file called "file.c"
- Open a terminal
- Use gcc for compile the file and make an executable (
gcc file.c -o executable) - Now you can open the executable file since shell (just go to the folder and execute
./executable