Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 506 Vote(s) - 3.52 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What does "%.*s" mean in printf?

#1
I got a code snippet in which there is a

printf("%.*s\n")

what does the `%.*s` mean?
Reply

#2
I don't think the code above is correct but (according to this description of [`printf()`](

[To see links please register here]

)) the `.*` means


> 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 it's a string with a passable width as an argument.
Reply

#3
See:

[To see links please register here]


> `.*` The precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.
>
> `s` String of characters
Reply

#4
You can use an asterisk (`*`) to pass the width specifier/precision to [`printf()`](

[To see links please register here]

), rather than hard coding it into the format string, i.e.

void f(const char *str, int str_len)
{
printf("%.*s\n", str_len, str);
}
Reply

#5
More detailed [here](

[To see links please register here]

).


> integer value or `*` that specifies minimum field width. The result is padded with space characters (by default), if required, on the left when right-justified, or on the right if left-justified. In the case when * is used, the width is specified by an additional argument of type int. If the value of the argument is negative, it results with the - flag specified and positive field width. (Note: This is the minimum width: The value is never truncated.)


> `.` followed by integer number or *, or neither that specifies precision
> of the conversion. In the case when * is used, the precision is
> specified by an additional argument of type int. If the value of this
> argument is negative, it is ignored. If neither a number nor * is
> used, the precision is taken as zero. See the table below for exact
> effects of precision.

So if we try both conversion specification

#include <stdio.h>

int main() {
int precision = 8;
int biggerPrecision = 16;
const char *greetings = "Hello world";

printf("|%.8s|\n", greetings);
printf("|%.*s|\n", precision , greetings);
printf("|%16s|\n", greetings);
printf("|%*s|\n", biggerPrecision , greetings);

return 0;
}

we get the output:

|Hello wo|
|Hello wo|
| Hello world|
| Hello world|


Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through