Listing 7 This program illustrates the use of the %n descriptor.

#include <stdio.h>

#define MAXLINE 80

main()
{
    char buf[MAXLINE+1];
    int nlines;

    for (nlines = 0; gets(buf) != NULL; ++nlines)
    {
        int temp, offset = 0;
        char s[MAXLINE+1];

        printf("%d: ",nlines+1);
        while (sscanf(buf+offset,"%s%n",s,&temp) == 1)
        {
            printf("%s ",s);
            offset += temp; /* Keep track of where we are in line */
        }
        putchar('\n');
    }
    return 0;
}

/*
Executing this program with its own text as input gives this result:

 1: #include <stdio.h>
 2:
 3: #define MAXLINE 80
 4:
 5: main()
 6: {
 7: char buf[MAXLINE+1];
 8: int nlines;
 9:
10: for (nlines = 0; gets(buf); ++nlines)
11: {
12: int temp, offset = 0;
13: char s[MAXLINE+1];
14:
15: printf("%d: ",nlines+1);
16: while (sscanf(buf+offset,"%s%n",s,&temp) == 1)
17: {
18: printf("%s ",s);
19: offset += temp; /* Keep track of where we are in line */
20: }
21: putchar('\n');
22: }
23: return 0;
24: }
*/

/* End of File */