{"id":23270,"date":"2023-12-17T12:10:09","date_gmt":"2023-12-17T12:10:09","guid":{"rendered":"https:\/\/linuxways.net\/?p=23270"},"modified":"2024-03-25T02:10:28","modified_gmt":"2024-03-25T02:10:28","slug":"c-printf-formatting-specifiers","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/scripting\/c-printf-formatting-specifiers\/","title":{"rendered":"Printf() Formatting Specifiers in C Language"},"content":{"rendered":"<p>The printf() function has several interesting properties, both in terms of its use and its design and operation. First, it is a variadic function which means that it can accept as many input arguments as the programmer wishes to send, thus solving a task with a single call that would otherwise require multiple functions and calls. Another important feature of printf() is that it can display the variables in any format that is used in C and insert them into the text without converting them into strings or using the concatenation functions for it.<\/p>\n<p>In this LinuxWays article, you&#8217;ll learn how to specify the format in which you want to show the variables when using the printf() function. We&#8217;ll look at a theoretical section that shows the syntax and the method for setting the output formats. We&#8217;ll also look at the formats available in this function and learn how to nest the text and variables so you can output them to the screen with a single call.<\/p>\n<h2><a id=\"post-23270-_8tskjz4bmrqq\"><\/a>How Does the Printf() Function Work?<\/h2>\n<p>To see how printf() handles the formats, we first need to understand some aspects of the function and how its input arguments are processed. Let\u2019s see the prototype of printf():<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"kw4\">int<\/span> <a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span> <span class=\"kw4\">const<\/span> <span class=\"kw4\">char<\/span> <span class=\"sy0\">*<\/span>restrict format<span class=\"sy0\">,<\/span> ... <span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><\/div><\/div>\n<p>As we see in the prototype, the first input argument is the format pointer to a string, while the second argument is an ellipsis (&#8230;).<\/p>\n<p>The string that is pointed to by the format performs the following three functions:<\/p>\n<ol>\n<li>Contains the text that is used to concatenate the values of the variables converted to strings.<\/li>\n<li>Includes the format specifiers for the conversion of variables.<\/li>\n<li>Specifies in which part and in which order the conversion results are to be concatenated.<\/li>\n<\/ol>\n<p>In variadic functions, ellipses stand for an indefinite number of input arguments. In the case of printf(), the programmer can insert as many variables as he wants to be converted and printed, with the only rule that each of them must be separated by commas and has its own format specifier in the format string.<\/p>\n<p>When called, the printf() function parses the format string and replaces the format specifiers with the resulting string of converted variables. As we can see in the format string, we specify not only the information about the conversion formats, but also the order and location where the variable should be concatenated in the text to print the final string.<\/p>\n<p>The format identifiers that we will see in this article are compatible with all functions of this family.<\/p>\n<h2><a id=\"post-23270-_wx19iryn2lt9\"><\/a>Format Specifiers in the Printf() Function<\/h2>\n<p>Format specifiers are strings or fragments of strings that are contained in the format string of the printf() function. These strings or fragments of them determine the type of conversion and how a variable will be displayed in the output text.<\/p>\n<p>The format identifiers can be contained in explicit or implicit strings and must always begin with the symbol \u201c%\u201d and end with the letter that specifies the conversion type. Below we see the syntax of a format specifier:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"sy0\">%<\/span> <span class=\"br0\">&#91;<\/span>order<span class=\"br0\">&#93;<\/span><span class=\"br0\">&#91;<\/span>flags<span class=\"br0\">&#93;<\/span><span class=\"br0\">&#91;<\/span>width<span class=\"br0\">&#93;<\/span><span class=\"br0\">&#91;<\/span>.<span class=\"me1\">precision<\/span><span class=\"br0\">&#93;<\/span><span class=\"br0\">&#91;<\/span>length<span class=\"br0\">&#93;<\/span>type<\/div><\/div>\n<p>The arguments enclosed in square brackets are optional and, as we will see later, specify the precision, field size, etc.<\/p>\n<p>The most common way to establish a conversion format is to specify the type letter preceded by the \u201c%\u201d symbol. Let\u2019s see a table with the letter of type, the syntax used, and a short description for each:<\/p>\n<table>\n<tbody>\n<tr>\n<td><strong>Type<\/strong><\/td>\n<td><strong>Syntax<\/strong><\/td>\n<td><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td><strong><em>c<\/em><\/strong><\/td>\n<td><strong><em>%c<\/em><\/strong><\/td>\n<td>Print ASCII character<\/td>\n<\/tr>\n<tr>\n<td><strong><em>s<\/em><\/strong><\/td>\n<td><strong><em>%s<\/em><\/strong><\/td>\n<td>Print character strings<\/td>\n<\/tr>\n<tr>\n<td><strong><em>i, d<\/em><\/strong><\/td>\n<td><strong><em>%i, %d<\/em><\/strong><\/td>\n<td>Signed integers conversion<\/td>\n<\/tr>\n<tr>\n<td><strong><em>u<\/em><\/strong><\/td>\n<td><strong><em>%u<\/em><\/strong><\/td>\n<td>Unsigned integers conversion<\/td>\n<\/tr>\n<tr>\n<td><strong><em>f, F<\/em><\/strong><\/td>\n<td><strong><em>%f, %F<\/em><\/strong><\/td>\n<td>Floating point conversion (standard notation)<\/td>\n<\/tr>\n<tr>\n<td><strong><em>e, E<\/em><\/strong><\/td>\n<td><strong><em>%e, %E<\/em><\/strong><\/td>\n<td>Floating point conversion (scientific notation)<\/td>\n<\/tr>\n<tr>\n<td><strong><em>g, G<\/em><\/strong><\/td>\n<td><strong><em>%g, %G<\/em><\/strong><\/td>\n<td>Reduced floating point conversion<\/td>\n<\/tr>\n<tr>\n<td><strong><em>x, X<\/em><\/strong><\/td>\n<td><strong><em>%x, %X<\/em><\/strong><\/td>\n<td>Hexadecimal conversion<\/td>\n<\/tr>\n<tr>\n<td><strong><em>o<\/em><\/strong><\/td>\n<td><strong><em>%o<\/em><\/strong><\/td>\n<td>Octal conversion<\/td>\n<\/tr>\n<tr>\n<td><strong><em>p<\/em><\/strong><\/td>\n<td><strong><em>%p<\/em><\/strong><\/td>\n<td>Pointer conversion<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>How to Specify the Conversion Format in the Printf() Function<\/h2>\n<p>Now, let&#8217;s look at an example where we declare a variable for each conversion type, assign it with a value, and then print it to the command console using the printf() function, specifying the appropriate format for each type:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;height:100%;\"><div class=\"c codecolorer\"><span class=\"co2\">#include &lt;stdio.h&gt;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">void<\/span> main <span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">char<\/span> a <span class=\"sy0\">=<\/span><span class=\"st0\">'Z'<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw4\">char<\/span> <span class=\"sy0\">*<\/span> Ptr <span class=\"sy0\">=<\/span><span class=\"st0\">&quot;Hello World&quot;<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw4\">int<\/span> &nbsp;b <span class=\"sy0\">=-<\/span><span class=\"nu0\">2023<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw4\">unsigned<\/span> <span class=\"kw4\">int<\/span> c <span class=\"sy0\">=<\/span><span class=\"nu0\">12345<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw4\">float<\/span> d <span class=\"sy0\">=<\/span><span class=\"nu16\">1.123456789<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw4\">double<\/span> sc <span class=\"sy0\">=<\/span><span class=\"nu19\">1.123457e+00<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw4\">int<\/span> o <span class=\"sy0\">=<\/span><span class=\"nu0\">100<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw4\">int<\/span> x <span class=\"sy0\">=<\/span> <span class=\"nu12\">0x2EFF02<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;char: %c<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">,<\/span> a<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;string: %s<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">,<\/span> Ptr<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;Pointer address: %p<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">,<\/span> Ptr<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;int: %i<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">,<\/span> b<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;unsigned int: %u<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">,<\/span> c<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;float: %f<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">,<\/span> d<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;scientific float: %e<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">,<\/span> d<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;octal: %o<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">,<\/span> o<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;hexadecimal: 0x%x<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">,<\/span> x<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>Let\u2019s see the compilation and execution of this code:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" class=\"wp-image-23274\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23270-1.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23270-1.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23270-1-300x225.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23270-1-768x576.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>As we can see in the picture, the identifiers have been replaced by the value of the corresponding variable. To make the example more readable, we use a call to the printf() function for each variable to be displayed, but the correct way is to resolve everything in a single call as shown in the following:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;char: %c<span class=\"es1\">\\n<\/span>string: %s<span class=\"es1\">\\n<\/span>Pointer address: %p<span class=\"es1\">\\n<\/span>int: %i<span class=\"es1\">\\n<\/span>unsigned int: %u<span class=\"es1\">\\n<\/span>float: %f<span class=\"es1\">\\n<\/span>scientific float: %e<span class=\"es1\">\\n<\/span>octal: %o<span class=\"es1\">\\n<\/span>hexadecimal: 0x%x<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">,<\/span> a<span class=\"sy0\">,<\/span> Ptr<span class=\"sy0\">,<\/span> Ptr<span class=\"sy0\">,<\/span> b<span class=\"sy0\">,<\/span> c<span class=\"sy0\">,<\/span> d<span class=\"sy0\">,<\/span> sc<span class=\"sy0\">,<\/span> o<span class=\"sy0\">,<\/span> x<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><\/div><\/div>\n<h2>How to Set the Order of Variables in the Printf() Function<\/h2>\n<p>As we have already seen, the variables, by default, are displayed in the final text in the same order in which they appear in the input arguments. However, we can change this order using the format specifier. Let\u2019s see a table with the syntax that we can use to specify the order of the variables:<\/p>\n<table>\n<tbody>\n<tr>\n<td><strong>Syntax<\/strong><\/td>\n<td><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td><strong>&#8220;%<\/strong>n$<strong>[type]&#8221;<\/strong><\/td>\n<td>n stands for the order of occurrence of the variables<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Next, let&#8217;s look at an example where we call the printf() function twice to display the \u201cHello\u201d and \u201cWorld\u201d strings in the console.<\/p>\n<p>The first call is done by default, while we change the order in the second call using the \u201c%2s%1s\u201d specifications. Let\u2019s see the code for this example:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"co2\">#include &lt;stdio.h&gt;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">void<\/span> main <span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n<span class=\"kw4\">char<\/span> <span class=\"sy0\">*<\/span> str_1 <span class=\"sy0\">=<\/span><span class=\"st0\">&quot;Hello &quot;<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw4\">char<\/span> <span class=\"sy0\">*<\/span> str_2 <span class=\"sy0\">=<\/span><span class=\"st0\">&quot;World &quot;<\/span><span class=\"sy0\">;<\/span><br \/>\n<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;%s%s<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">,<\/span> str_1<span class=\"sy0\">,<\/span> str_2<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;%2$s%1$s<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">,<\/span> str_1<span class=\"sy0\">,<\/span> str_2<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>As seen in the following figure, the \u201c$\u201d specifications change the order of appearance of the variables:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" class=\"wp-image-23277\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23270-2.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23270-2.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23270-2-300x225.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23270-2-768x576.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<h2><a id=\"post-23270-_ulds6g7sktrt\"><\/a>Flags in the Format Specifiers of the Printf() Function<\/h2>\n<p>The flag field of a format specifier adds the following functions to the conversion:<\/p>\n<ol>\n<li>Prepends the base notation of a variable<\/li>\n<li>Prepends the sign of a variable<\/li>\n<li>Prepends a blank space before a variable<\/li>\n<\/ol>\n<p>Let\u2019s see a table of the most commonly used flags in the print() function:<\/p>\n<table>\n<tbody>\n<tr>\n<td><strong>Flag<\/strong><\/td>\n<td><strong>Syntax<\/strong><\/td>\n<td><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td><strong>#<\/strong><\/td>\n<td><strong>&#8220;%#[type]&#8221;<\/strong><\/td>\n<td>Prepends the base notation of the variable<\/td>\n<\/tr>\n<tr>\n<td>+<\/td>\n<td><strong>&#8220;%<\/strong>+<strong>[type]&#8221;<\/strong><\/td>\n<td>Shows the sign of the variable<\/td>\n<\/tr>\n<tr>\n<td>&#8216; &#8216;<\/td>\n<td><strong>&#8220;%<\/strong>&#8216; &#8216;<strong>[type]&#8221;<\/strong><\/td>\n<td>Prepends a blank space<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>How to Prefix the Base Notation of a Variable with the Printf() Function in the C Language<\/h2>\n<p>In this example, we convert the variable \u201ca\u201d from an integer to a hexadecimal number, and then call the printf() function twice to display it on the screen. In the first call, we omit the <strong><em># <\/em><\/strong>indicator, while we insert it in the second call so that the base notation is displayed before the variable&#8217;s value.<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"co2\">#include &lt;stdio.h&gt;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">void<\/span> main <span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n<span class=\"kw4\">int<\/span> a <span class=\"sy0\">=<\/span> <span class=\"nu0\">2222<\/span><span class=\"sy0\">;<\/span><br \/>\n<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;Without: %x<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">,<\/span> a<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;With: %#x<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">,<\/span> a<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>Let\u2019s see the following image that shows the execution of this code:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" class=\"wp-image-23281\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23270-3.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23270-3.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23270-3-300x225.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23270-3-768x576.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<h2><a id=\"post-23270-_giohnx7y85e6\"><\/a>How to Print the Sign of a Variable with the Printf() Function in the C Language<\/h2>\n<p>In this example, we will see how to print the sign of an integer variable. In C, the signed variables with negative values always output their sign by default, so the \u201c+\u201d specifier only affects the positive values.<\/p>\n<p>In the following code, we declare the integer \u201ca\u201d and assign it the value of 12345. Then, we call the printf() function twice to print this variable. The first call does not include the \u201c+\u201d specifier, but the second call does.<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"co2\">#include &lt;stdio.h&gt;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">void<\/span> main <span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n<span class=\"kw4\">int<\/span> a <span class=\"sy0\">=<\/span> <span class=\"nu0\">12345<\/span><span class=\"sy0\">;<\/span><br \/>\n<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;Without +: %i<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">,<\/span> a<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;With +: %+i<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">,<\/span> a<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>The following image shows the execution of this code:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" class=\"wp-image-23284\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23270-4.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23270-4.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23270-4-300x225.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23270-4-768x576.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<h2><a id=\"post-23270-_u72j69qlzidv\"><\/a>Decimal Precision and Width Fields in the Printf() Function<\/h2>\n<p>The precision and width fields in the format specifiers determine the precision and width of the field in which the variables are displayed. This specifier has the following two usage modes:<\/p>\n<ol>\n<li>Sets the precision of the decimal part in floating point values<\/li>\n<li>Sets the size of the field where the integer, double, long, and string variables will be displayed<\/li>\n<\/ol>\n<p>For each of these specifiers that we insert into the string, an input argument \u201cn\u201d is added in the order of their occurrence of the variables, specifying the number of fields or precision digits to be displayed.<\/p>\n<p>Let\u2019s see a table with the syntax of these specifiers and a description of them:<\/p>\n<table>\n<tbody>\n<tr>\n<td><strong>Specifier<\/strong><\/td>\n<td><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td><strong>&#8220;%.*[type]&#8221;,n<\/strong><\/td>\n<td>Specifies the precision in the decimal part of a floating-point value.<\/td>\n<\/tr>\n<tr>\n<td><strong>&#8220;%* [type]&#8221;,n<\/strong><\/td>\n<td>Specifies the number of fields to display.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2><a id=\"post-23270-_adgd4hu96sro\"><\/a><br \/>\nHow to Set a Decimal Precision on a Floating Point Value with the Printf() Function<\/h2>\n<p>In this example, we use the \u201c%.*\u201d formatter that we just saw to specify the decimal precision to display a floating point number. To do this, we declare the variable \u201ca\u201d of type float and give it the value of 1.22222222. Then, we use the \u201c%.*\u201d specifier to set the decimal precision of \u201ca\u201d to two digits, then to 3 and 4. Let\u2019s see the code for this example:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"co2\">#include &lt;stdio.h&gt;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">void<\/span> main <span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n<span class=\"kw4\">float<\/span> a <span class=\"sy0\">=<\/span> <span class=\"nu16\">1.22222222<\/span><span class=\"sy0\">;<\/span><br \/>\n<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;%.*f<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">,<\/span> <span class=\"nu0\">2<\/span><span class=\"sy0\">,<\/span> a<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;%.*f<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">,<\/span> <span class=\"nu0\">3<\/span><span class=\"sy0\">,<\/span> a<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;%.*f<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">,<\/span> <span class=\"nu0\">4<\/span><span class=\"sy0\">,<\/span> a<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>As we can see, the printf() function now has three input arguments &#8211; the second is the precision indicator and the third is the variable to display. Let\u2019s see the compilation and execution of this code:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" class=\"wp-image-23287\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23270-5.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23270-5.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23270-5-300x225.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23270-5-768x576.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<h2><a id=\"post-23270-_a4byohqhc9m7\"><\/a>How to Adjust the Number of Fields to be Displayed in a Variable or String in the Printf() Function<\/h2>\n<p>In this example, we will use the \u201c%*\u201d formatter to adjust the number of fields that we want to display from a variable or string. The operation of this formatter is identical to the previous one, except that its syntax does not include a period and it works with integers, double numbers, and strings.<\/p>\n<p>In the following code, we will use the \u201cstr_1\u201d pointer with the <em>\u201cHello world! Hello Linux Ways!\u201d<\/em> string and we use the incremental variable \u201ci&#8221; as an index of the number of fields in a \u201cfor\u201d loop, creating an animation in which the text is written letter by letter every 50 ms.<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"co2\">#include &lt;stdio.h&gt;<\/span><br \/>\n<span class=\"co2\">#include &lt;stdlib.h&gt;<\/span><br \/>\n<span class=\"co2\">#include &lt;unistd.h&gt;<\/span><br \/>\n<span class=\"co2\">#include &lt;string.h&gt;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">void<\/span> main <span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n<span class=\"kw1\">while<\/span> <span class=\"br0\">&#40;<\/span><span class=\"nu0\">1<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n<span class=\"kw4\">char<\/span> <span class=\"sy0\">*<\/span> str_2 <span class=\"sy0\">=<\/span><span class=\"st0\">&quot;Hello world! Hello Linux Ways!!&quot;<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw1\">for<\/span> <span class=\"br0\">&#40;<\/span><span class=\"kw4\">int<\/span> i <span class=\"sy0\">=<\/span><span class=\"nu0\">0<\/span><span class=\"sy0\">;<\/span> i<span class=\"sy0\">!=<\/span><a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/strlen.html\"><span class=\"kw3\">strlen<\/span><\/a><span class=\"br0\">&#40;<\/span>str_2<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span>i<span class=\"sy0\">++<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/system.html\"><span class=\"kw3\">system<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;clear&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;%.*s<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">,<\/span> i<span class=\"sy0\">,<\/span> str_2<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\nusleep<span class=\"br0\">&#40;<\/span><span class=\"nu0\">50000<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><br \/>\nsleep<span class=\"br0\">&#40;<\/span><span class=\"nu0\">1<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>Let\u2019s see an image with the execution of this code:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" class=\"wp-image-23290\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23270-6.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23270-6.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23270-6-300x225.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23270-6-768x576.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<h2><a id=\"post-23270-_6brx2m6wj0nd\"><\/a>The Length Field in the Format Specifier of the Printf() Function<\/h2>\n<p>Format specifiers also provide the option to convert certain types of data to integers. Let\u2019s see a table of them, their syntax, and a description of each:<\/p>\n<table>\n<tbody>\n<tr>\n<td><strong>Syntax<\/strong><\/td>\n<td><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td><strong>\u201c%hhi\u201d<\/strong><\/td>\n<td>Converts a char to int and prints it<\/td>\n<\/tr>\n<tr>\n<td><strong>\u201c%hi\u201d<\/strong><\/td>\n<td>Converts a short int to int and prints it<\/td>\n<\/tr>\n<tr>\n<td><strong>\u201c%li\u201d<\/strong><\/td>\n<td>Converts a long to int and prints it<\/td>\n<\/tr>\n<tr>\n<td><strong>\u201c%lli\u201d<\/strong><\/td>\n<td>Converts a long long to int and prints it<\/td>\n<\/tr>\n<tr>\n<td><strong>\u201c%Li\u201d<\/strong><\/td>\n<td>Converts a long double to int and prints it<\/td>\n<\/tr>\n<tr>\n<td><strong>\u201c%zi\u201d<\/strong><\/td>\n<td>Converts a size_t to int and prints it<\/td>\n<\/tr>\n<tr>\n<td><strong>\u201c%ji!<\/strong><\/td>\n<td>Converts a intmax_t or uintmax_t to int and prints it<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>How to Convert a Data Type to Integer with the Format Specifier in the Printf() Function<\/h2>\n<p>In this example, we will show you how to convert a character and a long value to an integer and display it to the command console using the width field of the printf() format specifier. For this, we declare the character \u201cc\u201d and the long l and assign them with a random value. Then, we call printf() on each of these variables, passing the &#8220;%hhi&#8221; specifier for the character and &#8220;%li&#8221; for the long. Let\u2019s see the code for this example:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"co2\">#include &lt;stdio.h&gt;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">void<\/span> main <span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n<br \/>\n&nbsp; <span class=\"kw4\">char<\/span> c <span class=\"sy0\">=<\/span> <span class=\"st0\">'C'<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; <span class=\"kw4\">long<\/span> l <span class=\"sy0\">=<\/span> <span class=\"nu0\">1234899876<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; <a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;%hhi<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">,<\/span> c<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; <a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;%li<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">,<\/span> l<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>The following image shows the execution of this code, with the values of \u201cc\u201d and l converted to integers:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" class=\"wp-image-23292\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23270-7.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23270-7.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23270-7-300x225.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23270-7-768x576.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<h2><a id=\"post-23270-_teny07nfzj3n\"><\/a>Conclusion<\/h2>\n<p>In this Linux Ways article, we taught you all about the format specifications of the printf() family of functions so that you can harness their potential and save the lines of code by avoiding multiple calls to other functions. We explained how these functions handle their input arguments and we showed you what the syntax of a format specifier is. Then, in several sections, we explained the individual fields and showed how we can use them to specify the format and order of appearance of the variables that we want to print.<\/p>","protected":false},"excerpt":{"rendered":"<p>Guide on how to specify the format in which you want to show a variable when using the printf() function, the syntax, and the method to set the output formats.<\/p>","protected":false},"author":110,"featured_media":23303,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[168],"tags":[],"class_list":["post-23270","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/23270","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/110"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=23270"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/23270\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/23303"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=23270"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=23270"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=23270"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}