Home > Backend Development > C++ > body text

Why am I getting an \'undeclared identifier\' error for `HelloWorld()` in my C code?

Barbara Streisand
Release: 2024-10-26 18:19:29
Original
618 people have browsed it

Why am I getting an

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;
}
Copy after login

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;
}
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!