{"id":1323,"date":"2023-02-03T12:43:31","date_gmt":"2023-02-03T12:43:31","guid":{"rendered":"https:\/\/ibex.tech\/c\/?p=1323"},"modified":"2024-06-07T15:33:37","modified_gmt":"2024-06-07T14:33:37","slug":"printf","status":"publish","type":"post","link":"https:\/\/ibex.tech\/c\/c\/console\/printf","title":{"rendered":"printf"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;<\/code><\/pre>\n\n\n\n<p><em><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-orange-color\">Remember to include a terminating \u201c\\n\u201d in your printf&nbsp;\u2013&nbsp;stdout&nbsp;doesn\u2019t flush until it encounters one by default!!!!<\/mark><\/strong><\/em><\/p>\n\n\n\n<p><em><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-orange-color\">If printf still doesn&#8217;t occur you may need fflush(stdout); or consider using std::cout instead<\/mark><\/strong><\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\tprintf(\"Hello world!\\n\");\n\n\tprintf(\"%i bytes read : %s\\n\", rx_length, rx_buffer);\n\n\tprintf(\"data_h: %02X\\n\", sensor_data_h);<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">printf format codes<\/h5>\n\n\n<pre style=\"padding-left: 40px;\">bool %d<br \/>int %d<br \/>int %i<br \/>int8_t %hhd<br \/>uint8_t %hhu<br \/>int16_t %hd<br \/>uint16_t %hu<br \/>int32_t %ld<br \/>uint32_t %lu<br \/>int64_t %lld<br \/>uint64_t %llu<br \/>char[] %s<br \/>float %f (Decimal floating point)<br \/>float %.3f (3 represents the number of digits after the decimal separator)<br \/>float %e (Scientific notation (mantissa\/exponent), lowercase)<br \/>float %g (Use the shortest representation:\u00a0<tt>%e<\/tt>\u00a0or\u00a0<tt>%f<\/tt>)<br \/>double %lf (up to 6 digits of precision using standard decimal notation, without any trailing zeros)<br \/>double %lg (up to 6 digits of precision which can use scientific notation if its shorter)<br \/><br \/>\/\/Displaying hex:<br \/>uin16_t 0x%04X (display 4 characters of hex)<br \/><br \/>\/\/Alternative bool<br \/>printf(\"%s\", x ? \"true\" : \"false\");<br \/><br \/><br \/><\/pre>\n\n\n<p>d or i \u2013 int, decimal (base ten) number<\/p>\n\n\n\n<p>x or X \u2013 int, hexadecimal number.&nbsp; Also \u201c%02x\u201d to specify length of digits<\/p>\n\n\n\n<p>ld \u2013 long, decimal number (\u2018l\u2019 can also be applied to any of the above to change the type from \u2018int\u2019 to \u2018long\u2019)<\/p>\n\n\n\n<p>u \u2013 unsigned, decimal number<\/p>\n\n\n\n<p>lu \u2013 unsigned long, decimal number<\/p>\n\n\n\n<p>llu \u2013 unsigned long long, decimal number (uint64_t)<\/p>\n\n\n\n<p>c \u2013 char, single character<\/p>\n\n\n\n<p>s \u2013 char pointer, string<\/p>\n\n\n\n<p>f \u2013 float, number with six digits of precision<br>g \u2013 float, number with up to six digits of precision<br>e \u2013 float, number with up to six digits of precision, scientific notation<br>lf \u2013 double, number with six digits of precision<br>lg \u2013 double, number with up to six digits of precision<br>le \u2013 double, number with up to six digits of precision, scientific notation<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Including values examples<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">Hex values<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\t\/\/Include a 2 digit HEX value \"%02\"\n\tprintf(\"MyValue: 0x%02X\\n\", (uint16_t)SwitchInputs);\n\n\t\/\/Include a 4 digit HEX value \"%04\"\n\tprintf(\"MyValue: 0x%04X\\n\", (uint16_t)SwitchInputs);<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Int values<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\t\/\/Force to 2 digits\n\tprintf(\"MyValue: %02i\\n\", (uint16_t)TimeHours);<\/code><\/pre>\n\n\n\n<p>Result of an operation<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\tprintf((err != ESP_OK) ? \"Failed!\\n\" : \"Done\\n\");<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">printf in a multi-thread environment<\/h4>\n\n\n\n<p>Watch out for clashes causing bugs!&nbsp; This can happen with irq\u2019s using printf, different threads using it, etc.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Using a semaphore to protect<\/h5>\n\n\n\n<p>This is one option to stop clahses, e.g.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include \"semphr.h\"\n\n\/\/Create semphore\nSemaphoreHandle_t print_semaphore = NULL;\n\n\tprint_semaphore = xSemaphoreCreateMutex();\n    \n    \n\/\/Use sempahore\n\txSemaphoreTake(print_semaphore, portMAX_DELAY);\n\tprintf(\"Something\");\n\tprintf(\"Something\");\n\txSemaphoreGive(print_semaphore);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Remember to include a terminating \u201c\\n\u201d in your printf&nbsp;\u2013&nbsp;stdout&nbsp;doesn\u2019t flush until it encounters one by default!!!! If printf still doesn&#8217;t occur you may need fflush(stdout); or consider using std::cout instead printf format codes bool %dint %dint %iint8_t %hhduint8_t %hhuint16_t %hduint16_t %huint32_t %lduint32_t %luint64_t %llduint64_t %lluchar[] %sfloat %f (Decimal floating point)float %.3f (3 represents the number [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[111,113],"tags":[],"class_list":["post-1323","post","type-post","status-publish","format-standard","hentry","category-console","category-console-c-2"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/posts\/1323","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/comments?post=1323"}],"version-history":[{"count":22,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/posts\/1323\/revisions"}],"predecessor-version":[{"id":1579,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/posts\/1323\/revisions\/1579"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/media?parent=1323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/categories?post=1323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/tags?post=1323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}