编译C源代码时出错

时间:2011-06-29 16:00:08

标签: c compiler-errors

我需要帮助来识别我用C编写的程序中的错误。请记住我还在学习C.我正在努力实现我所学到的东西。我的IDE是MS visual studio 2010。

这是程序,程序描述写成注释:

/*The distance between two cities (in km) is input through the keyboard. 
Write a program to convert and print this distance in meters, feet, inches and centimeters*/

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

//I have used #include<stdio.h> and #include<conio.h> above


int main()
{
float km, m, cm, ft, inch ;

clrscr();
printf("\nEnter the distance in Kilometers:");
scanf("%f", &km );

// conversions

m=km*1000;
cm=m*100;
inch=cm/2.54;
ft=inch/12;

// displaying the results

printf("\nDistance in meters =%f", m);
printf("\nDistance in centimeters =%f", cm);
printf("\nDistance in feet =%f", ft);
printf("\nDistance in inches = %f", inch);

printf("\n\n\n\n\n\n\nPress any key to exit the program.");
getchar();
return 0;
}

Errors:
1>e:\my documents\visual studio 2010\projects\distance.cpp(32): error C2857: '#include' statement specified with the /YcStdAfx.h command-line option was not found in the source file

3 个答案:

答案 0 :(得分:6)

错误C2857:在源代码中找不到使用/YcStdAfx.h命令行选项指定的'#include'语句

这意味着编译器(VisualStudio 2010)强制包含StdAfx.h,但在源代码中您不包含它。

尝试添加:

#include <StdAfx.h>

位于源文件的顶部。

答案 1 :(得分:3)

SanSS已经解释了错误消息。让我简要解释一下这些警告。此时可以忽略有关scanf的第一个警告。 scanf的问题在于,如果您尝试将字符串读入预先分配的C字符串(例如char数组或char指针),则它是不安全的。你正在读取一个浮点数,它总是有一个固定的大小(通常是四个字节)。所以这里不会发生溢出。

第二个警告是关于表达式inch=cm/2.54。文字2.54被视为双精度值。因此cm/2.54也将是一个双重值 - 这样的计算表达式的结果将始终是向上的。虽然cm的类型为float(单精度),但结果为double。但是,inch的类型为float,因此作业=会隐式地将结果从double转发到float。由于float变量的精度较低,因此结果将变得不那么精确。要避免此警告,请更改数字文字,以使表达式如下所示:inch = cm / 2.54f。这告诉编译器将2.54视为单精度float字面值。

答案 2 :(得分:3)

警告C4996
在vs 2010,特别是在vs 2012 你必须将以下代码放在文件的顶部

#define _CRT_SECURE_NO_WARNINGS  

并在项目的属性页面上将预编译的标题选项设置为“不使用”。