Q:

Why %n in scanf returning 0 when reading a string from console

I tried to write a program which takes a line from user (console) until a comma, full stop or newline character is encountered and tell how many characters read following the specific pattern.
The string is read correctly but the count is not accurate.
#include <stdio.h>

int main()
{
    int n;
    char s[100];

    // The following line should read a string until (. or , or new-line in encountered) 
    // and %n should tell me the no of characters it read until that point
    scanf("%[^.,\n]s%n", s, &n);

    // The String in okay
    printf("%s\n", s);

    // But count of characters read is 0
    printf("%d\n",n);

    return 0;
}

A:

5 problems:
The "s" in "%[^.,\n]s%n" stops scanning as there can be no 's' read after %[^.,\n].  %[^.,\n] would have read in all 's'.  Thus the later "%n" never occurred and the n that was later printed, a 0, was bad code - printing the uninitialized n - which could have been any int or a trap.
Scan lacks a width limit.  Bad things happen after the 99th character.
With scanf("%[^.,\n]s%n", s, &n);, scanning will saving nothing in s if the first character is a .,\n.  Later printing of s is bad as s[] is uninitialized.
Code fails to check the return value of scanf().
scanf() does not certainly read a line as directed with "write a program which takes a line from user" - it stops short using scanf().
C Std Lib defines a line as below, so code should attempt to read the entire line.

A text stream is an ordered sequence of characters composed into lines, each line consisting of zero or more characters plus a terminating new-line character.

scanf() alternative
int n = 0;
char s[100];
s[0] = '\0';

int cnt == scanf("%99[^.,\n]%n", s, &n);
if (cnt == EOF) puts("Nothing was read");
else printf("%s\n", s);

Better alternative
I'd recommend using fgets() instead of scanf(): takes a line from user.
char s[100];
if (fgets(s, sizeof s, stdin)) {
  size_t n = strcspn(s, ".,\n");   // offset of string that does not have .,\n
  s[n] = '\0';
  printf("%s\n", s);
  printf("%zu\n", n);
}

Note: Lines longer than about 100 need additional code.

