Why can't I run this C program?

You can't just run ./fork.c. It's not a program; it's the source for a program. Using ./ assumes that the file is a script (which it isn't) and treats it accordingly.

However, as noted in another answer, there are compilers (like Tiny C Compiler) that can execute C code without explicitly compiling it.

Since it's a C program, you have to compile the program. Try cc -o fork fork.c then ./fork; it worked here.


That's not a program, that's the source code for a program.

C is a compiled language, meaning it must be "compiled" into machine-readable instructions before you can run it. As you are using C, the "C Compiler" (cc) can do this.

cc -o fork for.c   # compile the code
chmod +x fork      # ensure it it executable
./fork             # run the compiled program

As you move on to more complicated programs, using multiple source files and external libraries, you'll likely move on to using the "GNU Compiler Collection" (gcc) and make to describe how to turn the source code into a working executable.

This question has various information on the difference between scripts (as you are attempting to treat your source code) and compiled programs.

Tags:

C

Compiler