The question is :
How can variable field width be implemented using
printf()
? That is, instead of%8d
, the width should be specified at run time.
I came across some C code on the Internet based on the question above but as I am new to C programming I haven't been able to make heads or tails of the code. I am posting the code below:
#include <stdio.h>
int main()
{
const char text[] = "Hello world";
int i;
for ( i = 1; i < 12; ++i )
{
printf("\"%.*s\"\n", i, text);
}
return 0;
}
The field width can also be specified as asterisk (*) in which case an additional argument of type int is accessed to determine the field width. For example, to print an integer x in a field width determined by the value of the int variable w, you would write the D statement: printf("%*d", w, x);
Answer: All field widths are variable with scanf(). You can specify a maximum field width for a given field by placing an integer value between the % and the field type specifier.
Field width as the name suggests is the width or space which needs to be given for that particular display line and it is specified using numbers. For example, In the following C code,observe the outputs on the screen. We can see how useful field width specifiers are for displaying formatted output on the screen.
As for using printf and scanf as variable names, you can only do that if you don't already have them declared as functions - In other words, you can't do it if you've already included stdio. h .
First of all, let me tell you, the code you have shown is about controlling the precision, not the field width. For a shortened form**
%A.B<format specifier>
A
denotes the field width and B
makes the precision.
Now, quoting the C11
standard, chapter §7.21.6.1, fprintf()
(emphasis mine)
Each conversion specification is introduced by the character %. After the %, the following appear in sequence:
[..]
- An optional precision that gives the minimum number of digits to appear for the
d
,i
,o
,u
,x
, andX
conversions, the number of digits to appear after the decimal-point character fora
,A
,e
,E
,f
, andF
conversions, the maximum number of significant digits for theg
andG
conversions, or the maximum number of bytes to be written fors
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. If a precision appears with any other conversion specifier, the behavior is undefined.
and
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. [...]
So, in your case,
printf("\"%.*s\"\n", i, text);
the precision will be supplied by i
which can hold different values at run-time.
The complete format (broken down in separate lines for ease of readability)
%
<Zero or more flags>
<optional minimum field width>
<optional precision>
<optional length modifier>
<A conversion specifier character>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With