-
formatted output
hey, could u tell me what exactly this line means?
I need to print on the console telephone #s. How to understand the 3.3 4.4?Is there a better function that I can use? Can this be achieved with printf?
cprintf("%3.3s-%2.2s-%4.4s", student->SSN, &student->SSN[3], &student->SSN[5]);
-
The .3 has no effect here because it's used with "s" which is a string. The ".3" is supposed to be used with floating point (or double) values, not integers, strings, or characters.
Quzah.
-
Quzah, you're wrong, you can use this for strings too, look:
Code:
#include <stdio.h>
#include <stdlib.h>
int main( void )
{
char *test = "What's the result?";
printf( "%2.4s\n", test );
return 0;
}
This have effect, and it'll print "What".
-
Years ago I created the following program to display all the different permutations of string formats for a printf statement.
Code:
#include <stdio.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];
main(int argc, char *argv[])
{
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");
}
The output specifies a short string of 6 characters and a long one of 12 and how output is affected by X and Y values in a printf("%X.Ys"...) format designator.
Walt