I was just reading the classic K&R and encountered the following syntax:
printf("%.*s",max,s);
What is the meaning of "." here?When I don't apply a "." here,then whole string is printed,but when we don't apply a "." ,atmost max characters are printed.I will be really thankful if anyone could explain this.
In %.*s, the .* limits the number of bytes that will be written. If this were written with a numeral included, such as %.34s, then the numeral would be the limit. When an asterisk is used, the limit is taken from the corresponding argument to printf.
From C 2011 (N1570) 7.21.6.1 4, describing conversion specifications for fprintf et al:
An optional precision that gives … the maximum number of bytes to be written for s conversions. The precision takes the form of a period (.) followed either by an asterisk * (described later) or by an optional decimal integer; if only the period is specified, the precision is taken as zero.
It specifies the "Character String Maximum field width"
The precision within a string format specifies the maximum field width:
%2.6s
specifies a minimum width of 2 and a maximum width of 6 characters. If the string is greater than 6 characters, it will be truncated.
A printf format string allows specifying width and precision.
Width, such as %25s, tells printf to pad the string to the width of 25 characters, inserting spaces before the string. (If the string is wider than 25 characters, it is still printed in its entirety.)
A "precision" applied to a string format, such as %.25s, limits the length of the printed string to 25 characters. A string of 3 characters will be printed fully (with no padding), and a string of 30 characters will be missing its last five characters.
%.*s avoids hardcoding the precision in the format, specifying it instead as an integer argument to printf, in your case max.
First of all, K&R is the original implementation of C, which is different from the current specification. If you want specific information about K&R C, then consult specific documentation.
From the current C standard:
An optional precision that gives (...) the maximum number of bytes to be written for s conversions. The precision takes the form of a period (.) followed either by an asterisk * (described later) or by an optional decimal integer.
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
Similar documentation is available everywhere online for multiple standards or implementations:
glibc
http://www.gnu.org/software/libc/manual/html_node/Output-Conversion-Syntax.html#Output-Conversion-Syntax
POSIX
http://pubs.opengroup.org/onlinepubs/9699919799/functions/fprintf.html
Microsoft C runtime
http://msdn.microsoft.com/en-us/library/56e442dc.aspx
Dot has different meaning with different format strings . If you use a.b with %f , then b gives length . a gives number of decimal points . if you use a.b with %s , a will give minimum length of the string . whereas b gives maximum length of the string that will be printed .
It actually print the character in limited number and commanded format based on the function passed to printf,in this case it should print the specific of "max"
Related
I saw in some functions that in order to convert number to hexadecimal format, using this format: printf("%02hhx", some_char); , but I don't understand why, and what is the meaning of this format ?
Format string "%02hhx" means:
0: Results that would require less than specified field width shall be printed with leading 0s.
2: use at least 2 characters to format the value
x Print in hexadecimal format.
hh The provided parameter for x is only a char, not an int For values 0..127 it doesn't matter if you add the hh but values above could get sign extended and would be printed with lots of leading F.
The result will just be a hexadecimal value with 2 digits.
What is the maximum width value I can put in the %d format specifier? For example:
int c=0;
printf("%1234567899d%n",0,&c);
printf("%d",c);
When I use large values the written value of c is 0. Why is that?
Although it's not stated explicitly, the type of the field width is most likely an int. This is because if the field width is given as * then an int argument is expected.
Section 7.21.6.1p5 of the C standard regarding the fprintf function (any by extension printf) states the following regarding field witdh:
As noted above, a field width, or precision, or both, may be indicated
by an asterisk. In this case, an int argument supplies the
field width or precision. The arguments specifying field
width, or precision, or both, shall appear (in that order)
before the argument (if any) to be converted. A negative
field width argument is taken as a - flag followed by a
positive field width. A neg ative precision argument is
taken as if the precision were omitted.
I tested this on CentOS 7 and Ubuntu 18, and the largest width I could specify was 2147483614 which is 33 less than the max value for a signed 32 bit int. If I use anything larger the first printf prints nothing and c remains 0.
Go generally speaking, the largest value you can expect for c would be INT_MAX, however the exact value will vary based on the implementation.
What is the maximum width value I can put in the %d format specifier?
The limit may be as small as 4095.
Aside from issues with specifying a width outside the positive int range, long output can incur environmental limits.
Environmental limits (fprintf)
The number of characters that can be produced by any single conversion shall be at least 4095. C17dr § 7.21.6.1 15
As I see this, an attempt to "%(some large value)d" print more than 4K (or some greater implementation limit) characters is UB.
Also for output to a text file:
Environmental limits
An implementation shall support text files with lines containing at least 254 characters, including the terminating new-line character. The value of the macro BUFSIZ shall be at least 256. C17dr § 7.21.2 9
Although stdout with printf() is not a file, with re-directed output and freopen(), printf() may be subject to the above constraint.
Then printing an int with many digits can form a line exceeding 254 and some weak file system may not handle long lines.
Such a small 254 limit for a line length I have never seen, so I only include it as a reference, yet do not see it as a modern true limitation.
Let's take the following program:
#include <stdio.h>
int main()
{
long t =57 ;
printf("[%+03ld]", t);
}
and it's output:
[+57]
I am somehow confused: I told him to pad the output to width 3 (03ld), with zeroes, however it seems that if I force the output to put a plus sign before the number (+) it will not add the required zeroes if the length of the number is already 2 digits (as in 57). For numbers <10 it pads with 1 zero.
From http://www.cplusplus.com/reference/cstdio/printf/
(0) -> Left-pads the number with zeroes (0) instead of spaces when padding is specified (see width sub-specifier).
(+) -> Forces to preceed the result with a plus or minus sign (+ or -) even for positive numbers. By default, only negative numbers are preceded with a - sign.
(width) -> Minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is not truncated even if the result is larger.
So I just need a clarification ... The (width) specifier from the quote above refers to the full length of the output string (ie: the characters that will be printed) controlled by this format specifier ("%+03ld") or the full length of the characters of the number that is going to be printed?
Yes, the width specifier refers to the width of the entire formatted result, +57 in your case. This makes it useful for printing columnar text for easy reading on screen (important if you're using C to write an old-school text utility!).
C standard is rather precise that converted value is taken a whole. From C11 §7.21.6/2 (emphasis mine):
The fprintf function writes output to the stream pointed to by stream,
under control of the string pointed to by format that specifies how
subsequent arguments are converted for output.
along with §7.21.6/4:
An optional minimum field width. If the converted value has fewer
characters than the field width, it is padded with spaces (by default)
on the left (or right, if the left adjustment flag, described later,
has been given) to the field width. The field width
takes the form of an asterisk * (described later) or a nonnegative decimal integer.
As you quoted "Minimum number of characters to be printed.", so "+" is just another character for printf. Btw the zeros "0" are just characters aswell and have nothing to do with numbers. It could be any character.
Yes, the field width refers to the complete, converted value including decimal dots, signs etc.
You asked for a 3 characters length format and get 3 characters +57. If you want the 0 to be present, just use printf("[%+04ld]", t); and you'll get +057.
Do the C standards give any excuse for the program below not to produce gigabytes of spaces on its standard output?
#include <stdio.h>
int main()
{
// 2^64 = 18446744073709551616
printf("x%18446744073709551619dx\n", 42);
return 0;
}
Tested on Mac OS X 10.8 and on Linux, I get x 42x.
The C11 standard says (7.21.6.1:4): “An optional minimum field width. If the converted value has fewer characters than the field width, it is padded with spaces (by default) on the left (or right, if the left adjustment flag, described later, has been given) to the field width. The field width takes the form of an asterisk * (described later) or a nonnegative decimal integer.”
The wording in C99 is the same.
When the field width is given as *, then the corresponding argument is obtained as an int (7.12.6.1:5). But it does not seem to be said that the hardcoded field width should fit in an int.
Although it doesn't address the specific question of the field width encoding limit, note that §7.21.6.1/15 states "The number of characters that can be produced by any single conversion shall be at least 4095." So even if an implementation does parse 2^64 + 3 correctly, it's conforming to output 4094 spaces instead of "gigabytes".
Although the C standard does not define a maximum value I guess that every compiler has a maximum value for it.
Here is a similar question:
size limit of printf conversion specification
What are the differences between using scanf with the following format specifiers, when the input being scanned is 123456 versus when it's 123:
%6d
%-6d
%0d
What are the differences in the output?
I ended up just trying it with GCC 4.3.4, and got the following:
%6d: Works fine; only reads 6 characters (so if you try to read 123456789 it will only read 123456)
%-6d: Emits the warning:
warning: unknown conversion type character '-' in format
Doesn't read anything, and the int being written to is unmodified
%0d: Emits the warning:
warning: zero width in scanf format
Reads as though there were no width in the format specifier (%d)
I haven't checked the spec to see if those results are mandated or just how GCC handles it (EDIT: AndreyT found it)
The %-6d is an invalid format specifier. There are no format specifiers with - in them in scanf.
In %6d the 6 part is called maximum field width. It works with all format specifiers (not only with d) and specifies the maximum number of characters to read before any format-specific conversion is performed. For example, if input sequence is 1234567, then %3d will read and convert 123 and %6d will read and convert 123456.
The %0d is an invalid format specifier. Maximum field width in scanf must be a non-zero integer (see 7.19.6.2/3 in the language specification).
So, that leaves us with %6d as the only meaningful format specifier among the three you provided. Under these circumstances the question about differences in output (in results?) makes little sense.
EDIT: One can probably argue that in %-6d the -6 part is the maximum field width, which satisfies the standard requirement of being non-zero decimal integer. However, in C language terminology a decimal integer as a lexical element is a sequence of digits and digits only. It is not allowed to include a sign. I.e. neither -6 nor +6 are decimal integers. Every time you use -6 or +6 as integers in your program it is actually unary - and + operator lexeme followed by decimal integer lexeme. So, since the specification of scanf requires a non-zero decimal integer as maximum field width, it must be a sign-less integer.
Here's what I think will happen: %6d will get you the first 6 digits of the number, %-6d will probably not work as you expect, since - is more of an output alignment specifier. %0d would mean you want only 0 characters, which would probably not work as expected.
Both %-6d and %0d are invalid conversion specifications for scanf, so the behavior for those cases will be undefined. See the language standard, § 7.19.6.2, ¶ 3.
I'll assume, like Arkadiy, that you really meant printf-style formatting, since you refer to "output". I'll also assume that you're using C (as the tag suggests).
If you run:
printf("%6d %-6d %0d", num1, num2);
...you'll end up with compiler errors (or, worse still, runtime bugs), as you haven't supplied enough arguments for three formats.
I've a feeling that that's not what you were asking, though. Let's assume that you actually run:
// (I've added some extra stuff to show how the formatting works.)
printf("'%6d'/'%-6d'/'%0d'", num2, num2, num2);
...you'll get:
' 123'/'123 '/'123'
Normally, if the field width (6 in this case) is wide enough, numbers are right-aligned, space-padded. If you put a '-' before the field width, they will be right-aligned, space-padded.
The subtlety here is in the "%0d" format. You might think you're specifying a zero-width field...and you'd be wrong. The first thing after the '%' is an option flag, and '0' is a valid flag. It means, "If the field width is wider than the content, left-pad with zeroes." In this case, you haven't supplied a field width (the '0' is a flag, remember?), so the '0' flag has no effect: the field will be as wide as the content requires.
There's an even worse subtlety, though. If you specified "%-06d", you'd get right-padding with zeroes, right? Um, no. The '-' flag overrides the '0' flag, whichever order they're supplied. You'd get '123 '.