【问题标题】:undeclared identifier in CC中未声明的标识符
【发布时间】:2013-06-16 14:59:13
【问题描述】:

我正在尝试在 Visual Studio 2012 Express 中用 C 语言编译一个小型银行程序。它向我显示了几乎所有变量的这个错误“未声明的标识符”,而这个也显示了“语法错误:缺少';'在'type'之前"。请告诉我正确的语法。谢谢。

#include<stdio.h>
#include<conio.h>
int main()
{
printf("Welcome to skybank\n");
int deposit,withdraw,kbalance;
char option;
printf("Press 1 to deposit cash\n");
printf("Press 2 to Withdraw Cash\n");
printf("Press 3 to Know Your Balance\n");
scanf_s("%c",option);
int decash,wicash;
switch(option)
{
int balance;
printf("Enter your current Balance\n");
scanf_s("%d",&balance);
case 1:
    printf("Enter the amount you want to deposit\n");
    scanf_s("%d",&decash);
    printf("Thank You\n");
    printf("%d have been deposited in your account\n",decash);
    break;
case 2:
    printf("Enter the amount you want to withdraw\n");
    scanf_s("%d",&wicash);
    int wibal;
    wibal=balance-wicash;
    printf("Thank You\n");
    printf("%d have been withdrawed from your account\n",wicash);
    printf("Your balance is %d\n",wibal);
    break;
case 3:
    printf("Your balance is Rs.%d\n",balance);
    break;
default:
    printf("Invalid Input\n");
    break;
}
getchar();
}

【问题讨论】:

  • 在C语言中,你应该在父块的开头声明变量
  • 我仍然无法运行它。谢谢您的帮助
  • @gkovacs90:实际上,在当前的 C(意味着 C11,甚至 C99)中,您可以在代码块中的任何位置声明变量。只有在 20 年前的 C89 中,作为 C 编译器运行的 MSVC 所支持的所有东西才存在问题。您对此代码问题的“未声明变量”部分的诊断是正确的,因为 MSVC 编译器需要在块开头声明的变量。您的诊断是不正确的,因为现代 C 没有施加此限制;只有 MSVC 编译器仍然这样做。代码还有其他问题。
  • 感谢大家的宝贵时间和帮助!

标签: c visual-studio-2012


【解决方案1】:

Microsoft C 编译器仅支持该语言的 25 年旧版本。限制之一是所有变量必须在任何其他语句之前声明。所以把你所有的变量声明移到函数的顶部。

我可以看到的下一个错误是将scanf_s%c 格式字符串一起使用。您必须传递一个指向变量的指针,并传递要读取的字符数。

scanf_s("%c", &option, 1);

同样你需要传递一个地址来读取balance

您还需要更改 switch 语句,使其仅包含案例。将裸指令移到外面。

您对option 的阅读将不起作用。因为当您检查1 时,您正在检查具有ASCII 代码1 的字符。将option 更改为int 并使用%d 读取。

也许你正在寻找这样的东西:

#include<stdio.h>
#include<conio.h>

int main(void)
{
    int deposit,withdraw,kbalance;
    int option;
    int decash,wicash;
    int balance;
    int wibal;

    printf("Welcome to skybank\n");
    printf("Press 1 to deposit cash\n");
    printf("Press 2 to Withdraw Cash\n");
    printf("Press 3 to Know Your Balance\n");
    scanf_s("%d", &option);
    printf("Enter your current Balance\n");
    scanf_s("%d", &balance);
    switch(option)
    {
        case 1:
            printf("Enter the amount you want to deposit\n");
            scanf_s("%d", &decash);
            printf("Thank You\n");
            printf("%d have been deposited in your account\n", decash);
            break;
        case 2:
            printf("Enter the amount you want to withdraw\n");
            scanf_s("%d", &wicash);
            wibal=balance-wicash;
            printf("Thank You\n");
            printf("%d have been withdrawed from your account\n", wicash);
            printf("Your balance is %d\n", wibal);
            break;
        case 3:
            printf("Your balance is Rs.%d\n", balance);
            break;
        default:
            printf("Invalid Input\n");
            break;
    }
    getchar();
}

【讨论】:

  • 它运行但在我执行任何操作后立即给我“无效输入”消息。
  • 不,它没有。它运行良好。确保您粘贴的是最新版本。我必须修复很多错误!
  • 谢谢它的工作。你真是个天才先生!我记得你还帮助我编写了计算器代码。
  • 是的,你可以。我想我回答了最初的问题。我认为您可以自己进行下一步。 FWIW 使用真正的 C 编译器可能会更好。在 Windows 上尝试 mingw。
【解决方案2】:

对于未识别的变量,尝试将所有变量声明放在主块的顶部,例如:

int main()
{
    int deposit, withdraw, kbalance, decash, wicash, wibal;
    char option;
    printf("Welcome to skybank\n");

C 的旧变体不喜欢​​将变量声明与代码混合。据我所知,微软 C 实现的 C 标准是 C99 之前的标准,所以这可能是问题所在。

您应该研究的其他一些问题:

scanf_s("%c",option); - option 应该是 &amp;option,因为您正在获取指向该变量的指针。

也在这里:case 1:

你想要'1'(如case '1')而不是普通的1,因为它是char,而不是你想要的int

其他 case 检查也是如此。

关于scanf_s的问题,尝试编译时有警告,应该已经被编译器指出了。

最后,您可能希望在代码中删除不使用的变量,例如 kbalancewithdrawdeposit

【讨论】:

    【解决方案3】:

    在Visual c的变量声明中块的开头做。

    例如

    int main()
    {
        int deposit,withdraw,kbalance;
        char option;
        int decash,wicash
        int balance;
        int wibal;
    ...
    

    【讨论】:

    • @user2481364 如果它工作正常,你有什么疑问? “告诉我正确的语法。”
    • 它显示相同的错误“未声明的变量”和“缺少';'在“类型”之前。
    • @user2481364 对我来说很好(没有错误)。但是int balance, wibal; 移动到主要的bigining。
    • switch 语句之后的块顶部声明变量不是一个好主意。它在语法上是合法的,但充其量是可疑的做法,如果变量有任何初始化,则不会执行初始化。这在具有复杂用户定义类型的 C++ 中非常重要。这段代码的问题不大。但这种做法不好,不应鼓励。
    • @JonathanLeffler 是的,当然。固定。
    【解决方案4】:

    试试这个代码:

    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
        printf("Welcome to skybank\n");
        int deposit,withdraw,kbalance;
        char option;
        printf("Press 1 to deposit cash\n");
        printf("Press 2 to Withdraw Cash\n");
        printf("Press 3 to Know Your Balance\n");
        scanf("%c",&option);
        int decash,wicash;
        switch(option)
        {
        int balance;
        printf("Enter your current Balance\n");
        scanf("%d",&balance);
        case 1:
            printf("Enter the amount you want to deposit\n");
            scanf("%d",&decash);
            printf("Thank You\n");
            printf("%d have been deposited in your account\n",decash);
            break;
        case 2:
            printf("Enter the amount you want to withdraw\n");
            scanf("%d",&wicash);
            int wibal;
            wibal=balance-wicash;
            printf("Thank You\n");
            printf("%d have been withdrawed from your account\n",wicash);
            printf("Your balance is %d\n",wibal);
            break;
        case 3:
            printf("Your balance is Rs.%d\n",balance);
            break;
        default:
            printf("Invalid Input\n");
            break;
        }
        getchar();
    }
    

    【讨论】:

      【解决方案5】:

      移动这个:

      int balance;
      printf("Enter your current Balance\n");
      scanf_s("%d",&balance);
      

      switch 语句之前。

      【讨论】:

      • 这是问题的一部分;这不是唯一的问题,甚至不是最大的问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多