Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>\n</code> in <code>scanf</code> was the problem </p> <pre><code>#include&lt;stdio.h&gt; int main() { int marks[3]; int i; for(i=0;i&lt;3;i++) { printf("Enter a no\n"); scanf("%d",(marks+i)); } printf("\nEntered values:\n"); for(i=0;i&lt;3;i++) { printf("%d\n",*(marks+i)); } return 0; } </code></pre> <p><strong>Reason:</strong> </p> <blockquote> <p>I expect only <code>3</code> values is stored in an array but it stores 4 values and in next 'for' loop as expected show 3 values. My Question is why in 1st 'for' loop it takes 4 values instead of 3?</p> </blockquote> <p><strong>First:</strong> No, it only stores <code>3</code> number but not <code>4</code> numbers in array <code>marks[]</code>. </p> <p><strong>Second:</strong> interesting to understand loop runs only for three times for <code>i = 0</code> to <code>i &lt; 3</code>. The for loop runs according to condition. More interesting code is stuck in <code>scanf()</code> as described below: </p> <p>Your confusion is why you have to enter four numbers, its not because you loop runs <code>4</code> times but its because <code>scanf()</code> function returns only when you enter a non-space char (and after some <kbd>enter</kbd> press you inputs a number symbol that is non-space char). </p> <p>To understand this behavior read manual: <a href="http://man7.org/linux/man-pages/man3/scanf.3.html" rel="nofollow"><code>int scanf(const char *format, ...);</code></a>: </p> <blockquote> <p>A sequence of white-space characters (space, tab, newline, etc.; see <code>isspace(3)</code>). This directive <strong>matches any amount of white space, including none, in the input</strong>. </p> </blockquote> <p>Because in first for loop's, in <code>scanf()</code> you have included <code>\n</code> in format string, so <code>scanf()</code> returns only if press a number <kbd>enter</kbd> (or a non-space <kbd>key</kbd>). </p> <pre><code> scanf("%d\n",(marks+i)); ^ | new line char </code></pre> <p>What happens? </p> <p>Suppose input to program is: </p> <pre><code>23 &lt;--- because of %d 23 stored in marks[0] as i = 0 &lt;enter&gt; &lt;--- scanf consumes \n, still in first loop 543 &lt;--- scanf returns, and leave 542 unread, then in next iteration 543 read by scanf in next iteration &lt;enter&gt; 193 &lt;enter&gt; &lt;--- scanf consumes \n, still in 3rd loop &lt;enter&gt; &lt;--- scanf consumes \n, still in 3rd loop 123 &lt;--- remain unread in input stream </code></pre>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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