I am new to c coding, my program is running well when use int
data type, but when I use char
it starts skipping line.
#include <stdio.h>
int main() {
int i;
char k[10];
for (i = 0; i < 10; i ) {
printf("enter your character for line %d: \n", i);
scanf("%c", &k[i]);
}
for (i = 0; i < 10; i ) {
printf("your character for line %d are %c \n", i, k[i]);
}
return 0;
}
CodePudding user response:
Well, when you press Enter you are writting 'new line' character to program's input.
And because it's character, %c
is reading it.
Specifier %c
doesn't consume spaces like other specifiers, so you have to explicitly tell scanf
to skip whitespaces, by adding space before %c
, so you will have " %c"
as format.
More details might be here:
https://stackoverflow.com/a/5240807/2416941
EDIT: Be aware that this way you can't read space or tabs from user's input.