0

I am fairly new to C coding and cannot get a simple C hello world code to work with Visual Studio Code. The code looks like this:

#include <stdio.h>
int main() {
   // printf() displays the string inside quotation
   printf("Hello, World!");
   return 0;
} 

and I get two error messages:

line 2: syntax error near unexpected token `('
line 2: `int main() {'

Any suggestions on what can be going wrong?

Kulfy
  • 17,416
  • 26
  • 64
  • 103
user1146311
  • 19
  • 1
  • 4
  • 1
    Seems you have incorrectly configured the build task for the file. Since VS Code relies on file name for auto-detection of build tasks, what is the file name of this source code? Which build task are you selecting when you click Ctrl+Shift+B? Do you see any logs in the in-built terminal of VS Code? The error seems to be from BASH which obviously can't compile C programs. – Kulfy Nov 09 '20 at 11:39
  • 1
    hi, the file name is simply test2.c, but i dont get any option to select anything when ctrl+shift+b. The terminal says: "> Executing task: /usr/bin/gcc-9 -g /home/myname/Downloads/test2.c -o /home/myname/Downloads/test2 < Terminal will be reused by tasks, press any key to close it – user1146311 Nov 09 '20 at 11:49
  • OK so VS Code has compiled the code. Did you run any command and encountered the mentioned error? – Kulfy Nov 09 '20 at 11:52
  • I am just doing ./test2.c in the directory i have saved it – user1146311 Nov 09 '20 at 11:55
  • You have to compile the c-code, and then run the a.out binary file ... – Soren A Nov 09 '20 at 11:58
  • 2
    @user1146311 That's what you're doing wrong :) VS Code created an executable with name as `test`. You need to run that executable not the C code. If you want to do that from VS, either define a new task or follow the below answer. But if you want to run from terminal, see [How do I run my C program?](https://askubuntu.com/q/693650/816190) – Kulfy Nov 09 '20 at 12:00
  • @SorenA You don't have to do anything but right clivk this code and select *Run Code* to run it in Visual Studio code. – karel Nov 09 '20 at 12:00
  • @SorenA I assume OP is trying to run the executable created by VS. VS creates executable with the name same as the source code (due `-o` in task definition) unless build task is modified. So, `a.out` might be irrelevant unless OP compiles using `gcc fileName` only. – Kulfy Nov 09 '20 at 12:02
  • @Kulfy Thank u very much, it works now :) – user1146311 Nov 09 '20 at 12:06

1 Answers1

1

Here is a screenshot of the C code in the question (hello.c) running correctly in Visual Studio Code. In order to run hello.c in Code Runner right-click the code and select Run Code.

enter image description here
(Click image to enlarge)

Please note in the left pane that I have the C/C++ extension and the Code Runner extension installed in Visual Studio Code. Please note in the tabs below the menu bar that the C code has been saved in a file named hello.c. Please note in the bottom pane that Code Runner executed this C code by running the following command:

cd "/home/karel/Desktop/" && gcc tempCodeRunnerFile.c -o tempCodeRunnerFile && "/home/karel/Desktop/"tempCodeRunnerFile
karel
  • 110,292
  • 102
  • 269
  • 299