%*s

This is a discussion on %*s within the C Programming forums, part of the General Programming Boards category; I saw a source code Code: printf("%*s",indent,""); what does %*s mean? Is it the same as %s?...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    163

    %*s

    I saw a source code
    Code:
    printf("%*s",indent,"");
    what does %*s mean? Is it the same as %s?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    The * will get replaced by whatever value is stored in indent. If indent happens to hold the value of 5 for example at the time this is called, then the code is effectively saying:

    Code:
    printf("%5s","");
    This allows a somewhat dynamic formatting capacity of your output.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    For printf, * is a field width modifier. It takes two arguments: the field width, then the string (for %s). The code you posted will print an empty string the number of spaces to the right equal to indent. In other words, it's a fancy way of printing a bunch of spaces. The equivalent loop would be:
    Code:
    int i;
    for (i = 0; i < indent; i++)
      putchar(' ');
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    163
    er.... i think i roughly know what you meant... thanks! =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting whit MPI
    By isato in forum C Programming
    Replies: 0
    Last Post: 03-03-2009, 09:38 AM
  2. wats the fault...
    By ElemenT.usha in forum C Programming
    Replies: 17
    Last Post: 01-04-2008, 12:52 PM
  3. Reading Characters from file into multi-dimensional array
    By damonbrinkley in forum C Programming
    Replies: 9
    Last Post: 02-24-2005, 12:31 PM
  4. Mazes(urgent!!);
    By Oid21 in forum Game Programming
    Replies: 11
    Last Post: 03-18-2003, 02:29 AM
  5. The A-Bomb of Board Discussion: --
    By Aran in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 10-21-2001, 10:39 AM

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