Input & Output on Console

C Programming Language Overview of C Language (Article) Overview of C Language (Program)

1728

Program:

<span class="com">#include</span><span class="pln"> </span><span class="str">&lt;stdio.h&gt;</span><span class="pln">
</span><span class="kwd">int</span><span class="pln"> main</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
	</span><span class="kwd">int</span><span class="pln"> n </span><span class="pun">=</span><span class="pln"> </span><span class="lit">10</span><span class="pun">;</span><span class="pln">
	</span><span class="kwd">float</span><span class="pln"> m </span><span class="pun">=</span><span class="pln"> </span><span class="lit">1.245</span><span class="pun">;</span><span class="pln">
	</span><span class="kwd">char</span><span class="pln"> c </span><span class="pun">=</span><span class="pln"> </span><span class="str">'A'</span><span class="pun">;</span><span class="pln">
	printf</span><span class="pun">(</span><span class="str">"%d \n"</span><span class="pun">,</span><span class="pln">n</span><span class="pun">);</span><span class="pln">
	printf</span><span class="pun">(</span><span class="str">"%f \n"</span><span class="pun">,</span><span class="pln">m</span><span class="pun">);</span><span class="pln">
	printf</span><span class="pun">(</span><span class="str">"%.2f \n"</span><span class="pun">,</span><span class="pln">m</span><span class="pun">);</span><span class="pln">
	printf</span><span class="pun">(</span><span class="str">"%.3f \n"</span><span class="pun">,</span><span class="pln">m</span><span class="pun">);</span><span class="pln">
	printf</span><span class="pun">(</span><span class="str">"%c \n"</span><span class="pun">,</span><span class="pln">c</span><span class="pun">);</span><span class="pln">
	</span><span class="kwd">return</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pln">
</span><span class="pun">}</span>

Output:

10
1.245000
1.25
1.245
A
Press any key to continue . . .

Explanation:

You can see on %f that wrote floating data with 6 decimal digits. You can specify the number of decimal digit by passing numeric after dot (.), for instance, %.2f and %.3f.


The given code is a C program that demonstrates how to print values of variables of different data types to the console using the printf() function with different format specifiers.

Here's how it works:

  • The #include <stdio.h> line includes the standard input/output library in the program.
  • The main() function is the entry point of the program.
  • Three variables n, m, and c are declared and initialized with values of integer, float, and character data types respectively.
  • The printf() function is used to print the values of the variables to the console using different format specifiers:
    • %d format specifier is used to print the integer value of n.
    • %f format specifier is used to print the floating-point value of m.
    • %.2f format specifier is used to print the floating-point value of m with two decimal places.
    • %.3f format specifier is used to print the floating-point value of m with three decimal places.
    • %c format specifier is used to print the character value of c.
  • The \n character is used to insert a newline after each output is printed.
  • Finally, the return 0; statement terminates the program and returns 0 to the operating system indicating that the program executed successfully.

This Particular section is dedicated to Programs only. If you want learn more about C Programming Language. Then you can visit below links to get more depth on this subject.