【发布时间】: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