Working of scanf under for loop
NickName:imox Ask DateTime:2014-09-23T10:12:28

Working of scanf under for loop

I have a question about working of for loop in C. Please have a look at the following code:

#include<stdio.h>

void main()
{
    int ar[10],i;
    printf("Enter 10 numbers:");
    for(i=0;i<10;i++)
        scanf("%d",&ar[i]);
    for(i=0;i<10;i++)
        printf("%d",ar[i]);
}

When I execute this and give the following input:

1 2 3 4 5 6 7 8 9 10 11 12

I have given 12 inputs but the loop was supposed to run for only 10 times (scanf loop). I can give even more inputs and it is happy to take it unless I hit enter key. Is there something about for loop that I'm missing here?

Copyright Notice:Content Author:「imox」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/25985824/working-of-scanf-under-for-loop

More about “Working of scanf under for loop” related questions

Working of scanf under for loop

I have a question about working of for loop in C. Please have a look at the following code: #include&lt;stdio.h&gt; void main() { int ar[10],i; printf("Enter 10 numbers:"); for(i=0;i&...

Show Detail

scanf() not working correctly within for loop

I am attempting to store multiple strings from stdin into a temporary variable. Each string will be entered following a new line character. This is my attempt: #include &lt;stdio.h&gt; int main() {...

Show Detail

Problems using char and scanf and the for loop?

My problem is using char and scanf. Below you can find my code that is almost working right. The problem is concerning the for loop. You see, I would like to ask 10 characters from the user, and co...

Show Detail

Scanf after for loop

I don't understand why if I insert a letter the program executes the printf's after the for loop without letting me insert a value for the scanf after the for loop. int main() { float

Show Detail

C: loop on scanf

char buf[1024] = {0}; // send a message if(status == 0) { while(1) { printf("Enter message : "); scanf("%1023[^\n]", buf); fflush(stdin); ...

Show Detail

For loop skipping scanf()

I'm attempting to write a small program where a user specifies they wish to enter n many nunmbers, then they are prompted to enter n many floats in the subsequent lines with a prompt. The expected

Show Detail

scanf() method does not working in while loop?

I've been out of this loop for 5 hours. My scanf method does not work. Here is my loop. I could not execute library's strcmp so I wrote myself. int string_compare(char str1[], char str2[])//Compare

Show Detail

Scanf stops working abruptly

I'm trying to read data from an stl file, one character at a time using scanf/fscanf. I call scanf many times in my program, but input all the data at the first instance. It works perfectly for a w...

Show Detail

Issues with using scanf inside a while loop

I’m brand new to programming. I ‘m working on a homework assignment in order to help us understand scanf and arrays. The program is supposed to ask the user to input an unknown set of numbers. ...

Show Detail

Scanf is causing infinite loop

I have a C program for Guess the number game but when I run this code it goes into infinite loop, I think it's because of scanf() in the 11th line! How can I fix this? #include &lt;stdlib.h&gt; #in...

Show Detail