They most often come from forgetting to include the header file that contains the function declaration, for example, this program will give an 'undeclared identifier' error:

Missing header

int main() {
    std::cout << "Hello world!" << std::endl;
    return 0;
}

To fix it, we must include the header:

#include <iostream>
int main() {
    std::cout << "Hello world!" << std::endl;
    return 0;
}

If you wrote the header and included it correctly, the header may contain the wrong include guard.

To read more, see http://msdn.microsoft.com/en-us/library/aa229215(v=vs.60).aspx.

Misspelled variable

Another common source of beginner's error occur when you misspelled a variable:

int main() {
    int aComplicatedName;
    AComplicatedName = 1;  /* mind the uppercase A */
    return 0;
}

Incorrect scope

For example, this code would give an error, because you need to use std::string:

#include <string>

int main() {
    std::string s1 = "Hello"; // Correct.
    string s2 = "world"; // WRONG - would give error.
}

Use before declaration

void f() { g(); }
void g() { }

g has not been declared before its first use. To fix it, either move the definition of g before f:

void g() { }
void f() { g(); }

Or add a declaration of g before f:

void g(); // declaration
void f() { g(); }
void g() { } // definition

stdafx.h not on top (VS-specific)

This is Visual Studio-specific. In VS, you need to add #include "stdafx.h" before any code. Code before it is ignored by the compiler, so if you have this:

#include <iostream>
#include "stdafx.h"

The #include <iostream> would be ignored. You need to move it below:

#include "stdafx.h"
#include <iostream>

Feel free to edit this answer.

🌐
LabEx
labex.io › tutorials › c-how-to-fix-undeclared-identifier-in-c-419180
How to fix undeclared identifier in C | LabEx
An identifier is "undeclared" when you try to use it without first telling the compiler what it is. The compiler needs to know what type of data a variable holds or what parameters a function takes before you can use it.
🌐
Reddit
reddit.com › r/cs50 › "use of undeclared identifier" error for loops (in c)
r/cs50 on Reddit: "Use of undeclared identifier" error FOR loops (in C)
November 18, 2020 -

Hi CS50 gang!

I'm really enjoying the course, but my brain hurts already at week 2. I've noticed an error when compiling a programme in C when I try to declare a variable inside a FOR loop in C (example below).

I just move my variable declaration to above the FOR loop, but I have two questions about this:

  1. Why does this happen? Is it because the loop tries to declare the variable each time it iterates?

  2. Does it cause problems to declare the variable (int letter_count) above the loop, like I have done in the example below? Or is there a better convention for declaring variables?

 int letter_count = 0;
    //loop to break text string into chars
    for (int i = 0, n = strlen(text); i < n; i++)
    {
        //check if char i is lower or upper and increment letter-count by 1 if true
        if (isupper(text[i]) || islower(text[i]))
        {
            letter_count++;
        }
        
        printf("%c", text[i]);
    }

Thank you!

🌐
Coderanch
coderanch.com › t › 737485 › languages › undeclared-identifier-declared
Use of undeclared identifier that is declared (C / C++ forum at Coderanch)
December 15, 2020 - Hi Pat Looking at your parse.h file, parseName() is declared as a method of the Parse class. So I would expect to see it being used in something like: where obj is a variable of class Parse. You are calling it as if it's a 'normal' function, not a method, and there isn't one called parseName. ... Thank you very much John, that was my problem. I had to take it outside of the class.
Top answer
1 of 13
109

They most often come from forgetting to include the header file that contains the function declaration, for example, this program will give an 'undeclared identifier' error:

Missing header

int main() {
    std::cout << "Hello world!" << std::endl;
    return 0;
}

To fix it, we must include the header:

#include <iostream>
int main() {
    std::cout << "Hello world!" << std::endl;
    return 0;
}

If you wrote the header and included it correctly, the header may contain the wrong include guard.

To read more, see http://msdn.microsoft.com/en-us/library/aa229215(v=vs.60).aspx.

Misspelled variable

Another common source of beginner's error occur when you misspelled a variable:

int main() {
    int aComplicatedName;
    AComplicatedName = 1;  /* mind the uppercase A */
    return 0;
}

Incorrect scope

For example, this code would give an error, because you need to use std::string:

#include <string>

int main() {
    std::string s1 = "Hello"; // Correct.
    string s2 = "world"; // WRONG - would give error.
}

Use before declaration

void f() { g(); }
void g() { }

g has not been declared before its first use. To fix it, either move the definition of g before f:

void g() { }
void f() { g(); }

Or add a declaration of g before f:

void g(); // declaration
void f() { g(); }
void g() { } // definition

stdafx.h not on top (VS-specific)

This is Visual Studio-specific. In VS, you need to add #include "stdafx.h" before any code. Code before it is ignored by the compiler, so if you have this:

#include <iostream>
#include "stdafx.h"

The #include <iostream> would be ignored. You need to move it below:

#include "stdafx.h"
#include <iostream>

Feel free to edit this answer.

2 of 13
21

Consider a similar situation in conversation. Imagine your friend says to you, "Bob is coming over for dinner," and you have no idea who Bob is. You're going to be confused, right? Your friend should have said, "I have a work colleague called Bob. Bob is coming over for dinner." Now Bob has been declared and you know who your friend is talking about.

The compiler emits an 'undeclared identifier' error when you have attempted to use some identifier (what would be the name of a function, variable, class, etc.) and the compiler has not seen a declaration for it. That is, the compiler has no idea what you are referring to because it hasn't seen it before.

If you get such an error in C or C++, it means that you haven't told the compiler about the thing you are trying to use. Declarations are often found in header files, so it likely means that you haven't included the appropriate header. Of course, it may be that you just haven't remembered to declare the entity at all.

Some compilers give more specific errors depending on the context. For example, attempting to compile X x; where the type X has not been declared with clang will tell you "unknown type name X". This is much more useful because you know it's trying to interpret X as a type. However, if you have int x = y;, where y is not yet declared, it will tell you "use of undeclared identifier y" because there is some ambiguity about what exactly y might represent.

🌐
Reddit
reddit.com › r/learnprogramming › [c] getting a "error: use of undeclared identifier c" when calling function even though it's declared.
r/learnprogramming on Reddit: [C] Getting a "error: use of undeclared identifier c" when calling function even though it's declared.
March 31, 2017 -

Hello, I'm new to programming and while coding my example program I ran into an issue that I've been trying to figure out how to resolve. When print_Menu_Info(); executes and it tries to execute print_Customer_Table(&c); I encounter the error in the title.

sales.c:101:24: error: use of undeclared identifier 'c'

Even though the line is being declared in line 12.

struct customer c = empty_customer;

#include <stdio.h>
#include <stdlib.h>
#include "sales.h"

int main(void)
{
    menu_update();
    return 0;
}

void menu_update(void){
    struct customer c = empty_customer;
	float f = 0.00;
	int i = 0;
	while (1) {
	    cls();
	    print_Menu_Info();
	    switch(get_User_Input()){
	        case 1:
				printf("Customer First Name: ");
				scanf("%s",c.first_Name);
	            break;
	        case 2:
				printf("Customer Last Name: ");
				scanf("%s",c.last_Name);
	            break;
	        case 3:
				printf("Customer Amount: ");
				scanf("%f",&f);
				c.amount = f;
	            break;
	        case 4:
				printf("Customer Card Number: ");
				scanf("%s",c.card_Number);
	            break;        
	        case 5:
				printf("Customer Card Expiration Month: ");
				scanf("%i",&i);
				c.card_Expiration_Month = i;
	            break;
	        case 6:
				printf("Customer Card Expiration Year: ");
				scanf("%i",&i);
				c.card_Expiration_Year = i;
	            break;
	        case 7:
				printf("Sales Person: ");
				scanf("%s",c.sales_Person);
	            break;
            case 8:
                print_Customer_Table(&c);
                break;
            case 9:
                c = empty_customer;
                break;
	        case 10:
	            exit(0);
	        default:
	            printf("Incorrect action ID!\n");
	            break;
	    }    
	}
}

void cls(void){
    printf("\033c");
}

int get_User_Input(){
	int a = 0;
    char line[32];
    printf("Please input an action ID: ");
	if (fgets(line, sizeof(line), stdin) != NULL) {
    	sscanf(line, "%i", &a);
    	cls();
    }
	return a;
}


void print_Customer_Table(struct customer *cp){
    printf(
    "1. Customer First Name: %s\n"
    "2. Customer Last Name: %s\n"
    "3. Customer Amount: $%.2f\n"
    "4. Customer Card Number: %s\n"
    "5. Customer Card Expiration Month: %i\n"
    "6. Customer Card Expiration Year: %i\n"
    "7. Sales Person: %s\n",
    cp->first_Name,
    cp->last_Name,
    cp->amount,
    cp->card_Number,
    cp->card_Expiration_Month,
    cp->card_Expiration_Year,
    cp->sales_Person);
}

void print_Menu_Info(void){
	print_Customer_Table(&c);
	printf(
	"If you would like to update a value type the value you would like to update(1-7)\n"
    "If you would like to send this as a email type 8\n"
    "If you would like to clear all customer data type 9\n"
    "If you would like to exit this program type 10\n");
}                

and sales.h

#define NAME_LEN_MAX 128
#define CARD_NUM_LEN_MAX 16
struct customer {
    char first_Name[NAME_LEN_MAX + 1];
    char last_Name[NAME_LEN_MAX + 1];
    float amount;
    char card_Number[CARD_NUM_LEN_MAX + 1];
    int card_Expiration_Month;
    int card_Expiration_Year;
    char sales_Person[NAME_LEN_MAX + 1];
};
const static struct customer empty_customer = {{""},{""},0.00,{""},0,0,{""}};
void print_Customer_Table(struct customer *cp);
void print_Menu_Info(void);
void cls(void);
int get_User_Input();
void menu_update(void);
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › error-messages › compiler-errors-1 › compiler-error-c2065
Compiler Error C2065 | Microsoft Learn
August 4, 2025 - The most common causes of C2065 are that the identifier hasn't been declared, the identifier is misspelled, the header where the identifier is declared isn't included in the file, or the identifier is missing a scope qualifier, for example, ...
Find elsewhere
🌐
Reddit
reddit.com › r/cpp_questions › question regarding, an undeclared identifier...but i believe it is declared?
r/cpp_questions on Reddit: Question regarding, an Undeclared Identifier...but I believe it is declared?
November 3, 2019 -

I'm having difficulty figuring out why it won't recognize this function passing parameters, in the int main function. I'm trying to pass the computeoffSet function, and it doesn't like the parameters passed within it. Calls them an undeclared identifier, and I believe I have them declared above. I would appreciate some advice on the problem, for which I'd be grateful.

#include <iostream>
#include <iomanip>
using namespace std;

int getMonth()
{
int usermonth;
cout << "Enter a month number: ";
cin >> usermonth;
return usermonth;
}
int getYear()
{
int useryears;
cout << "Enter year: ";
cin >> useryears;
return useryears;
}

int leapCheck(int month, int years)
{
if(month == 2)
{
if((years % 400 == 0) || (years % 4== 0 && years % 100 != 0))
return 29;
else
return 28;
}
}
int calcDays(int month, int years)
{
int userday = 0;
{
if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
return userday + 31;
else
return userday + 30;
}
return userday;
}

int computeoffSet(int month, int years)
{
int offset = 0;
for(int y = 0; y < 1753; y++)
offset = (offset + 365 + calcDays(month, years)) % 7;
for (int m = 1; m < month; m++)
offset = (offset + calcDays(month, years)) % 7;
return offset;
}
void display(int month, int years, int offset)
{
int numDays = calcDays(month, years);
cout << " Su Mo Tu We Th Fr Sa";
int position = 6;
while(position != offset)
{
cout << " ";
position += 1;
if(position > 6)
position = position - 7;
}
for(int count = 1; count <= numDays; count++)
{
cout << setw(4) << count;
position += 1;

if(position == 7)
position = position - 7;
if(position == 6)
cout << endl;
}
if(position < 6)
cout << endl;
}

int main()
{
int userMonth = getMonth();
int offset = computeoffSet(usermonth, useryears);
int userYears = getYear();
display(userMonth, offset, userYears);
return 0;
}

Top answer
1 of 2
3

Undeclared identifier but is declared

No, it isn't. Your identifier is defined, not declared. This is a common source of confusion. Declaration of an identifier means to give it a unique signature that can be referred to in the following code lines. Definition means to give it a certain value/implementation. A definition implies declaration for the following code.

At least an identifiers complete declaration must have been seen before first usage.

That said you can simply put a declaration for your function before main()

HIDE int call1(int a,int b);

or just change your code to

HIDE int call1(int a,int b) {
    int r;
    r= a+b;
    return r;
}

HIDE int main() {
    int x = 10;
    int z = 5;
    int c;

    c=call1(x,z);
}

and put the definition (which actually implies the declaration) before main() to have a forward declaration for your function.

I'm going to cite the current standards section 3.1 here

3.1 Declarations and definitions [basic.def]

1 A declaration (Clause 7) may introduce one or more names into a translation unit or redeclare names introduced by previous declarations. If so, the declaration specifies the interpretation and attributes of these names. A declaration may also have effects including:
— a static assertion (Clause 7),
— controlling template instantiation (14.7.2),
— use of attributes (Clause 7), and
— nothing (in the case of an empty-declaration).

...

2 of 2
0

or try this

HIDE int call1(int a,int b);  // pre define 

HIDE int main(){
    int x = 10;
    int z = 5;
    int c;

    c = call1(x,z);
}

HIDE int call1(int a,int b)
{
    int r;
    r = a+b;
    return r;
}
🌐
GeeksforGeeks
geeksforgeeks.org › c language › how-to-avoid-compile-error-while-defining-variables
How to avoid Compile Error while defining Variables - GeeksforGeeks
April 5, 2019 - We can declare variables in common ... The identifier is undeclared: In any programming language, all variables have to be declared before they are used....
🌐
Reddit
reddit.com › r/learnprogramming › [c] undeclared identifier that has already been declared?
r/learnprogramming on Reddit: [C] Undeclared Identifier that has already been declared?
September 26, 2013 -

Ive done some research and everyone that seemed to have this problem already didn't declare the variable outside of the loops or if statements that they were used it. I made sure to do it before and I cant seem to get this darn thing working.

Its mostly practice so if anyone can tell me why Id appreciate it!

I get the error Errors

error C2143: syntax error : missing ';' before 'type' Line 0

and

error C2065: 'CountTotal' : undeclared identifier (This one comes up 4 times) Line 10

Now this is driving me crazy. It doesn't work in my compiler, but it works on the website compiler. Ive been trying to fix this for two days. Any ideas? I'm using Microsoft visual express c++ 2011

http://ideone.com/5ifRPT

🌐
Microsoft Learn
learn.microsoft.com › en-us › answers › questions › 810027 › i-have-a-c2065-undeclared-identifier-error-althoug
i have a c2065 undeclared identifier error although i declared it - Microsoft Q&A
April 12, 2022 - Perhaps it is complaining about Show and not reg? Go to definition on RegisterForm and make sure it has a Show method that accepts no arguments. Also make sure this is the only error in your code.
Top answer
1 of 2
5

The problem is the semicolon on line 138:

for (int column = 0; column < d - 1; column++);

In C, a semicolon is used to terminate a statement. The body of the for loop (the part in curly braces) is part of the loop statement. If you add a semicolon after the declaration, but before the curly braces, the compiler considers the loop statement to be terminated before the body even starts, producing the for loop has empty body warning.

Now, the loop variable column that you declared in line 138 is only in-scope during the body of the loop. Since you accidentally terminated the loop before giving it a body, column leaves scope immediately after the declaration on line 138. Since you haven't previously declared column in the scope outside of the loop, you get the use of undeclared identifier error.

If you delete the semicolon from the end of line 138 it should fix at least two of those errors; since row is also raising an error, it's likely you have the same problem in an earlier loop declaration. In general, you don't need to use semicolons after for loop declarations or body blocks in C. You only need them in the declaration itself and on the individual statements inside the body. Like this:

for (int i = 0; i < 5; i++)
{
    printf('%i\n', i);
}

Notice there is no semicolon at the end of the loop declaration or after the curly brace that closes the loop body.

2 of 2
1

The above user has identified the syntax errors in your code and also explained the reason.Here is some more info on the error:

Most of the time use of an undeclared identifier error occurs when you try to use a variable without first declaring it. To declare a variable one must write its type followed by its name (also known as identifier). Once you've declared it, only then you are allowed to use it in expressions or assignments.For example:

    int num; //variable declaration
    num=10;

The above code will not give an error but if you skip the declaration part,most probably the compiler will through a use of an undeclared identifier error.

Another way this error pops up is when you try to access a variable outside the block(a piece of code written between curly braces { } ) where it was originally defined.This is the case when you define a variable in a function body or a loop body and try to access it outside the loop or function.

Here, the compiler will throw an error because for it these variables cease to exist once the block has executed.Such variables are called local variables.The code outside their respective block is said to be outside of their scope. You can read more about this when you study C-Scope rules.Here's a useful link on Scope rules in C: http://www.geeksforgeeks.org/scope-rules-in-c/

🌐
Reddit
reddit.com › r/learnprogramming › undeclared identifier is in fact, declared?
r/learnprogramming on Reddit: Undeclared identifier is in fact, declared?
May 4, 2023 -

Hi,

So I'm very new to programming and am currently undertaking the Harvard CS50 course. Attempting lab 2, I have come across the following error:

scrabble.c:17:34: error: use of undeclared identifier 'word'

for (int i = 0, len = strlen(word); i < len; i++)

This error is confusing the heck out of me, as there are lines of code beneath this one that are unaffected.

The top of my code (prior to line 17) introduces the identifier as int compute_score(string word);. But for some reason, the "word" on line 17 is unaffected by the declaration, while lines of code beneath it that also include "word" are colored orange correspondingly. The "word" on line 17 is for some reason, uncolored.

Sorry if this makes little sense, half asleep trying to do this and am beating myself up a bit.

Thanks.