C is not an interpreted language like Python or Perl. You cannot simply type C code and then tell the shell to execute the file. You need to compile the C file with a C compiler like gcc then execute the binary file it outputs.
For example, running gcc file.c will output a binary file with the name a.out. You can then tell the shell to execute the binary file by specifying the files full path ./a.out.
Edit:
As some comments and other answers have stated, there are some C interpreters that exist. However, I would argue that C compilers are more popular.
C is not an interpreted language like Python or Perl. You cannot simply type C code and then tell the shell to execute the file. You need to compile the C file with a C compiler like gcc then execute the binary file it outputs.
For example, running gcc file.c will output a binary file with the name a.out. You can then tell the shell to execute the binary file by specifying the files full path ./a.out.
Edit:
As some comments and other answers have stated, there are some C interpreters that exist. However, I would argue that C compilers are more popular.
If you have a single foo.c, see if make can compile it for you:
make foo
No makefiles or anything needed.
How do I run my C program in Mac Terminal?
How Do I Start Programming in C on a Linux Machine That Runs on Arch?
How to run C in windows
How to run c++ files in the command line?
What Does the -static Flag Do When Compiling a C Program?
How Can You Include Debugging Symbols in a Compiled C Program?
How Do You Generate Assembly Code from a C Source File?
Videos
You need to compile your program before you can run it. To do this, you'll need a C compiler, like gcc. You can install this with:
sudo apt-get install gcc
Then, to compile your program, creating an executable called file:
gcc -Wall -o file file.c
Which you should then be able to run:
./file
Fabrice Bellard's TCC seems to be still in the repositories. It can run in a kind of interpreter-mode which makes the following possible:
You can make a simple C-file executable like the OP tried to do by adding the line
#!/usr/bin/tcc -run
to the very top of the file.
It also accepts input from STDIN by adding an empty option (just the minus sign -) at the end.
$ /usr/bin/tcc -run - <<EOF
> #include <stdio.h>
> int main()
> {
> printf("Hello World\n");
> return 0;
> }
> EOF
Hello World
or with echo
echo '#include <stdio.h> int main(){printf("Hello World\n");return 0;}' | /usr/bin/tcc -run -
or just run /usr/bin/tcc -run - type your code and start the run with CTRL + D
Seems useless and silly but the last method is the fastest (for me, YMMV etc.) to check for a function in a large library, look up the exact value of a constant etc. And it is small (180k) which makes it a good fit for e.g. the Raspberry-Pi.
Main disadvantage: development stopped (last version is from 2013).
Simply just do gcc filename.c && ./a.out. This will compile and run the code using GCC.
You're trying to run the wrong file. The compiler wrote your program to filename, but all error messages look as if you were trying to run the source code, filename.c, directly.
(Make sure you've ran the compiler correctly; too – it's gcc -o, not gcc o-.)
I'm trying to run a file that I made in the Mac Terminal. How do I do this? I don't really understand how to. I wrote my file in emacs in the terminal, if that makes sense/helps.