编译 C 源代码时出错

发布于 2024-11-17 15:18:21 字数 1086 浏览 1 评论 0原文

我需要帮助来识别我用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雪若未夕 2024-11-24 15:18:21

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

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

尝试添加:

#include <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:

#include <StdAfx.h>

at the top of your source file.

泛泛之交 2024-11-24 15:18:21

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. So cm/2.54 will be a double value as well -- the result of such a computational expression will always be upcast. Although cm is of type float (single precision), the result will be a double. However, inch is of type float, so the assignment = will implicitly downcast the result from double to float. Since a float 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 precision float literal.

挽心 2024-11-24 15:18:21

警告C4996
与 2010 年相比,尤其是与 2012 年相比。
您必须将以下代码放在文件顶部,

#define _CRT_SECURE_NO_WARNINGS  

并在项目的属性页中将预编译头选项设置为“不使用”。

to warning C4996
at vs 2010, especially at vs 2012.
you have to put the the following code at the top of the file

#define _CRT_SECURE_NO_WARNINGS  

And set the precompiled header option to "not using" at the project's property page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文