%s, %20s,%-20s

This is a discussion on %s, %20s,%-20s within the C Programming forums, part of the General Programming Boards category; Hi, what is the %2, %20 & %-20 mean in the C program? how can I search the meaning in ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    16

    %s, %20s,%-20s

    Hi,

    what is the %2, %20 & %-20 mean in the C program?
    how can I search the meaning in Borland C?

    thk.


    Code:
    #include <stdio.h>
    
    void main() {
      char string[9]="Hi there";
    
    
      printf("as is   : %s:\n",string);
      printf("format 1: %2s:\n",string);
      printf("format 2: %20s:\n",string);
      printf("format 3: %-20s:\n",string);
    
      printf("You may press Enter to bye\n");
      fflush(stdin);
      getchar();
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    > void main()
    > fflush(stdin);
    Read the FAQ - these are wrong

    > what is the %2, %20 & %-20 mean in the C program?
    Did you run the program to find out?
    If you had done, perhaps you would have seen differently formalled lines

    > how can I search the meaning in Borland C?
    Most people call them either help pages or manual pages.

    > char string[9]="Hi there";
    Save yourself some counting and just use
    char string[]="Hi there";
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    To see the effecf of various format specifiers on printf, run this program:
    Code:
    #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(void)
    {
        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;
    }
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21