繁体   English   中英

编译 C 源代码时出错

[英]Error During compilation of a C source code

我需要帮助来识别我在 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

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

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

尝试添加:

#include <StdAfx.h>

在源文件的顶部。

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

#define _CRT_SECURE_NO_WARNINGS  

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

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

第二个警告是关于表达式inch=cm/2.54 文字 2.54 被视为双精度值。 所以cm/2.54也将是一个双精度值——这种计算表达式的结果总是向上转换。 尽管cmfloat类型(单精度),但结果将是double 但是, inch是 float 类型,因此赋值=将隐式地将结果从double向下转换为float 由于float变量的精度较低,因此结果将变得不那么精确。 为避免此警告,请更改数字文字,使表达式如下所示: inch = cm / 2.54f 这告诉编译器 2.54 将被视为单精度float文字。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM