-1

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.

FedKad
  • 9,212
  • 7
  • 40
  • 79
best7861
  • 1
  • 2
  • Welcome to askubuntu. There seems to be no obvious error in your code above. But please note that your question is much more suitable for https://stackoverflow.com/ . You should also include some information on how you compile your code ... – dragosht Mar 01 '20 at 06:36
  • 1
    You haven't said what OS/release you are using, but on Ubuntu c programs get compiled to *executable* binaries and not .exe files. Your question is off-topic as written https://askubuntu.com/help/on-topic Usually too the compile and execution/run steps are separated, compile errors occurring at compile time, link step follows then a later step actually executes the code; your wording implies both *compile* and *execute* are done together so you didn't mention what tool as it doesn't sound like `gcc` or `clang` so you should be specific. – guiverc Mar 01 '20 at 06:39
  • Ah okay, Please have a look at the edit. – best7861 Mar 01 '20 at 06:58
  • 1
    you sure you are not executing the source instead –  Mar 01 '20 at 09:11
  • 1
    I just built it and executed on Ubuntu 16.04, it runs well. – kenn Mar 01 '20 at 09:48
  • 4
    Reviewers: This isn't really off-topic. It's not about how to write C++ code. It's a tooling question and it's OS-specific (at least the form it takes is OS-specific, and in practice the confusion here isn't one people face in some popular OSes, e.g., Windows). We have some questions that cover this. I think we should reopen this and close it as a duplicate of one of them, perhaps [Why do I get syntax error when I try to run my C++ program after compiling it?](https://askubuntu.com/q/1024721/22949) (Per the [help/on-topic], "Development on Ubuntu" is on-topic. That has to include *something*.) – Eliah Kagan Mar 02 '20 at 15:03
  • 4
    Does this answer your question? [Why do I get syntax error when I try to run my C++ program after compiling it?](https://askubuntu.com/questions/1024721/why-do-i-get-syntax-error-when-i-try-to-run-my-c-program-after-compiling-it) – Melebius Mar 03 '20 at 08:51

2 Answers2

1

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.

Melebius
  • 11,121
  • 8
  • 50
  • 77
FedKad
  • 9,212
  • 7
  • 40
  • 79
  • How should this help on the syntax-error ? – Soren A Mar 01 '20 at 12:57
  • 2
    The OP is trying the "execute" the source code instead of the binary file produced by the C compiler & linker. I assume that these precise steps will help the OP avoid such pitfalls. – FedKad Mar 01 '20 at 13:28
0

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 : https://appuals.com/fix-syntax-error-near-unexpected-token/

kenn
  • 5,074
  • 12
  • 55
  • 94