This is my C code:
#include <stdio.h>
void print_hello() { printf("Hello n10321234, welcome to BSB211");
}
int main() { print_hello(); return 0;
}However, I keep getting the following errors when I compile and run the executable.
./print_hello: line 3: syntax error near unexpected token ('
./print_hello: line 3: `void print_hello(){'For compiling, I use gcc print_hello.c -o print_hello
and to run I use ./print_hello.
2 Answers
You are probably trying to "execute" the source code instead of the binary file produced by the C compiler & linker.
Please:
Go to the directory containing your C program.
Remove file print_hello using command:
rm -f print_hello.Correct the permissions of print_hello.c file using command:
chmod 640 print_hello.cRun the command:
gcc print_hello.c -o print_helloand ensure that it does not output any error message.Ensure that a new executable is created in the current directory by checking the output of the command:
file print_hello.Run the new executable using the command:
./print_hello.
Note: After you edit (change) your source code, just re-run steps 4 and 6.
2Most probably your source code was written or edited in a non-Unix environment and you try to compile it in Ubuntu.
The error message syntax error near unexpected token `(‘ occurs in a Unix-type environment, Cygwin, and in the command-line interface in Windows. This error will most probably be triggered when you try to run a shell script which was edited or created in older DOS/Windows or Mac systems.
In such case you can use dos2unix tool to convert it.
dos2unix yoursourcecode.cFor more info :