Home > Enterprise >  Error: use of undeclared identifier on an apparently declared variable [duplicate]
Error: use of undeclared identifier on an apparently declared variable [duplicate]

Time:09-21

#include <stdio.h>
#include <cs50.h>

int main(void)
{
    long creditcardno;
    do
    {
        creditcardno = get_long("enter");
    }
    while (creditcardno<1000000000000 || 
           creditcardno > 9999999999999999);

}


int i = 0;
long cc = creditcardno;
while (cc>0);
{
    cc = creditcardno/10;
    i  ;
}

In the above program in C, I intend to have a variable named cc of long type which will initialize with the value of entered creditcardno by the user.

Getting error message:

credit.c:14:11: error: use of undeclared identifier 'creditcardno' By "undeclared identifier," clang means you've used a name creditcardno on line 14 of credit.c which hasn't been defined. If you mean to use creditcardno as a variable, make sure to declare it by specifying its type, and check that the variable name is spelled correctly.

To my understanding, I have defined variable creditcardno as this variable stores input value of credit card from the user.

CodePudding user response:

You write this part of code outside of main(). That's why it shows an error.

int i = 0;
long cc = creditcardno;
while (cc>0);
{
    cc = creditcardno/10;
    i  ;
}

creditcardno is a variable inside the main scoope. That's why it is only accessible from the main scoope.

  •  Tags:  
  • c
  • Related