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,034 of 243,242   
   Keith Thompson to James Kuyper   
   Re: printf and time_t   
   15 Jan 26 04:00:57   
   
   From: Keith.S.Thompson+u@gmail.com   
      
   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.   
      
   Of course I do not advocate doing this other than as a test of an   
   implementation's behavior.   
      
   J.B.S. Haldane famously said that "The Universe is not only queerer   
   than we suppose, but queerer than we can suppose."  The same is   
   true of undefined behavior.   
      
   --   
   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