home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c      Meh, in C you gotta define EVERYTHING      243,242 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 243,032 of 243,242   
   James Kuyper to Keith Thompson   
   Re: printf and time_t   
   15 Jan 26 20:08:06   
   
   From: jameskuyper@alumni.caltech.edu   
      
   On 2026-01-15 07:00, Keith Thompson wrote:   
   > James Kuyper  writes:   
   > [...]   
   >> I'm not sure exactly what you intended. And, as I mentioned in another   
   >> sub-thread, I've worked for most of my career under rules that   
   >> prohibited me from writing code that depends upon the kinds of details   
   >> that you're talking about - as a result, I've had little reason to   
   >> familiarize myself with those details. However, I can say that using   
   >> "%u" to print a value of unsigned long type has no chance of working   
   >> unless unsigned int and unsigned long have the same size and   
   >> representation. Even if they do, the behavior is still undefined, but   
   >> there's a pretty good chance it will work.   
   > [...]   
   >   
   > On one implementation (gcc, glibc, 64 bits), it *can* "work":   
   >   
   > ```   
   > #include    
   > int main(void) {   
   > unsigned long x = 123456789;   
   > printf("sizeof (unsigned) = %zu\n", sizeof (unsigned));   
   > printf("sizeof (unsigned long) = %zu\n", sizeof (unsigned long));   
   > printf("x = %u\n", x);   
   > }   
   > ```   
   >   
   > The output on my system (after some compiler warnings):   
   >   
   > ```   
   > sizeof (unsigned) = 4   
   > sizeof (unsigned long) = 8   
   > x = 123456789   
   > ```   
   >   
   > Apparently printf tries to grab a 32-bit value and happens to get   
   > the low-order 32 bits of the 64-bit value that was passed. A value   
   > exceeding LONG_MAX is not printed correctly, but in principle it   
   > could be.   
      
   I knew about that possibility, and had intended to word my comment to   
   cover it, but I forgot. Thanks for covering it. The key point is that   
   this only works for a large but limited range of values - it cannot work   
   in general.   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca