<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: jiresimon</title>
    <description>The latest articles on Forem by jiresimon (@jiresimon).</description>
    <link>https://forem.com/jiresimon</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F646230%2Ff8a0588b-972b-4aad-8f8b-170aab439c34.jpg</url>
      <title>Forem: jiresimon</title>
      <link>https://forem.com/jiresimon</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/jiresimon"/>
    <language>en</language>
    <item>
      <title>Formatted Input and Output Functions in C - Basic Guide</title>
      <dc:creator>jiresimon</dc:creator>
      <pubDate>Thu, 30 Jun 2022 23:47:46 +0000</pubDate>
      <link>https://forem.com/jiresimon/formatted-input-and-output-functions-in-c-basic-guide-2103</link>
      <guid>https://forem.com/jiresimon/formatted-input-and-output-functions-in-c-basic-guide-2103</guid>
      <description>&lt;p&gt;If you're a beginner in the C programming language, then here's an important concept for you to learn.&lt;/p&gt;

&lt;p&gt;You might have wondered - how does the computer receive an input of characters, read and print the input of characters in C?&lt;/p&gt;

&lt;p&gt;In C programming, the &lt;strong&gt;scanf() and printf()&lt;/strong&gt; functions are two basic formatted input and output functions. &lt;/p&gt;

&lt;h2&gt;
  
  
  Why use a formatted input and output function in C?
&lt;/h2&gt;

&lt;p&gt;These basic functions accept arguments containing a format specification string and a variable or multiple variables.&lt;/p&gt;

&lt;p&gt;What does that mean? It means the functions can allow multiple inputs and outputs.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;printf("Hello world");&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Hmm, Is that the only thing you can do with the "printf() function"?&lt;/p&gt;

&lt;p&gt;As you continue to read on, you'll see multiple interesting ways to use that function. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IEi5Iz20--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fve99mtjruqilau7t2yk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IEi5Iz20--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fve99mtjruqilau7t2yk.png" alt="formatted-input-and-output-in-c-basic-guide" width="880" height="370"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Format specifiers in C
&lt;/h2&gt;

&lt;p&gt;Here are four common format specifiers in C for a formatted input and output in C:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Format Specifiers&lt;/th&gt;
&lt;th&gt;Functions&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;%d or %i&lt;/td&gt;
&lt;td&gt;integers&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;%c&lt;/td&gt;
&lt;td&gt;characters&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;%f&lt;/td&gt;
&lt;td&gt;floating point values&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;%s&lt;/td&gt;
&lt;td&gt;strings&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Other format specifiers &lt;strong&gt;&lt;em&gt;are %u, %lu, %lf&lt;/em&gt;&lt;/strong&gt; etc...To find out more on format specifiers, check out &lt;strong&gt;"The C Programming Language by Brian W.Kernighan and Dennis M. Ritchie"&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Or read this article on &lt;a href="https://www.tutorialspoint.com/format-specifiers-in-c"&gt;format specifiers in C&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The printf() function
&lt;/h2&gt;

&lt;p&gt;The output function in C programming is called "printf." This function converts, formats, and prints to the standard output(screen).&lt;/p&gt;

&lt;h3&gt;
  
  
  Basic syntax of the printf() function
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;--&amp;gt; printf(format, var1, var2,...)&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  How to use the printf() function in C
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Step1: Include the header file
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;#include &amp;lt;stdio.h&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The header file contains the printf() function - a basic I/O(standard input and output) function.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step2: Write the program inside the main function
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;int main(){program codes}&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Here's a simple program in C using the printf() function:

#include &amp;lt;stdio.h&amp;gt;

int main(){
     printf("Welcome to my tech blog, and hope you're having a fun-time reading my article");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Adding format specifiers to the printf() function
&lt;/h3&gt;

&lt;p&gt;Here are examples of using the format specifier with the printf() function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;stdio.h&amp;gt;

int main(){

    int num1 = 1234; //declare and initialize the variable


    //NOTE: The colon is just to serve as a boundary to see the start and end
    //Every example has an output and reason stated together

    //Example1
    printf(":%d:\n", num1);
    //output&amp;gt;&amp;gt; :1234:
    //Reason&amp;gt;&amp;gt; prints the variable

    //Example2
    printf(":%10d:\n", num1);
    //output&amp;gt;&amp;gt; :      1234:
    //Reason&amp;gt;&amp;gt; allocates 10 digits to the variable...The number has 4digits
    //and that's why there are 6empty digits

    //Example3
    printf(":%010d:\n", num1);
    //output&amp;gt;&amp;gt; :0000001234:
    //Reason&amp;gt;&amp;gt; The 6empty digits gets filled with zeros

    //Example4
    printf(":%-10d:\n", num1);
    //output&amp;gt;&amp;gt; :1234      :
    //Reason&amp;gt;&amp;gt; variable is shifted to the left and the other digits are empty

    //Example5
    printf(":%-15.10i:\n", num1); 
    //output&amp;gt;&amp;gt; :0000001234     :
    //Reason&amp;gt;&amp;gt; left justified and the empty digits are filled with zeros


    //Example6
    printf(":%.10d:\n", num1);
    //output&amp;gt;&amp;gt; :0000001234:
    //Reason&amp;gt;&amp;gt; Same output as Ex3 since the datatype isn't a float


    //Example7
    printf(":%15.10d:\n", num1);
    //output&amp;gt;&amp;gt; :     0000001234:
    //Reason&amp;gt;&amp;gt; 15 digits but only 10 gets filled up...4digits to the number(1234) and 6digits are filled up with zero

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The scanf() function
&lt;/h2&gt;

&lt;p&gt;The scanf() function reads characters from the standard input(keyboard) and converts them based on the format specification string. &lt;/p&gt;

&lt;p&gt;The inputs are also stored in the memory addresses through the other arguments.&lt;/p&gt;

&lt;h3&gt;
  
  
  Basic Syntax
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;scanf(format, &amp;amp;var1, &amp;amp;var2)&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  How to use the scanf() function
&lt;/h3&gt;

&lt;p&gt;Apart from including the header file and the main() function, here's a rule for using the scanf() function in your program.&lt;/p&gt;

&lt;p&gt;If your variable is a string or character datatype, there's no need to add the &lt;em&gt;"&amp;amp;"&lt;/em&gt; symbol. Other datatypes will require the &lt;em&gt;"&amp;amp;"&lt;/em&gt; symbol to store the inputs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Example1

#include &amp;lt;stdio.h&amp;gt;

int main(){
     int birth_month;

     printf("Enter your birth month in figures:&amp;gt;&amp;gt;&amp;gt; ");
     scanf("%d", &amp;amp;birth_month);
     printf("Your birth month is, %d", birth_month);
}

//Output

Enter your birth month in figures:&amp;gt;&amp;gt;&amp;gt; 10
Your birth month is, 12
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Other formatted input and output functions
&lt;/h2&gt;

&lt;p&gt;sprintf() and sscanf() functions are other notable functions in C but differ slightly in usage. &lt;/p&gt;

&lt;p&gt;You can read about the &lt;a href="https://www.tutorialspoint.com/c_standard_library/c_function_sprintf.htm"&gt;sprinf() function&lt;/a&gt; and &lt;a href="https://www.tutorialspoint.com/c_standard_library/c_function_sscanf.htm"&gt;sscanf() function&lt;/a&gt; here.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Formatted input and output help to modify outputs, inputs, and allow multiple variables. &lt;/p&gt;

&lt;p&gt;To read more about formatted input and output, read the chapter1 (variables and arithmetic expressions, and chapter7) of "The C Programming Language by Brian W.Kernighan, and Dennis M. Ritchie"&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>programming</category>
      <category>c</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
