Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C TurboC
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);
}
 
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 12-Sep-11 7:38am
Edited 12-Sep-11 11:05am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
Comments
RDSchaefer at 12-Sep-11 18:36pm
   
Thanks! I tried %u, %U, and %ul but somehow forgot to check %lu. I'm getting old.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

#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!
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

try the following :
lDays = 2011L * 365;
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 587
1 Sergey Alexandrovich Kryukov 479
2 Maciej Los 305
3 Mathew Soji 195
4 Afzaal Ahmad Zeeshan 170
0 OriginalGriff 7,356
1 Sergey Alexandrovich Kryukov 6,777
2 DamithSL 5,461
3 Manas Bhardwaj 4,916
4 Maciej Los 4,475


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 12 Sep 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100