Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
void main(void)
{
   long lDays;

   lDays = 2011 * 365;
   printf("\nDays=[%u]\n", lDays);
}


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.
Posted
Updated 12-Sep-11 10:05am
v2

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.
 
Share this answer
 
Comments
RDSchaefer 12-Sep-11 18:36pm    
Thanks! I tried %u, %U, and %ul but somehow forgot to check %lu. I'm getting old.
C
#include <stdio.h>
#include <conio.h>
void main(void)
{
   long lDays;

   lDays = 2011L * 365;
   printf("\nDays: %ul\n", lDays);
   getch();
}


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!
 
Share this answer
 
try the following :
C++
lDays = 2011L * 365;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900