编译 C 源代码时出错
我需要帮助来识别我用 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
I need help to identify the bug in my program that I have written in C. Please bear in mind that I am still learning C. I am trying to implement what I have learned to far. My IDE is MS visual studio 2010.
Here is the program, the program description is written as a comment:
/*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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
错误 C2857:在源代码中找不到使用 /YcStdAfx.h 命令行选项指定的“#include”语句
这意味着编译器 (VisualStudio 2010) 强制包含 StdAfx.h,但在源代码中您没有包含它。
尝试添加:
在源文件的顶部。
error C2857: '#include' statement specified with the /YcStdAfx.h command-line option was not found in the source
This means the compiler (VisualStudio 2010) is forcing the inclusion of StdAfx.h but in the source you are not including it.
Try adding:
at the top of your source file.
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 将被视为单精度浮点文字。SanSS already explained the error message. Let me shortly explain the warnings. The first warning about scanf can be ignored at this point. The problem with scanf is that it is unsafe, if you try to read in a string into a pre-allocated C-string (e.g. a char array or char pointer). You are reading in a float, which always has a fixed size (usually four bytes). So no overflow can happen here.
The second warning is about the expression
inch=cm/2.54
. The literal 2.54 is treated as a double precision value. Socm/2.54
will be a double value as well -- the result of such a computational expression will always be upcast. Althoughcm
is of typefloat
(single precision), the result will be adouble
. However,inch
is of type float, so the assignment=
will implicitly downcast the result fromdouble
tofloat
. Since afloat
variable has less precision, the result will become less precise. To avoid this warning, change the numerical literal, so that the expression looks like this:inch = cm / 2.54f
. This tells the compiler that 2.54 is to be treated as a single precisionfloat
literal.警告
C4996
与 2010 年相比,尤其是与 2012 年相比。
您必须将以下代码放在文件顶部,
并在项目的属性页中将预编译头选项设置为“不使用”。
to warning
C4996
at vs 2010, especially at vs 2012.
you have to put the the following code at the top of the file
And set the precompiled header option to "not using" at the project's property page.