How Do I Execute A. C File?

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

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

Alexander Ross
Author

Alexander Ross

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.