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 242,893 of 243,242   
   Keith Thompson to Scott Lurndal   
   Re: printf and time_t   
   07 Jan 26 13:28:45   
   
   From: Keith.S.Thompson+u@gmail.com   
      
   scott@slp53.sl.home (Scott Lurndal) writes:   
   > Keith Thompson  writes:   
   >>Michael S  writes:   
   >>> On Tue, 6 Jan 2026 10:31:41 -0500   
   >>> James Kuyper  wrote:   
   >>>> On 2026-01-06 04:29, Michael S wrote:   
   >>>> > On Tue, 6 Jan 2026 00:27:04 -0000 (UTC)   
   >>>> > Lawrence D’Oliveiro  wrote:   
   >>>> ...   
   >>>> >> Section 7.8 of the C spec defines macros you can use so you   
   don’t   
   >>>> >> have to hard-code assumptions about the lengths of integers in   
   >>>> >> printf-format strings.   
   >>>> >   
   >>>> > Did you ever try to use them? They look ugly.   
   >>>>   
   >>>> Which is more important, correctness or beauty?   
   >>>   
   >>> It depends.   
   >>>   
   >>> When I know for sure that incorrectness has no consequences, like   
   >>> in case of using %u to print 'unsigned long' on target with 32-bit   
   >>> longs, or like using %llu to print 'unsigned long' on  target with   
   >>> 64-bit longs, then beauty wins. Easily.   
   >>   
   >>Seriously?   
   >>   
   >>An example:   
   >>   
   >>    unsigned long n = 42;   
   >>    printf("%u\n", n);  // incorrect   
   >>    printf("%lu\n", n); // correct   
   >>   
   >>Are you really saying that the second version is so much uglier   
   >>than the first that you'd rather write incorrect code?   
   >   
   > I suspect he may have been referring to code that needs   
   > to build for both 32-bit and 64-bit targets.   One might   
   > typedef 'uint64' to be unsigned long long on both targets   
   > and just use %llu for the format string.  BTDT.   
      
   In the quoted paragraph above, Michael wrote about using %u to print   
   unsigned long, not about using %u to print some type hidden behind   
   a typedef.  If he didn't mean that, he can say so.   
      
   But even if he meant to talk about printing, say, uint64_t values,   
   my point stands.   
      
   I wouldn't define my own "uint64" type.  I'd just use "uint64_t",   
   defined in .  And I'd use one of several *correct* ways   
   to print uint64_t values.   
      
   Michael, if you'd care to clarify, given:   
      
       unsigned long n = 42;   
       printf("%u\n", n);  // incorrect   
       printf("%lu\n", n); // correct   
      
   (and assuming that unsigned int and unsigned long are the same width on   
   the current implementation), do you really prefer the version marked as   
   "incorrect"?   
      
   --   
   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