Remi Guan
Remi Guan

Reputation: 22292

What does "declaration is incompatible" error mean in VS Code with C_lang linting?

I'm writing a simple code in C language, and this works.
Which compiles and excutes with no errors, gives the expected output.

#include <stdio.h>

int main(void) {
  struct SiteTemplate {
    int views;
  };
  
  int visit(struct SiteTemplate *site) {
    site -> views++;
    return 0;
  }

  struct SiteTemplate site;
  site.views = 0;

  visit(&site);
  printf("%d\n", site.views);

  return 0;
}

But in my VS Code, with C_Cpp linting is on, my IDE shows the following error and other problems with it.

declaration is incompatible with previous "visit" (declared at line 8)

Having a screenshot of it:

VS Code Screenshot

This error linting is really confusing me since my code works with gcc, it doesn't show any error when compiling.

And also, if I move my struct and function definition to the global level instead of inside main(), then the errors don't exist anymore... But what's the error declaration is incompatible? Or is there any problem with my code?

Click here to view the another screenshot to save whitespaces of this page.

By the way, the version of my VS Code is 1.52.0, with default C_Cpp linting.

Upvotes: 0

Views: 1785

Answers (1)

Sourav Ghosh
Sourav Ghosh

Reputation: 134376

Nested function definition is not standard C, it's supported by compiler extensions. According to C standard, any function definition needs to appear outside of any other function definition.

Upvotes: 1

Related Questions