1

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

2

5 Answers 5

8

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
1
  • GCC is so basic that it's installed by default in Ubuntu as part of build-essential, so is installed already; technically sudo apt-get install gcc updates it.
    – kos
    Nov 4, 2015 at 4:21
3

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

1
  • There goes my clear distinction between compiled and interpreted languages. Oh my... ;).
    – MadMike
    Mar 20, 2017 at 7:32
1

AFAIK, you have to compile C code before executing it like so:

gcc file.c
0
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.

0

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 gcc

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

and 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.out

The 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 gcc

That should keep you going for a couple of years... welcome to coding - Enjoy!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .