best7861
best7861

Reputation: 29

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

This is my current 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 .exe:

./print_hello: line 3: syntax error near unexpected token ('

./print_hello: line 3: `void print_hello(){'

For compiling I used gcc print_hello.c -o print_hello and to run I use ./print_hello

Upvotes: 2

Views: 588

Answers (2)

Shlomi Agiv
Shlomi Agiv

Reputation: 1198

Your code is correct. I compiled and run it successfully.

What happened is you've accidentally copied print_hello.c to print_hello. You're running this file as a script and get a script error. please make sure you compile correctly before running print_hello.

You can try to delete print_hello then run your gcc again.

This is what happens when I try to run the code as a script(exactly the same error)

>./hello.c
./hello.c: line 3: syntax error near unexpected token `('
./hello.c: line 3: `void print_hello() {'

Upvotes: 3

conectionist
conectionist

Reputation: 2924

Your code seems to be correct. I ran it on my machine and works fine.

I also ran it on ideone and it works fine.

Try reinstalling/updating your gcc. Maybe that will help.

Alternatively, you can create an Ubuntu Virtual Machine and try it there.

Upvotes: -1

Related Questions