In the below program when am reading input from keyboard its taking only 2 characters instead of 4 and remaining 2 characters its taking spaces by default.
why is it???
program to take char input through pointers/
int c,inc,arrysize;
char *revstring;
printf("enter the size of char arry:");
scanf("%d",&arrysize);
revstring = (char *)malloc(arrysize * sizeof(*revstring));
printf("%d",sizeof(revstring));
printf("enter the array elements:");
for(inc=0;inc<arrysize;inc++)
{
scanf("%c",&revstring[inc]);
}
for(inc =0;inc<arrysize;inc++)
printf("%c",revstring[inc]);
getch();
return 0;
}
scanfreads formatted inputs. When you tape a number, you tape the digits, and then, you press<Enter>. So there is a remaining\ninstdin, which is read in the nextscanf. The same applies if you press<Enter>between the characters.A solution is to consume the characters in the standard input stream after each input, as follow:
Another idea is to use
fgetsto get human inputs.scanfis not suitable for such readings.