(First C Program / Hello World) Keep getting Syntax Error [duplicate]

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.

7

2 Answers

You are probably trying to "execute" the source code instead of the binary file produced by the C compiler & linker.

Please:

  1. Go to the directory containing your C program.

  2. Remove file print_hello using command: rm -f print_hello.

  3. Correct the permissions of print_hello.c file using command: chmod 640 print_hello.c

  4. Run the command: gcc print_hello.c -o print_hello and ensure that it does not output any error message.

  5. Ensure that a new executable is created in the current directory by checking the output of the command: file print_hello.

  6. Run the new executable using the command: ./print_hello.

Note: After you edit (change) your source code, just re-run steps 4 and 6.

2

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

For more info :

You Might Also Like