<pre><code>#include &lt;stdio.h&gt;
int main() {
    unsigned long long int num = 285212672; //FYI: fits in 29 bits
    int normalInt = 5;
    printf("My number is %d bytes wide and its value is %ul. A normal number is %d.\n", sizeof(num), num, normalInt);
    return 0;
}
</code></pre>

<p>Output:</p>

<pre><code>My number is 8 bytes wide and its value is 285212672l. A normal number is 0.
</code></pre>

<p>I assume this unexpected result is from printing the <code>unsigned long long int</code>. How do you <code>printf()</code> an <code>unsigned long long int</code>?</p>

[{'answer_id': 2850, 'author': 'John Downey', 'author_id': 200, 'author_profile': 'https://Stackoverflow.com/users/200', 'pm_score': 10, 'selected': True, 'text': '<p>Use the ll (el-el) long-long modifier with the u (unsigned) conversion. (Works in windows, GNU).</p>\n\n<pre><code>printf("%llu", 285212672);\n</code></pre>\n'}, {'answer_id': 2851, 'author': 'sparkes', 'author_id': 269, 'author_profile': 'https://Stackoverflow.com/users/269', 'pm_score': 1, 'selected': False, 'text': "<p>Non-standard things are always strange :)</p>\n\n<p>for the long long portion\nunder GNU it's <code>L</code>, <code>ll</code> or <code>q</code></p>\n\n<p>and under windows I believe it's <code>ll</code> only</p>\n"}, {'answer_id': 2895, 'author': 'vzczc', 'author_id': 224, 'author_profile': 'https://Stackoverflow.com/users/224', 'pm_score': 0, 'selected': False, 'text': '<p>Well, one way is to compile it as x64 with VS2008</p>\n\n<p>This runs as you would expect:</p>\n\n<pre><code>int normalInt = 5; \nunsigned long long int num=285212672;\nprintf(\n    "My number is %d bytes wide and its value is %ul. \n    A normal number is %d \\n", \n    sizeof(num), \n    num, \n    normalInt);\n</code></pre>\n\n<p>For 32 bit code, we need to use the correct __int64 format specifier %I64u. So it becomes. </p>\n\n<pre><code>int normalInt = 5; \nunsigned __int64 num=285212672;\nprintf(\n    "My number is %d bytes wide and its value is %I64u. \n    A normal number is %d", \n    sizeof(num),\n    num, normalInt);\n</code></pre>\n\n<p>This code works for both 32 and 64 bit VS compiler.</p>\n'}, {'answer_id': 8679, 'author': 'Nathan Fellman', 'author_id': 1084, 'author_profile': 'https://Stackoverflow.com/users/1084', 'pm_score': 7, 'selected': False, 'text': "<p>You may want to try using the inttypes.h library that gives you types such as\n<code>int32_t</code>, <code>int64_t</code>, <code>uint64_t</code> etc.\nYou can then use its macros such as:</p>\n<pre><code>#include &lt;inttypes.h&gt;\n\nuint64_t x;\nuint32_t y;\n\nprintf(&quot;x: %&quot;PRIu64&quot;, y: %&quot;PRIu32&quot;\\n&quot;, x, y);\n</code></pre>\n<p>This is &quot;guaranteed&quot; to not give you the same trouble as <code>long</code>, <code>unsigned long long</code> etc, since you don't have to guess how many bits are in each data type.</p>\n"}, {'answer_id': 51366, 'author': 'Paul Hargreaves', 'author_id': 5330, 'author_profile': 'https://Stackoverflow.com/users/5330', 'pm_score': 5, 'selected': False, 'text': '<p>That is because %llu doesn\'t work properly under Windows and %d can\'t handle 64 bit integers. I suggest using PRIu64 instead and you\'ll find it\'s portable to Linux as well.</p>\n\n<p>Try this instead:</p>\n\n<pre><code>#include &lt;stdio.h&gt;\n#include &lt;inttypes.h&gt;\n\nint main() {\n    unsigned long long int num = 285212672; //FYI: fits in 29 bits\n    int normalInt = 5;\n    /* NOTE: PRIu64 is a preprocessor macro and thus should go outside the quoted string. */\n    printf("My number is %d bytes wide and its value is %" PRIu64 ". A normal number is %d.\\n", sizeof(num), num, normalInt);\n    return 0;\n}\n</code></pre>\n\n<p>Output</p>\n\n<pre><code>My number is 8 bytes wide and its value is 285212672. A normal number is 5.\n</code></pre>\n'}, {'answer_id': 55586, 'author': 'Adam Pierce', 'author_id': 5324, 'author_profile': 'https://Stackoverflow.com/users/5324', 'pm_score': 4, 'selected': False, 'text': "<p>In Linux it is <code>%llu</code> and in Windows it is <code>%I64u</code></p>\n\n<p>Although I have found it doesn't work in Windows 2000, there seems to be a bug there!</p>\n"}, {'answer_id': 1385786, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 5, 'selected': False, 'text': '<p>For long long (or __int64) using MSVS, you should use %I64d:</p>\n\n<pre><code>__int64 a;\ntime_t b;\n...\nfprintf(outFile,"%I64d,%I64d\\n",a,b);    //I is capital i\n</code></pre>\n'}, {'answer_id': 12833401, 'author': 'Sandy', 'author_id': 517581, 'author_profile': 'https://Stackoverflow.com/users/517581', 'pm_score': 3, 'selected': False, 'text': '<p>Compile it as x64 with VS2005:</p>\n\n<blockquote>\n  <p>%llu works well.</p>\n</blockquote>\n'}, {'answer_id': 30221946, 'author': 'Shivam Chauhan', 'author_id': 4080247, 'author_profile': 'https://Stackoverflow.com/users/4080247', 'pm_score': 7, 'selected': False, 'text': '<p><code>%d</code>--> for <code>int</code></p>\n\n<p><code>%u</code>--> for <code>unsigned int</code></p>\n\n<p><code>%ld</code>--> for <code>long int</code> or <code>long</code></p>\n\n<p><code>%lu</code>--> for <code>unsigned long int</code> or <code>long unsigned int</code> or <code>unsigned long</code></p>\n\n<p><code>%lld</code>--> for <code>long long int</code> or <code>long long</code></p>\n\n<p><code>%llu</code>--> for <code>unsigned long long int</code> or <code>unsigned long long</code></p>\n'}, {'answer_id': 31635676, 'author': 'kungfooman', 'author_id': 1952626, 'author_profile': 'https://Stackoverflow.com/users/1952626', 'pm_score': 0, 'selected': False, 'text': '<p>Hex:</p>\n\n<pre><code>printf("64bit: %llp", 0xffffffffffffffff);\n</code></pre>\n\n<p>Output:</p>\n\n<pre><code>64bit: FFFFFFFFFFFFFFFF\n</code></pre>\n'}, {'answer_id': 34838293, 'author': 'Bernd Elkemann', 'author_id': 618598, 'author_profile': 'https://Stackoverflow.com/users/618598', 'pm_score': 2, 'selected': False, 'text': '<p>In addition to what people wrote years ago:</p>\n\n<ul>\n<li>you might get this error on gcc/mingw:</li>\n</ul>\n\n<p><code>main.c:30:3: warning: unknown conversion type character \'l\' in format [-Wformat=]</code></p>\n\n<p><code>printf("%llu\\n", k);</code></p>\n\n<p>Then your version of mingw does not default to c99. Add this compiler flag: <code>-std=c99</code>.</p>\n'}, {'answer_id': 55306337, 'author': '7vujy0f0hy', 'author_id': 6314667, 'author_profile': 'https://Stackoverflow.com/users/6314667', 'pm_score': 3, 'selected': False, 'text': '<p>Apparently no one has come up with a multi-platform* solution for <strong>over a decade</strong> since [the] year 2008, so I shall append mine . Plz upvote. (Joking. I don’t care.)</p>\n<h2>Solution: <code>lltoa()</code></h2>\n<p>How to use:</p>\n<pre><code>#include &lt;stdlib.h&gt; /* lltoa() */\n// ...\nchar dummy[255];\nprintf(&quot;Over 4 bytes: %s\\n&quot;, lltoa(5555555555, dummy, 10));\nprintf(&quot;Another one: %s\\n&quot;, lltoa(15555555555, dummy, 10));\n</code></pre>\n<p>OP’s example:</p>\n<pre><code>#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt; /* lltoa() */\n\nint main() {\n    unsigned long long int num = 285212672; // fits in 29 bits\n    char dummy[255];\n    int normalInt = 5;\n    printf(&quot;My number is %d bytes wide and its value is %s. &quot;\n        &quot;A normal number is %d.\\n&quot;, \n        sizeof(num), lltoa(num, dummy, 10), normalInt);\n    return 0;\n}\n</code></pre>\n<p>Unlike the <code>%lld</code> print format string, this one works for me <strong>under 32-bit GCC on Windows.</strong></p>\n<p>*) Well, <em>almost</em> multi-platform. In MSVC, you apparently need <a href="https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/itoa-itow" rel="nofollow noreferrer"><code>_ui64toa()</code></a> instead of <a href="https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.3.0/com.ibm.zos.v2r3.bpxbd00/lltoa.htm" rel="nofollow noreferrer"><code>lltoa()</code></a>.</p>\n'}, {'answer_id': 66028620, 'author': 'chux - Reinstate Monica', 'author_id': 2410359, 'author_profile': 'https://Stackoverflow.com/users/2410359', 'pm_score': 3, 'selected': False, 'text': '<blockquote>\n<p>How do you format an <code>unsigned long long int</code> using <code>printf</code>?</p>\n</blockquote>\n<p>Since C99 use an <code>&quot;ll&quot;</code> (ell-ell) before the conversion specifiers <code>o,u,x,X</code>.</p>\n<p>In addition to base 10 options in many answers, there are base 16 and base 8 options:</p>\n<p>Choices include</p>\n<pre><code>unsigned long long num = 285212672;\nprintf(&quot;Base 10: %llu\\n&quot;, num);\nnum += 0xFFF; // For more interesting hex/octal output.\nprintf(&quot;Base 16: %llX\\n&quot;, num); // Use uppercase A-F\nprintf(&quot;Base 16: %llx\\n&quot;, num); // Use lowercase a-f\nprintf(&quot;Base  8: %llo\\n&quot;, num);\nputs(&quot;or 0x,0X prefix&quot;);\nprintf(&quot;Base 16: %#llX %#llX\\n&quot;, num, 0ull); // When non-zero, print leading 0X\nprintf(&quot;Base 16: %#llx %#llx\\n&quot;, num, 0ull); // When non-zero, print leading 0x\nprintf(&quot;Base 16: 0x%llX\\n&quot;, num); // My hex fave: lower case prefix, with A-F\n</code></pre>\n<p>Output</p>\n<pre><code>Base 10: 285212672\nBase 16: 11000FFF\nBase 16: 11000fff\nBase  8: 2100007777\nor 0x,0X prefix\nBase 16: 0X11000FFF 0\nBase 16: 0x11000fff 0\nBase 16: 0x11000FFF\n</code></pre>\n'}, {'answer_id': 73709670, 'author': 'ad absurdum', 'author_id': 6879826, 'author_profile': 'https://Stackoverflow.com/users/6879826', 'pm_score': 1, 'selected': False, 'text': '<p>One possibility for formatting an <code>unsigned long long</code> is to make use of <code>uintmax_t</code>. This type has been available since C99 and unlike some of the other optional exact-width types found in <code>stdint.h</code>, <code>uintmax_t</code> is <em>required</em> by the Standard (as is its signed counterpart <code>intmax_t</code>).</p>\n<p>According to the Standard, <a href="http://port70.net/%7Ensz/c/c11/n1570.html#7.20.1.5" rel="nofollow noreferrer">a <code>uintmax_t</code> type can represent any value of any unsigned integer type</a>.</p>\n<p>You can print a <code>uintmax_t</code> value using the <code>%ju</code> conversion specifier (and <code>intmax_t</code> can be printed using <code>%jd</code>). To print a value which is not already <code>uintmax_t</code>, you must first cast to <code>uintmax_t</code> to avoid undefined behavior:</p>\n<pre><code>#include &lt;stdio.h&gt;\n#include &lt;stdint.h&gt;\n\nint main(void) {\n    unsigned long long num = 285212672;\n    printf(&quot;%ju\\n&quot;, (uintmax_t)num);\n\n    return 0;\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/2844', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/432/']