What does the %*s format specifier mean?

CPrintfFormat Specifiers

C Problem Overview

In some code that I have to maintain, I have seen a format specifier %*s . Can anybody tell me what this is and why it is used?

An example of its usage is like:

fprintf(outFile, "\n%*s", indent, "");

C Solutions

Solution 1 - C

[It's used to specify, in a dynamic way, what the width of the field is][1]:

> * The width is not specified in the format string, but as an additional > integer value argument preceding the > argument that has to be formatted.

so "indent" specifies how much space to allocate for the string that follows it in the parameter list.


printf("%*s", 5, "");

is the same as

printf("%5s", "");

It's a nice way to put some spaces in your file, avoiding a loop. [1]: http://www.cplusplus.com/reference/clibrary/cstdio/printf/

Solution 2 - C

Don't use "%*s" on a buffer which is not NULL terminated (packed) thinking that it will print only "length" field.

Solution 3 - C

The format specifier %4s outputs a String in a field width of 4—that is, printf displays the value with at least 4 character positions.

If the value to be output is less than 4 character positions wide, the value is right justified in the field by default.

If the value is greater than 4 character positions wide, the field width expands to accommodate the appropriate number of characters.

To left justify the value, use a negative integer to specify the field width.

References: Java™ How To Program (Early Objects), Tenth Edition

Solution 4 - C

When used in printf and fprintf:

printf("%*s", 4, myValue); is equivalent to printf("%4s", myValue);

It displays the variable with minimum width, rest right-justified spaces. To left-justify the value, use a negative integer.

When used in scanf and sscanf:

/* sscanf example */
#include <stdio.h>

int main ()
  char sentence []="Rudolph is 12 years old";
  char str [20];
  int i;

  sscanf (sentence,"%s %*s %d",str,&i);
  printf ("%s -> %d\n",str,i);
  return 0;


Rudolph -> 12

It is used to ignore a string.

Solution 5 - C

* Causes fprintf to pad the output until it is n characters wide, where n is an integer value stored in the a function argument just preceding that represented by the modified type.

printf("%*d", 5, 10) //will result in "10" being printed with a width of 5.

Solution 6 - C


> The width is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.

e.g: printf("%*s", 4, myValue); is equivelant to printf("%4s", myValue);.


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAamirView Question on Stackoverflow
Solution 1 - CakappaView Answer on Stackoverflow
Solution 2 - Cuser2132064View Answer on Stackoverflow
Solution 3 - CBasheer AL-MOMANIView Answer on Stackoverflow
Solution 4 - CzeitgeistView Answer on Stackoverflow
Solution 5 - CjitterView Answer on Stackoverflow
Solution 6 - CpauldooView Answer on Stackoverflow