C 中的布尔函数
Boolean function in C
我想做的是看看一年是否是双六分相的,
但是当我使用布尔函数时,它给了我这个奇怪的信息。
这是我的代码:
#include<stdio.h>
#include<stdbool.h>
main(){
int n1;
printf("what is the year?\n");
scanf("%d",&n1);
if(itIS(n1)){
printf("the year %d is bissextile\n",n1);
}else{
printf("the year %d is not bissextile\n",n1);
}
}
bool itIS(int n1){
bool is = false;
if((n1/400)== 0){
is = true;
}
return is;
}
这就是我所看到的:
exe1.c:144:6: error: conflicting types for ‘itIS’ bool itIS(int n1){
^
exe1.c:134:6: note: previous implicit declaration of ‘itIS’ was here if(itIS(n1)==true){
^
我不明白这是什么问题。虽然如果我在没有布尔函数的情况下这样做,它会完美地工作。
编辑:感谢@Bill Lynch,我已经知道问题出在哪里了。
问题是我需要在主函数之前写布尔函数,这样编译器才能看到函数,基本上就是这样。
您在使用函数之前没有声明它。在 main
之前添加:
bool itIS(int n1);
还有,是int main(void)
,不是main()
我想做的是看看一年是否是双六分相的, 但是当我使用布尔函数时,它给了我这个奇怪的信息。
这是我的代码:
#include<stdio.h>
#include<stdbool.h>
main(){
int n1;
printf("what is the year?\n");
scanf("%d",&n1);
if(itIS(n1)){
printf("the year %d is bissextile\n",n1);
}else{
printf("the year %d is not bissextile\n",n1);
}
}
bool itIS(int n1){
bool is = false;
if((n1/400)== 0){
is = true;
}
return is;
}
这就是我所看到的:
exe1.c:144:6: error: conflicting types for ‘itIS’ bool itIS(int n1){
^
exe1.c:134:6: note: previous implicit declaration of ‘itIS’ was here if(itIS(n1)==true){
^
我不明白这是什么问题。虽然如果我在没有布尔函数的情况下这样做,它会完美地工作。
编辑:感谢@Bill Lynch,我已经知道问题出在哪里了。 问题是我需要在主函数之前写布尔函数,这样编译器才能看到函数,基本上就是这样。
您在使用函数之前没有声明它。在 main
之前添加:
bool itIS(int n1);
还有,是int main(void)
,不是main()