Hi,
What is the significance of the following format specifiers:
%10s
%.10s
%-10s
%.15s
%-15s
%15.10s
%-15.10s
Thanks.
Hi,
What is the significance of the following format specifiers:
%10s
%.10s
%-10s
%.15s
%-15s
%15.10s
%-15.10s
Thanks.
>What is the significance of the following format specifiers:
Relatively low, but they come in handy occasionally.
>What is the significance of the following format specifiers:
Relatively low, but they come in handy occasionally.
:twisted: :twisted:
%10s - Output a minimum of 10 chars, leading spaces
%.10s - Output up to 10 characters
%-10s - Output a minimum of 10 chars, trailing spaces
%.15s - see above
%-15s - see above
%15.10s - output up to 10 chars with 5 leading spaces
%-15.10s - output up to 10 chars with 5 trailing spaces
You could have tested it out yourself, you know. Just write a program that tests them:
#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;
}
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.