{"id":1886,"date":"2024-05-10T06:52:37","date_gmt":"2024-05-10T06:52:37","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=1886"},"modified":"2024-05-10T06:52:37","modified_gmt":"2024-05-10T06:52:37","slug":"basic-input-and-output-in-c","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/basic-input-and-output-in-c\/","title":{"rendered":"Basic Input And Output In C"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#basic-input-and-output-in-c\">Basic Input And Output In C<\/a><\/li><li ><a href=\"#how-to-take-the-input-and-output-of-basic-types-in-c\">How to take the input and output of basic types in C?<\/a><\/li><li ><a href=\"#how-to-take-input-and-output-of-advanced-type-in-c\">How to take input and output of advanced type in C?<\/a><\/li><li ><a href=\"#faq-basic-input-and-output-in-c\">FAQ- Basic Input And Output In C<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"basic-input-and-output-in-c\">Basic Input And Output In C<\/h2>\n\n\n\n<p>Basic Input and Output (I\/O) in the C programming language is fundamental to interacting with the user and the external world. It provides the means to receive data from the user, display information to the user, and read from or write to files. C&#8217;s I\/O functions are concise yet powerful, making it possible to create robust command-line programs and handle file operations efficiently.<\/p>\n\n\n\n<p> In this exploration of Basic Input and Output in C, we will delve into the foundational concepts and functions that allow developers to handle input from the keyboard, output to the screen, and manage data in files. Understanding these principles is essential for building interactive and data-driven applications using the C language.<\/p>\n\n\n\n<p><strong>scanf()<\/strong><\/p>\n\n\n\n<p>In C, the <code>scanf()<\/code> function is used to read values from the console based on the specified data type.<\/p>\n\n\n\n<p>In <code>scanf(\"%X\", &amp;variableOfXType);<\/code>, <code>%X<\/code> is indeed the format specifier in C. It&#8217;s used to specify that you are expecting hexadecimal input. However, it&#8217;s not necessarily about telling the compiler the data type of the variable; it&#8217;s more about indicating how the data should be interpreted when it&#8217;s read.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>%X<\/code> expects hexadecimal input, and it tells <code>scanf<\/code> us to convert the input to an integer using base 16 (hexadecimal).<\/li>\n\n\n\n<li><code>&amp;<\/code> is indeed the address operator in C, but it&#8217;s not changing the real value of the variable; rather, it&#8217;s providing the memory location (address) where <code>scanf<\/code> should store the input value.<\/li>\n<\/ul>\n\n\n\n<p>So, in your example, <code>scanf<\/code> reads a hexadecimal value from the user and stores it in the memory location pointed to by <code>&amp;variableOfXType<\/code>. It&#8217;s about converting the input data to the specified format and storing it at the given memory address.<\/p>\n\n\n\n<p><strong>printf()<\/strong><\/p>\n\n\n\n<p>In C, the <code>printf()<\/code> function is used to display (or print) values on the console screen. It takes one or more parameters, known as format specifiers, and prints them according to the format specified in the function&#8217;s argument. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int num = 42;\nprintf(\"The value of num is: %d\\n\", num);<\/code><\/pre>\n\n\n\n<p>In this example, <code>%d<\/code> is a format specifier that indicates the integer value <code>num<\/code> should be inserted into the string at that position. When you run this program, it will display &#8220;The value of num is: 42&#8221; on the console screen.<\/p>\n\n\n\n<p><code>printf()<\/code> is a versatile and powerful function used for formatted output in C, allowing you to control how data is displayed and formatted.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-to-take-the-input-and-output-of-basic-types-in-c\">How to take the input and output of basic types in C?<\/h2>\n\n\n\n<p> In C, the basic data types like int, float, and char each has specific format specifiers that should be used with functions like <code>scanf()<\/code> and <code>printf()<\/code> for input and output. <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>%d<\/code> is used for integers (e.g., <code>int<\/code>).<\/li>\n\n\n\n<li><code>%f<\/code> is used for floating-point numbers (e.g., <code>float<\/code> or <code>double<\/code>).<\/li>\n\n\n\n<li><code>%c<\/code> is used for characters (e.g., <code>char<\/code>).<\/li>\n\n\n\n<li><code>%s<\/code> is used for strings (arrays of characters).<\/li>\n<\/ul>\n\n\n\n<p>For example, to read an integer using <code>scanf()<\/code>, you would use <code>%d<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int num;\nscanf(\"%d\", &amp;num);<\/code><\/pre>\n\n\n\n<p>To print a floating-point number using <code>printf()<\/code>, you would use <code>%f<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>float price = 19.99;\nprintf(\"The price is: %f\\n\", price);<\/code><\/pre>\n\n\n\n<p>These format specifiers ensure that the data is interpreted and displayed correctly, and they are an essential part of input and output operations in C.<\/p>\n\n\n\n<p><strong>Integer<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Input: scanf(\"%d\", &amp;intVariable);\nOutput: printf(\"%d\", intVariable);<\/code><\/pre>\n\n\n\n<p> <strong>Float <\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Input: scanf(\"%f\", &amp;floatVariable);\nOutput: printf(\"%f\", floatVariable);<\/code><\/pre>\n\n\n\n<p><strong>Character<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Input: scanf(\"%c\", &amp;charVariable);\nOutput: printf(\"%c\", charVariable);<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C program to show input and output\n \n#include &lt;stdio.h&gt;\n \nint main()\n{\n \n    \/\/ Declare the variables\n    int num;\n    char ch;\n    float f;\n \n    \/\/ --- Integer ---\n \n    \/\/ Input the integer\n    printf(\"Enter the integer: \");\n    scanf(\"%d\", &amp;num);\n \n    \/\/ Output the integer\n    printf(\"\\nEntered integer is: %d\", num);\n \n    \/\/ --- Float ---\n   \n    \/\/For input Clearing buffer\n      while((getchar()) != '\\n');\n \n    \/\/ Input the float\n    printf(\"\\n\\nEnter the float: \");\n    scanf(\"%f\", &amp;f);\n \n    \/\/ Output the float\nprintf(\"\\nEntered float is: %f\", f);\n \n    \/\/ --- Character ---\n \n    \/\/ Input the Character\n    printf(\"\\n\\nEnter the Character: \");\n    scanf(\"%c\", &amp;ch);\n \n    \/\/ Output the Character\n    printf(\"\\nEntered character is: %c\", ch);\n \n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Refer to the example given above <\/p>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter the integer: 10\nEntered integer is: 10\n\nEnter the float: 2.5\nEntered float is: 2.500000\n\nEnter the Character: A\nEntered Character is: A<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-to-take-input-and-output-of-advanced-type-in-c\">How to take input and output of advanced type in C?<\/h2>\n\n\n\n<p> In C, strings are typically represented as arrays of characters, and when working with strings, you indeed use the <code>%s<\/code> format specifier for input and output.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Input: scanf(\"%s\", stringVariable);\nOutput: printf(\"%s\", stringVariable);<\/code><\/pre>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C program to show input and output\n  \n#include &lt;stdio.h&gt;\n  \nint main()\n{\n  \n    \/\/ Declare string variable\n    \/\/ as character array\n    char str&#91;50];\n  \n    \/\/ --- String ---\n    \/\/ To read a word\n  \n    \/\/ Input the Word\n    printf(\"Enter the Word: \");\n    scanf(\"%s\\n\", str);\n  \n    \/\/ Output the Word\n    printf(\"\\nEntered Word is: %s\", str);\n  \n    \/\/ --- String ---\n    \/\/ To read a Sentence\n  \n    \/\/ Input the Sentence\n    printf(\"\\n\\nEnter the Sentence: \");\n    scanf(\"%&#91;^\\n]\\ns\", str);\n  \n    \/\/ Output the String\n    printf(\"\\nEntered Sentence is: %s\", str);\n  \n    return 0;\n}<\/code><\/pre>\n\n\n\n<p><strong>Output <\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter the Word:  Skill Vertex\nEntered Word is: Skill Vertex\n\nEnter the Sentence: Skill Vertex\nEntered Sentence is: Skill Vertex<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"faq-basic-input-and-output-in-c\">FAQ- Basic Input And Output In C<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1694692050524\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. What are the inputs and outputs of C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. In C, input involves providing data to a program, and output means displaying or sending data from the program. The <code>stdio.h<\/code> header file contains essential input and output functions, such as <code>scanf()<\/code> for reading data and <code>printf()<\/code> for displaying it. These functions are crucial for handling data in C programs.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1694692062060\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. What is the basic input and output statement?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. An input\/output (IO) statement in a program tells the computer how to handle data. It can involve collecting data from an input device or sending data to an output device.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1694692068846\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. What is output function in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. Output operations in C are used to show data on the user&#8217;s screen, a printer, or in a file. The C programming language offers built-in output functions, including <code>printf()<\/code> and <code>putchar()<\/code>, to accomplish these tasks.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\"><br><\/h2>\n","protected":false},"excerpt":{"rendered":"<p>Basic Input And Output In C Basic Input and Output (I\/O) in the C programming language is fundamental to interacting with the user and the external world. It provides the means to receive data from the user, display information to the user, and read from or write to files. C&#8217;s I\/O functions are concise yet &#8230; <a title=\"Basic Input And Output In C\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/basic-input-and-output-in-c\/\" aria-label=\"More on Basic Input And Output In C\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":5282,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[318],"class_list":["post-1886","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-basic-input-and-output-in-c","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/1886","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=1886"}],"version-history":[{"count":13,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/1886\/revisions"}],"predecessor-version":[{"id":10609,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/1886\/revisions\/10609"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/5282"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=1886"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=1886"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=1886"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}