Reputation: 41
I wrote the below C code to check if a number is present in an array whose elements are input by the user. But weirdly it's skipping the the third printf
statement, directly taking the input and printing Enter the number you wish to look for
after taking that input. What is causing this? Included input and output box below code.
CODE:
#include <stdio.h>
#include <stdlib.h>
void main() {
int arr[30], size, i, num, flag=0;
printf("Enter size of array. \n");
scanf("%d",&size);
printf("Enter %d array elements one by one. \n",size);
for (i=0; i<size; i++) {
scanf("%d \n",&arr[i]);
}
printf("Enter the number you wish to look for. \n");
scanf("%d",&num);
for(i=0;i<size;i++) {
if (num == arr[i]) {
flag++;
}
}
if (flag>0) {
printf("The number %d is present in the array.",num);
} else {
printf("The number %d is not present in the array.",num);
}
}
INPUT/OUTPUT:
Enter size of array.
5
Enter 5 array elements one by one.
1
2
3
4
5
5
Enter the number you wish to look for.
The number 5 is present in the array.
You can see that Enter the number you wish to look for.
should come before 5
, but it is not so.
Solved
Simply fixed by removing \n
from scanf
.
Upvotes: 4
Views: 24607
Reputation: 28241
The space in the input format string "%d \n"
tells the input system to
consume... all available consecutive whitespace characters from the input
(described here)
So when you enter your last number 5
, the system now tries to consume all whitespace characters. To do that, it waits for additional input, until it's not a whitespace. So, paradoxically or not, to consume spaces, the system has to read a non-space, which is the second 5
you input.
To fix this behavior, you can tell your system to input only a number, without consuming whitespace:
scanf("%d",&arr[i]);
However, this will leave the whitespace in the buffer, which may interfere with later input. To discard the whitespace, you can use various techniques, described e.g. here.
In my opinion, the most correct technique (however, maybe the most cryptic one) is
scanf("%d%*[^\n]%*c",&arr[i]);
%d
- read the number%*[^\n]
- read a string, terminated by a newline byte; discard it and don't store it anywhere%*c
- read a byte (which is a newline byte); discard it and don't store it anywhereBTW in your format string "%d \n"
, there are two whitespaces: a regular space and an end-of-line. They both tell scanf
to consume all whitespaces in input. The effect is exactly the same as with one space "%d "
or with one end-of-line "%d\n"
, so this particular format string may be highly confusing to whoever reads your code (including yourself).
Upvotes: 2
Reputation: 59277
In scanf
, a space character already represents any whitespace. So in your "%d \n"
the function already processes the new line right after the last digit, but then you force it to wait for another newline.
This causes the program to wait for yet another line. After it's input, the program continues and asks for the number to search, and at that point the input was already entered.
Just use only one space in scanf
, it will already work for the newline, ideally before the digit itself so that you don't need one extra line to complete the operation:
scanf(" %d", arr + i);
Upvotes: 2