Thread: formatted output

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    6

    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]);

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
        "%3.3s
    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.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    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".

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  2. execl()/fork() output
    By tadams in forum C Programming
    Replies: 19
    Last Post: 02-04-2009, 03:29 PM
  3. strange virtual function output
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 02-08-2008, 08:08 AM
  4. Trying to store system(command) Output
    By punxworm in forum C++ Programming
    Replies: 5
    Last Post: 04-20-2005, 06:46 PM
  5. To all who read last post formatted output
    By spliff in forum C Programming
    Replies: 8
    Last Post: 08-21-2001, 03:37 AM