I'm using Windows and CodeBlocks IDE by the way.
If my code was( has space before the %d
specifier):
#include <stdio.h>
int main(void)
{
int num;
printf("Enter a number you want to check is present: ");
while(scanf(" %d",&num)==EOF) // As long as wrong input is present on buffer
{
printf("Please enter valid input...\n");
//clear the input buffer
}
return 0;
}
I've have to type in Ctrl+Z
twice(once on each line,doesn't have to be at the beginning of line) for the while(scanf(" %d",&num)==EOF)
case to run.
Whereas if my code was:
If my code was(no space before the %d
specifier):
#include <stdio.h>
int main(void)
{
int num;
printf("Enter a number you want to check is present: ");
while(scanf("%d",&num)==EOF) // As long as wrong input is present on buffer
{
printf("Please enter valid input...\n");
//clear the input buffer
}
return 0;
}
I've have to type in Ctrl+Z
once for the while(scanf("%d",&num)==EOF)
case to run.
So there are 2 parts in this question:
1)Why do I need to press Ctrl+Z
twice for the first example? I tried to refer to https://stackoverflow.com/a/21261742/10701114 but it was for UNIX and kind of confusing for me
2) Why does the space before the %d
specifier even cause such a discrepancy since it shouldn't matter unless its a %n
,%c
or %[...]
(I got this from https://stackoverflow.com/a/36504282/10701114)?
NOTE: If I should post these 2 parts as 2 questions, please do comment.
Update : I tried this on the cmd prompt with gcc(my first time on a cmd prompt), the same issue persists