以下是我的代码
main.c
#include <stdio.h>
#include <stdbool.h>
#include "func.h"
int main () {
int counts = 10;
printf("Expected: %lF and rcount %lF,%lF\n",
counts * 30* 0.156, rcount(0,30), rcount(30,0));
return 0;
}这是我的简化函数。h
#ifndef FUNC_INCLUDED
#define FUNC_INCLUDED
float rcount(int m, int n);
#endif最后这里是我的func.c
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
#include "func.h"
double rcount(int m, int n) {
double series1 = ((double)m/2)*(10+(double)m*10)/20;
double series2 = ((double)n/2)*(10+(double)n*10)/20;
return (series2 > series1) ? series2-series1 : series1-series2;
}现在,如果我执行,我会得到rcount()的随机值,而如果我从main中删除#include<stdbool.h>,我会得到正确的值。
有什么想法吗?
发布于 2013-05-24 12:37:05
正如@Carl Norum所说,“一定有什么你没告诉我们的”很能说明问题。
rcount()调用是printf()语句返回其双精度类型,但printf()由于原型的原因需要一个float,如果看不到原型,则需要一个int。无论哪种情况,printf()都会显示错误的数据。
尝试3种方法: 1)使用不同于FUNC_INCLUDED其他定义,可能stdbool.h正在使用该宏。2)改变你的原型和实现,返回相同的类型,最好是双精度的。3)直接在main()之前制作rcount()原型的冗余副本。
extern double rcount(int m, int n);使用[缺少] stdbool.h是在转移注意力。有/没有它,你的编译就是使用一些不同的文件,选项等(除非它是FUNC_INCLUDED)
https://stackoverflow.com/questions/14514967
复制相似问题