Reputation: 1
I've just started coding in C, but no no matter what I try, I keep on getting a syntax error. "Syntax error near unexpected token '('"
#include<stdio.h>
int main()
{
printf("Hello World");
return 0;
}
I've tried compiling it by using
gcc helloworld1.c -o helloworld1
cc helloworld1.c -o helloworld1
and it compiles without errors, but it always screws up and gives me the syntax error message when I try to run it. Any help at all would be appreciated. Thanks!
Upvotes: 0
Views: 3500
Reputation: 1
Don't execute the .c file, just execute the object code eg. a.out
In the terminal type: ./a.out
Tip for C++:
Since the gcc has not returned error in compilation, you don't have error in your header file.
But in the future if you encounter (say) iostream.h
not found, try giving an absolute path like:
/usr/include/c++/7/iostream
instead of iostream.h
in the header file
Upvotes: 0
Reputation: 35154
According to BLUEPIXY's comment, it's quite clear that you call ./helloworld1.c
instead of ./helloworld1
. Thereby, your shell is expecting a shell script, but the C source code isn't a valid script (like bash or something like that). Hence, the shell (and not the C compiler) gives you a syntax error which doesn't have anything to do with the lanaguage C at all. Tried it with a file test.c
into which I copied exactly the following lines of code:
test.c
#include<stdio.h>
int main()
{
printf("Hello World");
return 0;
}
Then I called chmod +ux test.c
to make it an executable, and finally called it in the terminal (which expects a bash script):
./test.c
yields:
./test.c: line 2: syntax error near unexpected token `('
./test.c: line 2: `int main()'
The error is at line 2, because the first line #include...
starts with a #
and is therefore interpreted as a comment.
Upvotes: 5