奇怪的TurboC数学错误? [英] Strange TurboC math error?
问题描述
对不起,如果我忽略了明显的内容,但我不知道为什么这给了我错误的答案.我在XP SP2下的FAT16分区上的DosBox中使用TurboC 3.0.代码如下:
I''m sorry if I''ve overlooked something obvious but I just can''t figure out why this is giving me the wrong answer. I''m using TurboC 3.0 in DosBox on a FAT16 partition under XP SP2. Here''s the code:
void main(void)
{
long lDays;
lDays = 2011 * 365;
printf("\nDays=[%u]\n", lDays);
}
根据我的计算器(并在Windows下在Borland C ++ 5.5.1中运行相同的代码),答案为734015,但以上代码显示13119!
这是我正在调试的大型项目的一小部分,以防您想知道为什么.
According to my calculator (and running the same code in Borland C++ 5.5.1 under Windows) the answer is 734015 but the above code displays 13119!
This is a very small portion of a much larger project that I''m debugging in case you''re wondering why.
推荐答案
值的最低16位734,015代表数字13,119,因此您必须具有错误的格式说明符.我不是C专家,但我认为您需要%ul(或%lu!)才能显示无符号的long.
艾伦.
The lowest 16 bits of the value 734,015 represent the number 13,119 so you must have an incorrect format specifier. I''m no C expert but I think you need %ul (or %lu !) to display a unsigned long.
Alan.
#include <stdio.h>
#include <conio.h>
void main(void)
{
long lDays;
lDays = 2011L * 365;
printf("\nDays: %ul\n", lDays);
getch();
}
在2011年或365年之后,都需要L(ong)说明符.还需要%ul printf格式说明符.
天哪!有人买了空气清新剂吗? TC3.0那种老旧的腐烂,腐烂的野兽烂在鼻子上.我希望TASM比TC的星期日好一个月!
You need both the L(ong) specifier after (either of) 2011 or 365. You also need the %ul printf format specifier.
Gawd! Anybody got some airfreshener? The old decrepit, decayed beast that is TC3.0 is rotten and on the nose. I''d prefer TASM for a month of Sundays over TC!
尝试以下方法:
try the following :
lDays = 2011L * 365;
这篇关于奇怪的TurboC数学错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!