special format specifiers in scanf

How exactly does %[\n] function?
what is role of * in %*c (though it's explanation is already present under scanf,yet it appears vague,so pls explain)
1
2
3
  char s[20],t[40];
  scanf("%s%[^\n]%*c",s,t);
  printf("%s\n%s",s,t);
There's nothing really special about them.
%[\n] (which isn't what you have in the code) reads (and stores in the provided array) as many consecutive newlines as possible

%[^\n] (which you have in the code) reads (and stores in the provided array) as many characters as possible until it encounters (and doesn't process) a newline

%*c reads (and doesn't store anywhere) a single character, which is a newline in this case, because the preceding %[ was until a newline.
According to my observations

If the i/p were: sam likes football
then "sam" is stored in s and " likes football"(including space) is stored in t.

But according to ur explanation u say %*c reads (and doesn't store anywhere) a single character.So,I don't really understand %*c yet.Can u pls explain it again.

If your input was "sam likes football", you pressed Enter at the end of that input, so the data entered were actually "sam likes football\n". %[^\n] took "likes football" and left \n unprocessed, %*c took the \n
I get it now.Thank you cubbi.
Topic archived. No new replies allowed.