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,035 of 243,242   
   Keith Thompson to James Kuyper   
   Re: printf and time_t   
   15 Jan 26 18:17:54   
   
   From: Keith.S.Thompson+u@gmail.com   
      
   James Kuyper  writes:   
   > 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.   
      
   I suppose it depends on just what you mean by "in general".   
      
   A conforming implementation *could* always print the mathematically   
   correct value of a 64-bit unsigned long argument when printf is   
   called with a 32-bit "%u" format.  I could speculate about how   
   this might work, but it doesn't really matter.  Probably few if   
   any implementations behave this way, but the nature of undefined   
   behavior is that there is no behavior that violates the C standard.   
      
   And of course the best advice is "don't do that".   
      
   --   
   Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com   
   void Void(void) { Void(); } /* The recursive call of the void */   
      
   --- 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