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,850 of 243,242   
   Keith Thompson to Michael S   
   Re: printf and time_t   
   06 Jan 26 16:29:04   
   
   From: Keith.S.Thompson+u@gmail.com   
      
   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?   
      
   If unsigned int and unsigned long happen to be the same size, both   
   are likely to print "42".  But what if your code is later compiled   
   on a system with 32-bit unsigned int and 64-bit unsigned long?   
      
   Even if I were certain the code would never be ported (and such   
   certainty is often unjustified), I'd much rather use the correct   
   code than waste time figuring out which incorrect code will happen   
   to "work" on the current system, with the current version of the   
   compiler and runtime library.  Oh, and gcc and clang both warn   
   about an incorrect format string.   
      
   I agree that the macros in  are ugly, and I rarely   
   use them.  If I want to print an integer value whose type I don't   
   know, I'll probably cast to a predefined type that I know to be   
   wide enough and use the specifier for that type.  Though now that   
   I think about it, I'm more likely to do that in throwaway code;   
   for production code, I'd be more likely to use the  macros.   
      
   [...]   
      
   --   
   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