This error occurs when a statement that is not a declaration is placed outside of a function body. In C or C++ programming languages, every statement must be contained within a function.

For example, the following code will result in a syntax error:

#include <stdio.h>

int main() {
   printf('Hello World!\n');
}

int x = 5; // non-declaration statement outside function body

The declaration of variable 'x' should be inside a function body or wrapped in a declaration block:

#include <stdio.h>

int main() {
   printf('Hello World!\n');
   int x = 5; // declaration inside function body
   return 0;
}

// or

#include <stdio.h>

int x = 5; // declaration block outside of function body

int main() {
   printf('Hello World!\n');
   return 0;
}

Avoid placing statements outside of function bodies in C or C++ programs.

C/C++ Syntax Error: Non-Declaration Statement Outside Function Body

原文地址: https://www.cveoy.top/t/topic/lKJK 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录