This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
what does “%.*s” mean in printf in c
I have found the following line :
asprintf(&c, "%s%.*s", *msg_in, size * rxed, buffer)
And I want to know the meaning of %.*s
The %.*s format means "print a string using a field width of n characters, where n is read from the next argument".
So here, it prints buffer with a width of size * rxed characters. (padding with spaces if necessary)
I would highly recommend reading the manual...
.* in a format string means:
the precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.
Details can be seen here.
So you didn't give any details, but if the result of: size * rxed was 5, then you could do this:
asprintf(&c, "%s%.*s", *msg_in, size * rxed, buffer)
or
asprintf(&c, "%s%5s", *msg_in, buffer)
to the same effect.
Related
This question already has answers here:
Number of character cells used by string
(6 answers)
Closed 5 years ago.
i have a problem with printf width modifier in C
example:
char a[] = "o", b[] = "l";
printf("%-3s %s", a, b);
console output gives me 3 spaces between strings, but when I change "o" to "ó", console shows 2 spaces between. Every time i use character like "ł", "ó", "ś" in the string, width modifier is shortened by 1 sign, why this happens?
OS X 10.11 (El Captain)
"Special" characters as you are showing them need more bytes (char) to be represented in a string. Your arbitrary limit of 3 is not enough, raise it to some suitable value.
In addition, the way and length that such special characters are represented depends on the system. For portable code you should never make such arbitrary assumptions.
This question already has answers here:
Set variable text column width in printf
(2 answers)
Closed 6 years ago.
In C, as I understand so far, this would be correct:
printf("%10s\n", "This is C");
would return:
" This is C!"
(with the intentional space prior to the string; no quotations).
My question is can you replace the 10 specifying the length of the print using a variable? If so, how?
That's how:
printf("%*s\n", 10, "This is C");
The format changed from %10s to %*s. The printf() now would expect among the argument, before the string, an int with the width to pad the string to (the 10 in the example above; obviously could be a variable too).
To tell the printf() to pad the output to the left (instead of the default right) use the -: %-*s. (The output would change from " This is C" to "This is C ".)
To tell the printf() to take only few first bytes from the string, or if the string is not null-terminated, you can add .* to the format at the same place as the precision for floating point types. The printf() would print up to that number of characters, stopping at the first null character. Example:
int width = 10;
int chars = 4;
printf( "%-*.*s", width, chars, "This is C" );
would produce output "This ".
This question already has answers here:
Is it possible to print out only a certain section of a C-string, without making a separate substring?
(3 answers)
Closed 7 years ago.
I want to do the following, taking into account a string as how are you I want to print from a until e ie the word are. I tried this
char str[] = "how are you";
printf("%*.*s\n", 5, 8, str);
Output
how are
Expected output
are
Someone has idea how this can be done?
The characters in the string are indexed from 0, so the a is at index 4. By passing &str[4] to printf, the printf will start at the a, and print as many characters as specified by the precision.
printf("%.*s\n", 3, &str[4]);
You are specifying field width and number of chars to print. However, the string always starts from the pointer as passed, there is no "start offset" specifier, as that would be unnecessary. See here for the format specifiers.
For your code, that would be printf("%.*s", width, str + start)
Note that &str[start] is identical.
try using this code
printf("%.*s\n", 3, &str[4]);
This question already has answers here:
What is the use of the %n format specifier in C?
(12 answers)
Closed 7 years ago.
I just encountered this by looking in the standard:
7.19.6.1 The fprintf function
in
8 The conversion specifiers and their meanings are:
regarding to:
n The argument shall be a pointer to signed integer into which is written the
number of characters written to the output stream so far by this call to
fprintf. No argument is converted, but one is consumed. If the conversion
specification includes any flags, a field width, or a precision, the behavior is
undefined.
What does this mean? What does %n do?
Did I get it correct, that acording to:
Returns
14 The fprintf function returns the number of characters transmitted
In this snippet:
int a, b;
b = printf ("Thi%n\s is just a test",&a);
a would equal to b?
the number of characters written to the output stream so far
"So far", means wherever you place your %n, the result will change. As of your example, it will be 3.
If your increase your %s position by one char, the resulting variable pointed will increase by one. Placing your %s at the very end of the string will make it equal to the value returned by printf
a = 3 and b = 19 for your case
a will be equal to number of character printed before %n.
Suppose you try to print printf ("This%sis%n just a test","coder", &a);
Then the value of a will be this + coder + is = 11.
And the value of b is always the total number of characters printed
This question already has answers here:
What does "%.*s" mean as a format specifier in printf?
(7 answers)
Closed 9 years ago.
This might be a very basic question to many of you, but I am not able to understand
what %.*s doing?
void substring(int i, int j, char *ch)
{
printf("The substring is: %.*s\n", j - i, &ch[i]);
//what is %.*s doing?
}
The * is taking the length limit for the string from the argument before the string. So the printf will output (at most) j - i characters from &ch[i] to stdout. If the string is shorter, then the entire string will be printed, but it will not be blank padded.
Here is a good reference for printf: http://en.cppreference.com/w/c/io/fprintf.*
And this is what it says:
. followed by integer number or * 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. See the table below for exact effects of precision.
And for s, it says:
Precision specifies the maximum number of bytes to be written.
So in your case, it prints a maximum of j-i characters.
* In fact, it's a very good reference for just about all standard C and C++ libraries. Use it!