我试图用函数编写一个小的c程序来执行算术运算,但是遇到了一些错误。
有谁能看一下这段代码并核实一下吗?
错误:
Checking file dependency...
50: error: cannot convert `div_t' to `int' in assignment
64: error: a function-definition is not allowed here before '{' token
69: error: a function-definition is not allowed here before '{' token
74: error: a function-definition is not allowed here before '{' token
79: error: a function-definition is not allowed here before '{' token
84: error: a function-definition is not allowed here before '{' token
90:2: warning: no newline at end of file
Complete Make Untitled25: 6 error(s), 1 warning(s)码
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
int add(int, int);
int sub(int, int);
int mul(int, int);
int divs(int, int);
int print(int);
int main(){
int ans,a,b,n;
printf("******************");
printf("\n1 ADD");
printf("\n2 SUB");
printf("\n3 MUL");
printf("\n4 DIV");
printf("\n5 EXT");
printf("\n******************");
printf("\n\n\nEnter Your Option(1-5):");
scanf("%d",&n);
// n=1;
printf("\nEnter Values a,b:");
scanf("%d%d",&a,&b);
// a=1;b=2;
if(n==4&&b==0){
printf("\n\n Value of `B` Can't Be `0` for Division\n\n");
exit(EXIT_SUCCESS);
}
if(n>0&&n<6){
if(n==1){
ans=add(a,b);
// main();
}
if(n==2){
ans=sub(a,b);
// main();
}
if(n==3){
ans=mul(a,b);
// main();
}
if(n==4){
ans=divs(a,b);
// main();
}
if(n==5){
exit(EXIT_SUCCESS);
}
print(ans);
}
else {
printf("\nWorng Entry!\n ");
exit(EXIT_SUCCESS);
}
int add(int a,int b){
ans=a+b;
return(ans);
}
int sub(int a,int b){
ans=a-b;
return(ans);
}
int mul(int a,int b){
ans=a*b;
return(ans);
}
int divs(int a,int b){
ans=a/b;
return(ans);
}
int print(int ans){
printf("\n\n Answer = %d",ans);
return;
}
}我使用C-免费专业人员编写和编译C程序.我也尝试过在线C程序编译器.但同样的错误。
发布于 2014-05-21 19:53:44
你包括:
#include<stdlib.h>这包含一个函数div(),它对您的div函数有不同的返回类型,因此产生了这个错误。要解决这个问题,您必须重命名您的div函数,或者不使用这个包含文件。
有关在div中构建的更多信息,请参见:div.htm
此外,您还需要将所有的函数移到main之外。现在,你有以下几点:
int main(){
...
int add(int a,int b){
ans=a+b;
return(ans);
}
...
}需要更改为
int add(int a,int b){
return( a+b); //note there is no "ans" now
}
int main(){
...
...
}发布于 2014-05-21 19:54:01
div()是标准库的一部分,并在stdlib.h中定义,您可以在程序中包含它。您必须为您的函数使用不同的名称,例如my_div。
代码中还有更多的错误--最重要的是,标准C没有嵌套函数。
发布于 2014-05-21 19:52:22
div已经作为一个标准的C函数存在,并返回一个不同的类型。请参阅here
您需要为您的div使用不同的名称。
https://stackoverflow.com/questions/23792480
复制相似问题