int ch;
do
{
ch = getchar();
printf("ch = %d\n",ch);
}while(ch != '\n' && ch != EOF);
In this case the output for input 1234 is
ch = 49
ch = 50
ch = 51
ch = 52
ch = 10
But on giving it like this
char chs[100] = {0};
unsigned int u = 0;
int sign = 1,k=0;
if(scanf("%[- 0-9]",&chs[0])== 1)
{
int ch;
do
{
ch = getchar();
printf("ch = %d\n",ch);
}while(ch != '\n' && ch != EOF);
In this case output for input = 1234 is
ch =10
Why is this happening here ???
Can someone please explain … Why isn’t the output same as before ??
How scanf is exactly working here ?
The call to
scanfreads data from the stream. When you later try to read from the stream again by usinggetcharthe characters that were read byscanfhave already been consumed and are not read again.Also, see the documentation for
scanffor valid values of the format string.