Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I execute a .c file?

Tags:

c

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?

like image 329
WCKennedays Avatar asked Feb 19 '26 05:02

WCKennedays


1 Answers

C files cannot be executed, they must be compiled into an executable first.

For example:

Given a C 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. Run the executable file, by literally just typing ./executable
like image 158
Jorge Espinar Avatar answered Feb 20 '26 20:02

Jorge Espinar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!