Visual Studio 2012“语法错误”

时间:2013-11-04 20:28:22

标签: c visual-studio-2012

当我尝试在Visual Studio 2012中编译项目时出现以下错误:

1>------ Build started: Project: ConsoleApplication1, Configuration: Debug Win32 ------
1>  main.c
1>e:\main.c(28): warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\stdio.h(290) : see declaration of 'scanf'
1>e:\main.c(30): error C2143: syntax error : missing ';' before 'type'
1>e:\main.c(31): error C2143: syntax error : missing ';' before 'type'
1>e:\main.c(33): error C2065: 'answerMin' : undeclared identifier
1>e:\main.c(33): error C2065: 'answerMax' : undeclared identifier
1>e:\main.c(35): error C2065: 'answerMax' : undeclared identifier
1>e:\main.c(36): error C2065: 'answerMin' : undeclared identifier
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

这是main.c中的代码

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

double a ;
double b ;
double ComputeMaximum( double, double ) ;
double ComputeMinimum( double, double ) ;

int main(void)
{

  printf("\nPlease enter two numeric values for comparison\n") ;

  scanf("%d%d", &a, &b );

  double answerMax = ComputeMaximum( a, b ) ;
  double answerMin = ComputeMinimum( a, b ) ;

  printf("Of %d and %d the minimum is %d and the maximum is %d\n", a, b, answerMin, answerMax ) ;

  printf("%d", answerMax ) ;
  printf("%d", answerMin ) ;

  system("pause");
  return 0;

}

以下是ComputeMinimum.c的代码

double ComputeMinimum( double a, double b )
{
  double result = 0 ;
  ( a > b ) ? ( b = result ) : ( a = result ) ;
  return result ;

}

以下是ComputeMaximum.c的代码

double ComputeMaximum(double a, double b)
{
  double result = 0 ;
  ( a > b ) ? ( a = result ) : ( b = result ) ;
  return result ;

}

2 个答案:

答案 0 :(得分:1)

使用C时,必须在任何指令或函数调用之前声明变量。例如:

int main(void)
{
    double answerMax;
    double answerMin;
    .....
    system("pause");
     return 0;
 }

关于弃用功能。您可以在项目属性中的预处理器定义中添加_CRT_SECURE_NO_WARNING。

答案 1 :(得分:0)

scanf("%d%d", &a, &b );

必须是

scanf("%lf %lf", &a, &b);

因为ab是双倍的(值之间有空格)。

同样适用于

printf("Of %d and %d the minimum is %d and the maximum is %d\n", a, b, answerMin, answerMax ) ;

%d更改为%f

另请注意

double ComputeMinimum( double a, double b )
{
  double result = 0 ;
  ( a > b ) ? ( b = result ) : ( a = result ) ;
  return result ;

}

result始终为0,更改为

( a > b ) ? ( result = b ) : ( result = a ) ;

computeMaximum

相同

当然,您需要在main.c中包含包含这些函数的标头(编译器会警告此事实)