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,144 of 243,242   
   Keith Thompson to Bart   
   Re: printf and time_t   
   04 Feb 26 15:39:46   
   
   From: Keith.S.Thompson+u@gmail.com   
      
   Bart  writes:   
   [...]   
   > The problem is that C expects an exact format-code when trying to use   
   > *printf functions, and for that you need to know the exact types of   
   > the expressions being passed. For example:   
   >   
   >   uintptr_t x;                    // from above examples   
   >   double y;                       //   
   >   printf("x * y is %?", x * y);   // What's '?'   
      
   Since you asked...   
      
   '?' is 'f' (or 'g' or 'e', or 'a', or any of those in upper case).   
   `x * y` is of type double.   
      
   When an integer operand is multiplied by (or added to, or ...) a   
   floating-point operand, the integer operand is converted to the type   
   of the floating-point operand.  The "usual arithmetic conversions"   
   are moderately complicated, but that particular rule is simple   
   enough that I didn't have to look it up (though I did double-check   
   it, no pun intended).   
      
   If I didn't know that, I'd look it up.  If I wanted something other   
   than the usual arithmetic conversions, I'd force the result I want   
   using a cast or casts.  And I know it's just an example, but in   
   real life I'd spend some time thinking about *why* I'm multiplying   
   a uintptr_t by a double (I can't think of a realistic scenario where   
   that would be appropriate), and likely concluding that one or both of   
   the operands should have been of some other type in the first place.   
      
   Yes, printf requires an exact type for each argument.  Yes, that   
   can be inconvenient.  But updating printf so it can take arguments   
   of any type and know what to do with them would require changes   
   to the language that nobody, as far as I know, has proposed.   
   (Any such proposal that would work only for the *printf functions,   
   not for arbitrary user-written code, would probably not be accepted.)   
      
   --   
   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