From: bc@freeuk.com   
      
   On 05/01/2026 12:45, Kenny McCormack wrote:   
   > In article <20260105105138.00005f0a@yahoo.com>,   
   > Michael S wrote:   
   > ...   
   >> I can't think about situation in which casting time_t value to 'long   
   >> long' can go wrong.   
   >   
   > These are all good suggestions, but in the end, are all just kludgey   
   > workarounds. My two points in posting are:   
   >   
   > 1) There really should be a generic way to print any numeric object and   
   > have the compiler "Do The Right Thing".   
      
   My original C compiler had a way to do it:   
      
    #include    
    #include    
      
    int main(void) {   
    printf("%v\n", clock());   
    }   
      
   The special format "%v" (obviously only possible within a string   
   literal) is translated by the compiler into a suitable default for the   
   type of the corresponding expression.   
      
   In this case (64-bit Windows), it would be "%lld". As a bonus, "%=v"   
   shows the expression itself as a label; output is:   
      
    CLOCK()=0   
      
      
   > 2) Although time_t was the specific occasion for composing and posting   
   > this, it, of course, applies to all the other "artificial" numeric   
   > types. As mentioned in the OP, they seem to have come up with a   
   > solution for size_t; it would be nice if that were generalized.   
      
   The issue is even bigger than that:   
      
   * You need to /know/ the exact type of your expression   
      
   * Even if you do, it might comprise mixed types and you need to figure   
   out the overall type of the result   
      
   * It may use typedefs that you have to hunt down   
      
   * Or they may be opaque types exported from some library   
      
   * You may need th exact type and format. But you then you change the   
   types of some variables. Now all printf calls that might use those   
   variables in expressions need to be checked and possibly updated.   
      
   * Or you might just want to rearrange your print items.   
      
   * The type might be in64_t, say, but even if you know this, the format   
   will depend on whether it is implemented on top of 'long', or 'long long'.   
      
   For some of this, a compiler like gcc /might/ report a mismatch. It can   
   do that because it knows the types of the print items. But if it can do   
   that, it can also insert the correct format.   
      
   With my feature, you just do this:   
      
    printf("%v %v %v %v", a, b, c, d);   
      
   Who cares what the types are!   
      
      
      
   > P.S. I like the rhythm of "long long can't go wrong"...   
      
   Unless the type is floating about with a value between 0 and 1.   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|