Check The Programming Section
The width option is the option you will use most frequently. This option is often called a field width. We usually use field widths to line up columns of data to form tables on the screen. Printing things in tables makes output more readable. We can also use field widths for other reasons as well. Since they allow us to control exactly where a value will appear on a line of the screen, we can also use field widths when we want to "draw" with characters. The value that the format specifier indicates is sometimes called a "field" so the field width controls how many columns the field will occupy on the screen. Most often the width is an integer. For example,
#include<stdio.h>
int main(){
char ch = '2';
printf("%5c\n",ch);
}
The printf() will displayed the character 2 at 5th column of the output line and the output cursor moves to the next line.
~~~~2
|
Notice that four blank spaces precede the digit '2'. This means the entire field (four blanks and one non-blank character) occupies five columns. In addition, notice that the blanks come before the digit. We call this right justification. Right justification implies that items on a single row of the screen will line up along the right margin.
The specified field width (w = 5, for this example) is the minimum number of columns that output will occupy. If a complete representation of a value requires more columns, printf will print the whole value, overriding the field width. For example, consider the following call to printf:
#include<stdio.h>
int main(){
int val = 12345;
printf("%2d\n",val);
}
In this case, the representation the value of val requires 5 columns, which is less than the value of the width = 2. So in this case, display will be left justified.
12345
|
The decimal point in the floating-point also requires a column in the output line. For example,
#include<stdio.h>
int main(){
printf("%f\n",34.56);
}
The value 34.56 requires nine columns. Because %f is the format specifier is used to print a value of type single-floating point and prints upto 6th decimal place. So 34.56 will displayed in the output line as 34.560000 and decimal point also requires one column as:
34.560000
If width specifier is used with %f then we need to set the width value by counting upto 6th decimal place. For example,
printf("%9f\n",4.5);
printf("%12f\n",34.56);
The output of the above printf() statements will be
~4.500000
~~~34.560000
|
You may be wondering at this point how justification allows us to print tables. So far, we have only put one value on each line, but if we print several values before each newline ('\n'), we can use field widths to force them to line up. Consider the following series of printf statements:
#include<stdio.h>
int main(){
printf ("%10s%10s\n", "month", "day");
printf ("%10d%10d\n", 1, 10);
printf ("%10d%10d\n", 12, 2);
}
For all the values inside printf() the value of the width = 10 and outputs will be right justified as:
~~~~~month~~~~~~~day
~~~~~~~~~1~~~~~~~~10
~~~~~~~~12~~~~~~~~~2
|
Notice in particular that the field widths always start from the last column printed. At the beginning, the cursor is in the extreme upper left corner of the screen. The format string in the first printf statement specifies that the function should print the string "month" using 10 columns. Since "month" only requires 5 columns, printf precedes the string with five blanks. At this point, the cursor will be in the eleventh column. The next format specifier requires printf to write the string "day", also using 10 columns. The string "day" takes up three columns, so printf must precede it with seven blanks, starting from the current cursor position. In other words, the column count begins just after the last letter of the string "month".
Occasionally, when we write a program, we cannot even predict how large a field width we will need when a program executes. This implies that the field width itself needs to be a variable, for which the program will compute a value. Although it is rare for this situation to arise, it is worth mentioning how you can accomplish this. Just as was the case when we wanted to print the value of a variable, if we try to use a variable's name as a field width specifier, printf will simply print the name to the screen. For example, assume that you have declared an integer variable named width and have somehow computed a value for it. For example,
#include<stdio.h>
int main(){
printf("%widthd\n",10)
}
will print
10dthd
|
To solve this problem, C uses an asterisk (*) in the position of the field width specifier to indicate to printf that it will find the variable that contains the value of the field width as an additional parameter. For instance, assume that the current value of width is 5. The statement:
#include<stdio.h>
int main(){
int width = 5;
printf ("%*d%*d\n", width, 10, width, 12);
}
will print
~~~10~~~12
|
Notice that the order of the additional parameters (width) is exactly the same as the order of the specifiers in the format string, and that even though we use the same value (width) twice as a field width, it must appear twice in the parameter list. In the printf() function, the formatted string "%*d%*d", have two asterics (*) and represents the value of the parameter width.