Syntax error near unexpected token `(' ./test.c: l

Solved
dragons2 Posted messages 182 Status Member -  
[Dal] Posted messages 6122 Registration date   Status Contributor Last intervention   -
Hello everyone!
So, I'm starting to learn the C language, I code under Linux using the command:
gcc (filename).c -Wall -o (filename)
then:
./filename
Everything was going well for 2 weeks, but since yesterday I can't execute a program. I keep getting the same error no matter the code.
./test.c: line 2: syntax error near unexpected token ‘(‘
./test.c: line 2: `int main(void)'
I tried to give the file permissions with: chmod +x filename, but nothing works!
The problem is that I often use my computer for university, I'm a computer science student. I need to be able to code on my computer.
Thank you in advance for the help you will provide me.

2 answers

dragons2 Posted messages 182 Status Member 2
 
I copied a code that works, the compilation and execution go well, but the same code copied to the desktop gives me this message in the console:
bash: ./exo7.c: Permission denied

If I then use:
chmod +x folder name I get the message:
./exo7.c: line 2: syntax error near unexpected token « ( » ./exo7.c: line 2: `int main (void)'

So it's indeed a permission issue?
1
[Dal] Posted messages 6122 Registration date   Status Contributor Last intervention   1 108
 
The error is produced by bash, your Linux command interpreter, because you are trying to execute the source code (the .c file) rather than the object code produced as a result of the compilation. Above all, bash first refuses to execute the .c file because it does not have the "x" attribute. Then, when you assign this attribute to it, bash tries to execute the contents of the text file through bash, and of course, since it is not a bash script, it returns a syntax error.

If your compilation command is:
gcc exo7.c -Wall -o exo7


this command produces an executable named "exo7" from "exo7.c"

Under Linux, you run the executable like this:
./exo7
0