How do I run my C program?
I am a newbie in C programming language. I am following a course right now, but I have a small problem which is run the file for testing.
I have opened gedit and I wrote those lines of code:
int main(int argc, char *argv[])
{ puts("Hello world."); return 0;
}and when I try to run it after giving the right permissions with this command
chmod +x file.cand run by this command
./file.cthose lines show up in terminal
./file.c: line 1: syntax error near unexpected token `('
./file.c: line 1: `int main (int argc, char *argv[])'Where is the problem ??
25 Answers
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 gccThen, to compile your program, creating an executable called file:
gcc -Wall -o file file.cWhich you should then be able to run:
./file 1 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 Worldor 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).
1AFAIK, you have to compile C code before executing it like so:
gcc file.c 0 C language unlike python is not an interpreting language. The C source code or simply the code written need to be compiled first to make it executable i.e, machine readable form. You have to first compile it by using command
cc -Wall filename.c
cc - GCC compiler
-Wall - Checks for error
filename.c - your file in which C code is saved with .C extension.
You will get an executable file named "a.out" in the working directory of your source code.
I have a small problem which is run the file
To answer your question:
Where is the problem ??
it is enough to say that you cannot run the executable file yet because you have not yet produced it. Your file, file.c, is the "source code" written in the language of "C". This source code is not something that is executable because the C language was designed to be writeable in an easy to understand way (at least 'easy' by human standards... even then, it's not easy at first - but it get's easier). The (source) code you write is then converted into a form that a computer can read easily. This process is called 'compiling'. Compiling creates a file which is executable by a computer. It is this step that you have not done. If you type
which gccand you get back something like /usr/bin/gcc then you have gcc already installed on your system. If you get back nothing - then you dont. If you don't have gcc then you can get gcc (provided you have the correct privileges) by doing:
sudo apt install gcc(NB: apt might not work if you're on some older release of the linux distribution - you can replace it with apt-get)
If you do have gcc then go right ahead and type:
gcc file.cand you will find an executable file called a.out has magically appeared in your directory. This is the file you've been looking for. Run it with
./a.outThe other answers have other things like -o and -Wall which you can learn about through the manual for gcc which you can access by typing:
man gccThat should keep you going for a couple of years... welcome to coding - Enjoy!
More in general
"Zoraya ter Beek, age 29, just died by assisted suicide in the Netherlands. She was physically healthy, but psychologically depressed. It's an abomination that an entire society would actively facilitate, even encourage, someone ending their own life because they had no hope. Th…"