{"id":2213,"date":"2024-05-10T07:15:08","date_gmt":"2024-05-10T07:15:08","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=2213"},"modified":"2024-05-10T07:15:08","modified_gmt":"2024-05-10T07:15:08","slug":"scanf-in-c","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/scanf-in-c\/","title":{"rendered":"Scanf 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=\"#scanf-in-c\">Scanf In C<\/a><\/li><li ><a href=\"#scanf-syntax\">Scanf Syntax<\/a><\/li><li ><a href=\"#return-value-of-scanf\">Return Value of scanf<\/a><\/li><li ><a href=\"#why\">Why &amp;?<\/a><\/li><li ><a href=\"#example-of-scanf\">Example Of Scanf<\/a><\/li><li ><a href=\"#faq-scanf-in-c\">FAQ- Scanf In C<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"scanf-in-c\">Scanf In C<\/h2>\n\n\n\n<p>In C programming, <code>scanf<\/code> is a function used to read data from the standard input (typically the keyboard) and store the result in specified variables. It can handle character, string, and numeric input and utilizes format specifiers similar to <code>printf<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"scanf-syntax\">Scanf Syntax<\/h2>\n\n\n\n<p>The syntax of scanf() in C is similar to the syntax of printf()<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int scanf( const char *format, ... );\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Return Type:<\/strong> <code>int<\/code> is the return type, indicating that <code>printf<\/code> returns an integer value, typically denoting the number of characters printed.<\/li>\n\n\n\n<li><strong>Format String:<\/strong> &#8220;format&#8221; is a string that contains format specifiers, allowing you to control the formatting of the output.<\/li>\n\n\n\n<li><strong>&#8220;&#8230;&#8221; (ellipsis):<\/strong> The ellipsis (&#8230;) indicates that <code>printf<\/code> can accept a variable number of arguments, corresponding to the format specifiers in the format string. This flexibility allows you to print multiple values of different types in a single <code>printf<\/code> statement<\/li>\n<\/ul>\n\n\n\n<p><strong>Example format specifiers recognized by scanf:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>%d to accept input of integers.\n\n%ld to  accept input of long integers\n\n%lld to accept input of long long integers\n\n%f to accept input of real number.\n\n%c to accept input of character types.\n\n%s to accept input of a string.<\/code><\/pre>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int var;\nscanf(\u201c%d\u201d, &amp;var);<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"return-value-of-scanf\">Return Value of scanf<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Greater than 0:<\/strong> This indicates that the number of values converted and assigned successfully. It means that <code>scanf<\/code> successfully read and assigned data according to the provided format specifiers.<\/li>\n\n\n\n<li><strong>Equal to 0:<\/strong> This means that no value was assigned. It typically occurs when <code>scanf<\/code> couldn&#8217;t match the input with the specified format, or when no input was provided.<\/li>\n\n\n\n<li><strong>Less than 0:<\/strong> This indicates a read error encountered or that the end-of-file (EOF) was reached before any assignment was made. It suggests a problem with reading input, such as reaching the end of a file or encountering an error while reading.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"why\">Why &amp;?<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The ampersand (<code>&amp;<\/code>) operator is often referred to as the &#8220;address of&#8221; operator.<\/li>\n\n\n\n<li>It is used to obtain the memory address of a variable.<\/li>\n\n\n\n<li>For example, <code>&amp;var<\/code> represents the address of the variable <code>var<\/code>.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-of-scanf\">Example Of Scanf<\/h2>\n\n\n\n<p>C Program to implement Scanf<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C program to implement\n\/\/ scanf\n#include &lt;stdio.h&gt;\n \n\/\/ Driver code\nint main()\n{\n    int a, b;\n   \n      printf(\"Enter first number: \");\n      scanf(\"%d\", &amp;a);\n   \n      printf(\"Enter second number: \");\n      scanf(\"%d\", &amp;b);\n   \n      printf(\"A : %d \\t B : %d\" ,\n            a , b);\n   \n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter first number: 5\nEnter second number: 6\nA : 5      B : 6<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"faq-scanf-in-c\">FAQ- Scanf 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-1695896624666\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. What does scanf do in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. <code>scanf<\/code> stands for &#8220;Scan Formatted String.&#8221;<br \/>It is used to read data from the standard input (usually the keyboard) and stores the result into specified variables.<br \/><code>scanf<\/code> can accept character, string, and numeric data from the user via the standard input stream.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1695896635988\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. Why should we use scanf?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. <code>scanf<\/code> is used to receive input from the standard input, often the keyboard. While it offers versatility, it can be unreliable when it comes to handling human input errors.<br \/>For straightforward programs, <code>scanf<\/code> is a convenient and straightforward method for receiving input, as demonstrated in the example <code>scanf(\"%d\", &amp;b);<\/code>, which reads an integer value from the standard input and stores it in the variable <code>b<\/code>.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1695896647080\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. What is the difference between scanf %d and scanf %i?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. Both <code>%d<\/code> and <code>%i<\/code> are used to read signed integers.<br \/>However, <code>%i<\/code> has additional functionality. It interprets the input differently based on certain prefixes:If the input is preceded by &#8220;0x&#8221; (e.g., &#8220;0x1A&#8221;), <code>%i<\/code> interprets it as a hexadecimal number.<br \/>If the input is preceded by &#8220;0&#8221; (e.g., &#8220;012&#8221;), <code>%i<\/code> interprets it as an octal number.<br \/>Otherwise, <code>%i<\/code> interprets the input as a decimal number.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Scanf In C In C programming, scanf is a function used to read data from the standard input (typically the keyboard) and store the result in specified variables. It can handle character, string, and numeric input and utilizes format specifiers similar to printf. Scanf Syntax The syntax of scanf() in C is similar to the &#8230; <a title=\"Scanf In C\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/scanf-in-c\/\" aria-label=\"More on Scanf In C\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":5330,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[392],"class_list":["post-2213","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-scanf-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\/2213","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=2213"}],"version-history":[{"count":10,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2213\/revisions"}],"predecessor-version":[{"id":10638,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2213\/revisions\/10638"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/5330"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=2213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=2213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=2213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}