C程序将不能编译 [英] C program won't compile

查看:181
本文介绍了C程序将不能编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要带前奏编程和我有谁不是非常投入的教授。不幸的是,类全像我这样的新手,所以没有帮助那里。我要创建一个税计算器只能通过输入/输出,赋值语句和算术。我的朋友居然帮我修复code了一下,并把它发送到我面前编译它。我没有改变什么,但它不会为我工作。

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;诠释的main()
{
    常量双TAX_RATE = 0.075;
    加倍ITEM1 = 0,ITEM2 = 0,项目3 = 0;    的printf(请输入第一个项目的价格:\\ n);
    scanf函数(%LF,&安培; ITEM1);    的printf(您输入%.2lf \\ n,ITEM1);
    的printf(请输入第二个项目的价格:\\ n);
    scanf函数(%LF,&安培; ITEM2);    的printf(您输入%.2lf \\ n,ITEM2);
    的printf(进入第三个项目的价格:\\ n);
    scanf函数(%LF,&安培;项目3);    双总= ITEM1 + ITEM2 +项目3;
    避免双重征税=总* TAX_RATE;
    双totalWithTax = +总税收;    的printf(您输入%.2lf \\ n,项目3);
    的printf(你的项目合计为%.2lf \\ n,总数);
    输出(税收是%.2lf \\ n,含税);
    的printf(税收总额为%.2lf \\ n,totalWithTax);    系统(暂停);}

下面是输出

  1 GT; ------启动生成:项目:ASSIGN2,配置:调试的Win32 ------
1> program.c
1> C:\\用户\\克里斯\\文档\\ Visual Studio 2010的\\项目\\ ASSIGN2 \\ ASSIGN2 \\ program.c(10):警告C4996:'scanf函数':此函数或变量可能是不安全的。考虑使用,而不是scanf_s。要禁用德precation,使用_CRT_SECURE_NO_WARNINGS。详细信息请参见在线帮助。
1> C:\\ Program Files文件(x86)的\\微软的Visual Studio 10.0 \\ VC \\ \\包括stdio.h中(304):参见scanf函数'的声明
1> C:\\用户\\克里斯\\文档\\ Visual Studio 2010的\\项目\\ ASSIGN2 \\ ASSIGN2 \\ program.c(14):警告C4996:'scanf函数':此函数或变量可能是不安全的。考虑使用,而不是scanf_s。要禁用德precation,使用_CRT_SECURE_NO_WARNINGS。详细信息请参见在线帮助。
1> C:\\ Program Files文件(x86)的\\微软的Visual Studio 10.0 \\ VC \\ \\包括stdio.h中(304):参见scanf函数'的声明
1> C:\\用户\\克里斯\\文档\\ Visual Studio 2010的\\项目\\ ASSIGN2 \\ ASSIGN2 \\ program.c(18):警告C4996:'scanf函数':此函数或变量可能是不安全的。考虑使用,而不是scanf_s。要禁用德precation,使用_CRT_SECURE_NO_WARNINGS。详细信息请参见在线帮助。
1> C:\\ Program Files文件(x86)的\\微软的Visual Studio 10.0 \\ VC \\ \\包括stdio.h中(304):参见scanf函数'的声明
1> C:\\用户\\克里斯\\文档\\ Visual Studio 2010的\\项目\\ ASSIGN2 \\ ASSIGN2 \\ program.c(20):错误C2143:语法错误:缺少;前型
1> C:\\用户\\克里斯\\文档\\ Visual Studio 2010的\\项目\\ ASSIGN2 \\ ASSIGN2 \\ program.c(21):错误C2143:语法错误:缺少;前型
1> C:\\用户\\克里斯\\文档\\ Visual Studio 2010的\\项目\\ ASSIGN2 \\ ASSIGN2 \\ program.c(22):错误C2143:语法错误:缺少;前型
1> C:\\用户\\克里斯\\文档\\ Visual Studio 2010的\\项目\\ ASSIGN2 \\ ASSIGN2 \\ program.c(25):错误C2065:'总':未声明的标识符
1> C:\\用户\\克里斯\\文档\\ Visual Studio 2010的\\项目\\ ASSIGN2 \\ ASSIGN2 \\ program.c(26):错误C2065:'税':未声明的标识符
1> C:\\用户\\克里斯\\文档\\ Visual Studio 2010的\\项目\\ ASSIGN2 \\ ASSIGN2 \\ program.c(27):错误C2065:totalWithTax:未声明的标识符
==========生成:0成功,1失败,0了最新,0已跳过==========


解决方案

在C比少C99变量必须在函数的顶部(其他非变量初始化语句之前)声明,因此

 常量双TAX_RATE = 0.075;
加倍ITEM1 = 0,ITEM2 = 0,项目3 = 0;
双总,税务,totalWithTax;

然后就可以使用它们:

  =总ITEM1 + ITEM2 +项目3;
税=总* TAX_RATE;
totalWithTax = +总税收;

在C ++和C99中你可以声明code里面的变量。

的Visual C ++不完全支持C99(仅适用于一些作品,例如读取 http://stackoverflow.com/a/146419 / 613130​​

的Visual C ++选择正在使用基于文件扩展名的语言。您的朋友可能命名为喜欢的 myprogram.cpp 的程序,并编写了他的计算机作为C ++的,而你命名的文件 myprogram.c ,并在您的计算机为C编译上

有关的警告......你可以忽略它们。微软几年前就开始对(即处理字符串和内存块的那些)C的基本库部分地区讨伐,声称他们不安全,因为它很容易滥用它们。据介绍,有一些更多的参数的新功能。这些功能是不是标准的和微软具体。见<一href=\"http://msdn.microsoft.com/en-us/library/8ef0s5kh.aspx\">http://msdn.microsoft.com/en-us/library/8ef0s5kh.aspx

您可以使用

 的#define _CRT_SECURE_NO_WARNINGS

在文件的开头(前的#include )以删除警告。它不会有副作用,如果你在编译GCC。

I'm taking intro to programming and i have a professor who isn't very involved. Unfortunately the class is full of novices like myself, so no help there. I have to create a tax calculator only using input/output, assignment statements, and arithmetic. My friend actually helped me fix the code up a bit and compiled it before sending it to me. I didn't change anything, yet it won't work for me.

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

int main()
{
    const double TAX_RATE = 0.075;
    double item1 = 0, item2 = 0, item3 = 0;

    printf("Enter the price of the first item: \n");
    scanf("%lf", &item1);

    printf("You entered %.2lf \n", item1);
    printf("Enter the price of the second item: \n");
    scanf("%lf", &item2);

    printf("You entered %.2lf \n", item2);
    printf("Enter the price of the third item: \n");
    scanf("%lf", &item3);

    double total = item1 + item2 + item3;
    double tax = total * TAX_RATE;
    double totalWithTax = total + tax;

    printf("You entered %.2lf \n", item3);
    printf("The total of your items is %.2lf \n", total);
    printf("The tax is %.2lf \n", tax);
    printf("The total with tax is %.2lf \n", totalWithTax);

    system("pause");

}

Here's the output

1>------ Build started: Project: assign2, Configuration: Debug Win32 ------
1>  program.c
1>c:\users\chris\documents\visual studio 2010\projects\assign2\assign2\program.c(10): 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 10.0\vc\include\stdio.h(304) : see declaration of 'scanf'
1>c:\users\chris\documents\visual studio 2010\projects\assign2\assign2\program.c(14): 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 10.0\vc\include\stdio.h(304) : see declaration of 'scanf'
1>c:\users\chris\documents\visual studio 2010\projects\assign2\assign2\program.c(18): 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 10.0\vc\include\stdio.h(304) : see declaration of 'scanf'
1>c:\users\chris\documents\visual studio 2010\projects\assign2\assign2\program.c(20): error C2143: syntax error : missing ';' before 'type'
1>c:\users\chris\documents\visual studio 2010\projects\assign2\assign2\program.c(21): error C2143: syntax error : missing ';' before 'type'
1>c:\users\chris\documents\visual studio 2010\projects\assign2\assign2\program.c(22): error C2143: syntax error : missing ';' before 'type'
1>c:\users\chris\documents\visual studio 2010\projects\assign2\assign2\program.c(25): error C2065: 'total' : undeclared identifier
1>c:\users\chris\documents\visual studio 2010\projects\assign2\assign2\program.c(26): error C2065: 'tax' : undeclared identifier
1>c:\users\chris\documents\visual studio 2010\projects\assign2\assign2\program.c(27): error C2065: 'totalWithTax' : undeclared identifier
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

解决方案

In C lesser than C99 variables must be declared at the top of the function (before other non-variable-initializing statements), so

const double TAX_RATE = 0.075;
double item1 = 0, item2 = 0, item3 = 0;
double total, tax, totalWithTax;

and then you can use them:

total = item1 + item2 + item3;
tax = total * TAX_RATE;
totalWithTax = total + tax;

In C++ and in C99 you can declare variables inside the code.

Visual C++ doesn't fully support C99 (only some pieces, read for example http://stackoverflow.com/a/146419/613130)

Visual C++ choose which language you are using based on file extension. Your friend probably named the program like myprogram.cpp and it compiled on his computer as C++, while you named the file myprogram.c and it compiled on your computer as C.

For the warnings... You can ignore them. Microsoft some years ago began a crusade against parts of the base library of C (the ones that handle strings and blocks of memory), declaring them "unsafe" because it's very easy to misuse them. It introduced new functions that have some more parameters. These functions are not-standard and Microsoft specific. See http://msdn.microsoft.com/en-us/library/8ef0s5kh.aspx .

You can use a

#define _CRT_SECURE_NO_WARNINGS

at the beginning of the file (before the #include) to remove the warnings. It won't have side-effects if you compile on GCC.

这篇关于C程序将不能编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆