有人能告诉我为什么下面的代码在标记的行输出0
?为什么这个整数除法产量为0?
看起来好像一切都是正确的,但是当我试图让结果接近尾声时,它每次都给我0。
#include <stdio.h>
int main() {
// Gather time-lapse variables
int frameRate, totalLengthSecs;
printf("How many frames per second: ");
scanf("%i", &frameRate);
printf("--> %i frames confirmed.", frameRate);
printf("\nDesired length of time-lapse [secs]: ");
scanf("%i", &totalLengthSecs);
printf("--> %i seconds confirmed.", totalLengthSecs);
int totalFrames = frameRate * totalLengthSecs;
printf("\nYou need %i frames.", totalFrames);
// Time-lapse interval calculation
int timeLapseInterval = totalLengthSecs/totalFrames;
printf("\n\n%i", timeLapseInterval); // <-- this prints 0
return 0;
}
你在“stdin”中输入“期望的延时长度”是什么? – 2012-01-11 14:24:16
如果totalLengthSecs/totalFrames小于1,则它被截断为0,因为所有值都是整数,而不是浮点数。 – 2012-01-11 14:26:44