Home > Enterprise >  C VS Code not recognizing syntax, unable to run code
C VS Code not recognizing syntax, unable to run code

Time:08-09

I am using a specific syntax needed for a course, but when I use this C syntax in VS Code, it doesn't work and raises errors.

Here is an example of the syntax that is not working:

error: expected ';' at end of declaration
        int i {0}; 
             ^
             ;

When I change it to int i = 0; the error disappears.

It specifically doesn't recognize the {} syntax for setting default variable values. I am using a ssh login for this course and the syntax works well in the ssh, but won't work in VS Code.

I attempted to change my VS Code C version to C 17 by doing the top answer in this thread, but it still doesn't recognize the syntax.

Am I using incorrect syntax, or is there a way to fix this?

CodePudding user response:

Adding on to the comment above, this is what solved my issue:

Go to this link: https://code.visualstudio.com/docs/cpp/config-clang-mac

Go to the section Clang on macOS and scroll down to Troubleshooting. Follow the steps in this paragraph:

"If you see build errors mentioning "C 11 extensions", you may not have updated your tasks.json build task to use the clang argument --std=c 17. By default, clang uses the C 98 standard, which doesn't support the initialization used in helloworld.cpp. Make sure to replace the entire contents of your tasks.json file with the code block provided in the Run helloworld.cpp section."

You will need to copy-paste this exact code and replace the code in the tasks.json folder:

{
  // See https://go.microsoft.com/fwlink/?LinkId=733558
  // for the documentation about the tasks.json format
  "version": "2.0.0",
  "tasks": [
    {
      "type": "shell",
      "label": "C/C  : clang   build active file",
      "command": "/usr/bin/clang  ",
      "args": [
        "-std=c  17",
        "-stdlib=libc  ",
        "-g",
        "${file}",
        "-o",
        "${fileDirname}/${fileBasenameNoExtension}"
      ],
      "options": {
        "cwd": "${workspaceFolder}"
      },
      "problemMatcher": ["$gcc"],
      "group": {
        "kind": "build",
        "isDefault": true
      },
      "detail": "Task generated by Debugger."
    }
  ]
}

To find the tasks.json folder, go to your program-file, do Command Shift . to find hidden files > .vscode > 'tasks.json` > replace all the code in the file with the one I provided. It updates the C compiler to use a more updated version of C .

CodePudding user response:

Excuse me, but I think that you had got a little mistake. There are some difference in program language grammar in SSH and c .

In C programming , --- int i = 0; --- is true and ---int i {0}; --- is false.

As you know, SSH(Secure Shell), is a UNIX-based command interface and protocol for securely getting access to a remote computer. SSH uses public key cryptography for both connection and authentication. SSH is the default tool for system administrator to perform various tasks on servers remotely.

sorry as my poor english.

  • Related