Home >Backend Development >C++ >Why am I getting an \'undeclared identifier\' error for `HelloWorld()` in my C code?
Why is HelloWorld() Undeclared in the Current Scope?
In the provided C code, the HelloWorld() function is called from within the main() function, but the compiler reports an error that it's not declared in that scope. This error occurs because the function definition must be available before it can be used.
To resolve this issue, you have two options:
Option 1: Declare the Function
You can declare the HelloWorld() function before using it, like this:
#include <iostream> using namespace std; void HelloWorld(); // Declare the HelloWorld function int main() { HelloWorld(); return 0; } void HelloWorld() { cout << "Hello, World" << endl; }
Option 2: Move the Function Definition
Alternatively, you can move the definition of HelloWorld() before the main() function:
#include <iostream> using namespace std; void HelloWorld() { cout << "Hello, World" << endl; } int main() { HelloWorld(); return 0; }
By declaring the function or moving its definition to a scope where it can be accessed by the main() function, you ensure that the compiler knows about its existence and can correctly resolve its call.
The above is the detailed content of Why am I getting an \'undeclared identifier\' error for `HelloWorld()` in my C code?. For more information, please follow other related articles on the PHP Chinese website!