Programming C – How to Run a C Program

cprogramming

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.c

and run by this command

./file.c

those 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 ??

Best Answer

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
Related Question