Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You know that variables in C can be of different types: </p> <ul> <li>int: Integer</li> <li>char: Character</li> <li>float: Floating point number.</li> <li>...</li> </ul> <p>Unlike other languages, variable types cannot be implicitly inferred at compilation time in C. That is why you always declare the type of your variables ( Example: int a or char c).</p> <p>Because scanf is just a function in C, and because functions in C should take parameters of a specific type, people who coded C decided to use the following format:</p> <pre><code> scanf("%d", &amp;var) ; // for integers scanf("%c", &amp;var); //for chars scanf("%f", &amp;var); //for double and floats. </code></pre> <p>using %d or %c does not waste memory or whatsoever. you can think about it as a flag that specifies the type of the input variable.</p> <p>Could the developers of C do it without %d, %c...etc? Yes they could, but then, they have to handle all possible exceptions that might arise from sending the wrong type.</p> <p>Suppose the developers of C used just the following format</p> <pre><code> scanf(&amp;var); </code></pre> <p>That is surly very concise, but then you will have to use the same syntax to send chars/int/double...etc, and then the function scanf has to figure out a way to decide about the type of the variable that was sent. Remember what I told you before? variable types CANNOT be implicitly inferred at compilation time, and thus, this task will be almost impossible.</p> <p>They could however use a different scanf function for every type. For example:</p> <pre><code> scanfInt(&amp;var); //for integers. scanfFloat(&amp;var); //for floats. ... ... </code></pre> <p>That would work perfectly, but it makes less sense to replicate the same code of scanf and use different functions just because the type is different.</p> <p>So what is the solution? ==> Use the same function name ( scanf ), and then add a parameter (%d, %f, %c..) that will be used internally as a flag by C to know the parameter type.</p> <p>I hope now you have a better understanding of the use of %d, %f....</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload