Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Usually you can use a cast to convert the operand to some type for which you know the right format.</p> <p>Your proposed solution:</p> <pre><code>time_t t = time(NULL); printf("%s", t); </code></pre> <p>clearly will <em>not</em> work, since <code>time_t</code> is a numeric type, not <code>char*</code>.</p> <p>We know, in general, that <code>time_t</code> is an arithmetic type. Something like this:</p> <pre><code> printf("%ld\n", (long)t); </code></pre> <p>is likely to work on most systems. It can fail (a) if <code>time_t</code> is an unsigned type no wider than <code>unsigned long</code> and the current value of <code>t</code> exceeds <code>LONG_MAX</code>, or (b) if <code>time_t</code> is a floating-point type.</p> <p>If you have C99 support, you can use <code>long long</code>, which is a little better:</p> <pre><code>printf("%lld\n", (long long)t); </code></pre> <p>If you really want to go overboard with portability, you can detect what kind of type <code>time_t</code> is:</p> <pre><code>if ((time_t)-1 &gt; 0) { // time_t is an unsigned type printf("%ju\n", (uintmax_t)t); } else if ((time_t)1 / 2 &gt; 0) { // time_t is a signed integer type printf("%jd\n", (intmax_t)t); } else { // time_t is a floating-point type (I've never seen this) printf("%Lg\n", (long double)t); } </code></pre> <p>You might want to tweak the <code>%Lg</code> format to something like <code>%Lf</code> or <code>%.10Lf</code>, depending on what output format you want.</p> <p>Again, this assumes C99 support -- and you'll need <code>#include &lt;stdint.h&gt;</code> to make <code>uintmax_t</code> and <code>intmax_t</code> visible.</p> <p><code>time_t</code> and <code>clock_t</code> are a bit unusual, in that the standard says <em>only</em> that they're arithmetic type capable of representing times. (In principle they could be complex types, but I'd say ignoring that possibility is worth the risk.)</p> <p>In most other cases, you'll probably know whether a given type is signed, unsigned, or floating-point, and you can just convert to the widest type of that kind.</p> <p>Note that if you don't know how <code>time_t</code> is represented, you probably won't understand the output of the <code>printf</code> (such as <code>1379375215</code>) either -- unless your goal is to figure that out.</p> <p>(If you were programming in C++ rather than C, <code>std::cout &lt;&lt; t &lt;&lt; "\n";</code> would automatically use the correct overloaded <code>operator&lt;&lt;</code>.)</p> <p>If you want <em>human-readable</em> output (like <code>Mon 2013-09-16 16:46:55 PDT</code>), you'll want to use one of the conversion functions declared in <code>&lt;time.h&gt;</code>, such as <code>asctime()</code> or <code>strftime()</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload