Hello,

Can someone explain me what does this mean?

"40[^\"], %*c"

Thanks

Hello,

Can someone explain me what does this mean?

"40[^\"], %*c"

Thanks

Could we see more of the program, so we can see it in context

Edited 7 Years Ago by gerard4143: n/a

>Can someone explain me what does this mean?
>"40[^\"], %*c"

Assuming it's actually "%40[^\"], %*c", it means read a string of up to 40 characters or until a double quote is found (%40[^\"]), then match a comma, any amount of whitespace, and ignore the next non-whitespace character (%*c).

The %[ specifier is a scanset. It's used to search for a collection of characters and either match based on the scanset or match based on its exclusion (a ^ as the first character means that the scanset is an exclusion scanset).

The * modifier is called assignment suppression. It means that whatever is read will not be assigned to a variable in the argument list. The conversion is simply thrown away.

The format string you posted is actually broken in that it won't get past the scanset because the scanset stops on a double quote character without extracting it. The correct format string would be:

"%40[^\"]\", %*c"

Or to be more strictly correct if there are likely to be more characters in the scanset:

"%40[^\"]%*1[\"], %*c"

This is ignoring the case where the field width ends the scanset specifier rather than one of the scanset character matches.

Edited 7 Years Ago by Narue: n/a

Comments
well, damn. how bout them apples?

>Can someone explain me what does this mean?
>"40[^\"], %*c"

Assuming it's actually "%40[^\"], %*c", it means read a string of up to 40 characters or until a double quote is found (%40[^\"]), then match a comma, any amount of whitespace, and ignore the next non-whitespace character (%*c).

The %[ specifier is a scanset. It's used to search for a collection of characters and either match based on the scanset or match based on its exclusion (a ^ as the first character means that the scanset is an exclusion scanset).

The * modifier is called assignment suppression. It means that whatever is read will not be assigned to a variable in the argument list. The conversion is simply thrown away.

The format string you posted is actually broken in that it won't get past the scanset because the scanset stops on a double quote character without extracting it. The correct format string would be:

"%40[^\"]\", %*c"

Or to be more strictly correct if there are likely to be more characters in the scanset:

"%40[^\"]%*1[\"], %*c"

This is ignoring the case where the field width ends the scanset specifier rather than one of the scanset character matches.

are these hidden any where , because we generally dont come across such usages is there any guide line which shows that.

>are these hidden any where , because we generally dont come
>across such usages is there any guide line which shows that.

I don't think advanced *scanf and *printf format strings are used very often, to be honest. It really is something of an art form, and takes time to master. Sadly, I don't know of any guide for doing it. I learned the usage from the standard definition (section 7.19.6) and personal experience implementing those functions.

>Can someone explain me what does this mean?
>"40[^\"], %*c"

Assuming it's actually "%40[^\"], %*c", it means read a string of up to 40 characters or until a double quote is found (%40[^\"]), then match a comma, any amount of whitespace, and ignore the next non-whitespace character (%*c).

The %[ specifier is a scanset. It's used to search for a collection of characters and either match based on the scanset or match based on its exclusion (a ^ as the first character means that the scanset is an exclusion scanset).

The * modifier is called assignment suppression. It means that whatever is read will not be assigned to a variable in the argument list. The conversion is simply thrown away.

The format string you posted is actually broken in that it won't get past the scanset because the scanset stops on a double quote character without extracting it. The correct format string would be:

"%40[^\"]\", %*c"

Or to be more strictly correct if there are likely to be more characters in the scanset:

"%40[^\"]%*1[\"], %*c"

This is ignoring the case where the field width ends the scanset specifier rather than one of the scanset character matches.

we can also use the - ( minus) before the width

int main()
{
        int num = 10 ;
        printf("%10d\n",num);
        printf("%-2c\f",'G');
        return 0;
}

but i dont find its effect in the out put
i understand that minus indicates left adjustment.

can some one show me how to use - within the printf so that it will have some effect on the out put

Run this program:

#include <stdio.h>
#include <string.h>
#define NEXTCOL 38

char *strs      = "ABCDEF";
char *strl      = "ABCDEFGHIJKL";
int   sizesingle= 9;
int   size[][2]= {   3,  3,   -3,  3,    3,  9,   -3,  9,
                     3, -3,    3, -9,   -3, -3,   -3, -9,
                     3, 15,    3,-15,   -3, 15,   -3,-15,
                     0,  0,
                     9,  3,   -9,  3,   -9,  9,   -9, -3,
                    -9, -9,   -9, 15,   -9, -15,   9,  9,
                     9, -9,    9, -3,    9,  15,   9,-15,
                     0,  0,
                    15,  3,   15,  9,   15, -3,   15, -9,
                    15, 15,   15,-15,  -15,  3,  -15,  9,
                   -15, -9,  -15, 15,  -15,-15,  -15, -3,
                    -1,  0
                  };
char buf[20];
                
int main()
{
    int  i = 0;
    int  j;
    
    j = printf("  Short -- %d chars <%s> ", strlen(strs), strs);
    while (j++ < NEXTCOL)  putchar(' ');
    printf("  Long -- %d chars <%s>  \n", strlen(strl), strl);
    printf("\n");
    
    j = sprintf(buf,"%*s", sizesingle, strs);
    j = printf("       %%%3ds (%2d)  <%s> ", sizesingle, j, buf);
    while (j++ < NEXTCOL)  putchar(' ');
    j = sprintf(buf,"%*s", sizesingle, strl);
    j = printf("       %%%3ds (%2d)  <%s> ", sizesingle, j, buf);
    printf("\n");
    
    j = sprintf(buf,"%*s", -sizesingle, strs);
    j = printf("       %%%3ds (%2d)  <%s> ", -sizesingle, j, buf);
    while (j++ < NEXTCOL)  putchar(' ');
    j = sprintf(buf,"%*s", -sizesingle, strl);
    j = printf("       %%%3ds (%2d)  <%s> ", -sizesingle, j, buf);
    printf("\n");
    
    j = sprintf(buf,"%.*s", sizesingle, strs);
    j = printf("       %%.%2ds (%2d)  <%s> ", sizesingle, j, buf);
    while (j++ < NEXTCOL)  putchar(' ');
    j = sprintf(buf,"%.*s", sizesingle, strl);
    j = printf("       %%.%2ds (%2d)  <%s> ", sizesingle, j, buf);
    printf("\n");
    
    j = sprintf(buf,"%.*s", -sizesingle, strs);
    j = printf("       %%.%2ds (%2d)  <%s> ", -sizesingle, j, buf);
    while (j++ < NEXTCOL)  putchar(' ');
    j = sprintf(buf,"%.*s", -sizesingle, strl);
    j = printf("       %%.%2ds (%2d)  <%s> ", -sizesingle, j, buf);
    printf("\n");
    
    printf("\n");

    while (size[i][0] != -1)
    {
    	if (size[i][0] != 0)
    	{ 
            j = sprintf(buf,"%*.*s", size[i][0], size[i][1], strs);
            j = printf("   %%%3d.%3ds (%2d)  <%s> ", size[i][0], size[i][1], j, buf);
            while (j++ < NEXTCOL)  putchar(' ');
            j = sprintf(buf,"%*.*s", size[i][0], size[i][1], strl);
            j = printf("   %%%3d.%3ds (%2d)  <%s> ", size[i][0], size[i][1], j, buf);
        }
        printf("\n");
        i++;
    }
    printf("done... \n");
    return 0;
}

It should explain. Note that you can write programs like this, too, and find out for yourself.

Comments
Fantastic Answer

Run this program:

#include <stdio.h>
#include <string.h>
#define NEXTCOL 38

char *strs      = "ABCDEF";
char *strl      = "ABCDEFGHIJKL";
int   sizesingle= 9;
int   size[][2]= {   3,  3,   -3,  3,    3,  9,   -3,  9,
                     3, -3,    3, -9,   -3, -3,   -3, -9,
                     3, 15,    3,-15,   -3, 15,   -3,-15,
                     0,  0,
                     9,  3,   -9,  3,   -9,  9,   -9, -3,
                    -9, -9,   -9, 15,   -9, -15,   9,  9,
                     9, -9,    9, -3,    9,  15,   9,-15,
                     0,  0,
                    15,  3,   15,  9,   15, -3,   15, -9,
                    15, 15,   15,-15,  -15,  3,  -15,  9,
                   -15, -9,  -15, 15,  -15,-15,  -15, -3,
                    -1,  0
                  };
char buf[20];
                
int main()
{
    int  i = 0;
    int  j;
    
    j = printf("  Short -- %d chars <%s> ", strlen(strs), strs);
    while (j++ < NEXTCOL)  putchar(' ');
    printf("  Long -- %d chars <%s>  \n", strlen(strl), strl);
    printf("\n");
    
    j = sprintf(buf,"%*s", sizesingle, strs);
    j = printf("       %%%3ds (%2d)  <%s> ", sizesingle, j, buf);
    while (j++ < NEXTCOL)  putchar(' ');
    j = sprintf(buf,"%*s", sizesingle, strl);
    j = printf("       %%%3ds (%2d)  <%s> ", sizesingle, j, buf);
    printf("\n");
    
    j = sprintf(buf,"%*s", -sizesingle, strs);
    j = printf("       %%%3ds (%2d)  <%s> ", -sizesingle, j, buf);
    while (j++ < NEXTCOL)  putchar(' ');
    j = sprintf(buf,"%*s", -sizesingle, strl);
    j = printf("       %%%3ds (%2d)  <%s> ", -sizesingle, j, buf);
    printf("\n");
    
    j = sprintf(buf,"%.*s", sizesingle, strs);
    j = printf("       %%.%2ds (%2d)  <%s> ", sizesingle, j, buf);
    while (j++ < NEXTCOL)  putchar(' ');
    j = sprintf(buf,"%.*s", sizesingle, strl);
    j = printf("       %%.%2ds (%2d)  <%s> ", sizesingle, j, buf);
    printf("\n");
    
    j = sprintf(buf,"%.*s", -sizesingle, strs);
    j = printf("       %%.%2ds (%2d)  <%s> ", -sizesingle, j, buf);
    while (j++ < NEXTCOL)  putchar(' ');
    j = sprintf(buf,"%.*s", -sizesingle, strl);
    j = printf("       %%.%2ds (%2d)  <%s> ", -sizesingle, j, buf);
    printf("\n");
    
    printf("\n");

    while (size[i][0] != -1)
    {
    	if (size[i][0] != 0)
    	{ 
            j = sprintf(buf,"%*.*s", size[i][0], size[i][1], strs);
            j = printf("   %%%3d.%3ds (%2d)  <%s> ", size[i][0], size[i][1], j, buf);
            while (j++ < NEXTCOL)  putchar(' ');
            j = sprintf(buf,"%*.*s", size[i][0], size[i][1], strl);
            j = printf("   %%%3d.%3ds (%2d)  <%s> ", size[i][0], size[i][1], j, buf);
        }
        printf("\n");
        i++;
    }
    printf("done... \n");
    return 0;
}

It should explain. Note that you can write programs like this, too, and find out for yourself.

thank you very much WaltP. :)

This article has been dead for over six months. Start a new discussion instead.