- Printf function prints data from the computers memory to the standard output device.
Argument->It can be constants,single variable, array names (or) complex expressions
- printf allow us to include labels and message with the output data. The extra labels in the double quotes in the printf will be displayed in the screen.
- The f-type conversion and the e-type conversions are both used to output floating point values. In e-type conversion an exponent to be included in the output.
100.000000 25.2500001.000000e+02 2.525000e+01
- In the printf, s-type conversion is used to output a string that is terminatedd by the null character(\0). Whitespace characters may be included with in the string.
Example: Reading and writing a line of text.
- We can restrict the number of characters or numbers displayed in the screen by specifying a fieldwidth. A minimum field width can be specified by preceding the conversion character by an unsigned integer.
- If the number of characters in the corresponding data item is less than the specified field width then the data item will be preceded by enough leading blanks to fill the specified field.
- If the number of characters in the data item exceeds the specified field width then additional space will be allocated to the data item so that the entire data item will be displayed.
12345 12345 12345
123.456000 123.456000 123.456000
1.234560e+02 1.234560e+02 1.234560e+02
x has 5 digits
%3d->no of digits(5) < fieldwidth(3) so additional space added and all the digits are displayed.
%7d-> no of digits(5) > fieldwidth(7)=>
- g-type conversion:
In g-type conversion no extra trailing zeros are added in the floating point number.
123.456 123.456 123.456
123.456 123.456 123.456
- It is also possible to specify the maximum number of decimal places for a floating point value or the maximum number of characters displayed for a string. This specification is known as precision.
- The precision is an unsigned integer. Precision is always preceded by a decimal point.
- A floating point number will be rounded if the number of digits exceeds the precision.
123.456000 123.456000 123.5 123.46 123.456
1.500000 1.500000 1.5 1.50 1.500
4575.254883 4575.254883 4575.3 4575.25 4575.255
123.456 123.456 1e+02 1.2e+02 123
12e 1.235e+02 1.23456e+02
123.456000 123.5 123.456
1.234560e+02 1.235e+02 1.23456e+02
- The specification for a string is same as numerical data. (ie) leading blanks will be added if the string is shorter than the specified field width and additional space will be allocatedd if the string is longer than the specified field width.
- The precision specification will determine the maximum number of characters that can be displayed.
- This rule is applied even if the minimum field width is larger than the entire string but additional blanks will be added to the truncated string.
%0.5s=Welco [no field width but the maximum precision is 5. so only 5 characters displayed.
%15.5s= < 10 blankspace > Welco [maximum precision is 5 so the string trimmed to Welco but the field width is 15 so 10 leading blanks added.]