- Printf function prints data from the computers memory to the standard output device.
- Syntax:printf("%converion character",Argument list);
Argument->It can be constants,single variable, array names (or) complex expressions - Example:
#include<stdio.h>
int main()
{
int i=10,j=20;
char text[20];
printf("%d %d %s\n",i,j,text);
return 0;
} - 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.
Example:#include < stdio.h>
int main()
{
int avg=80;
printf("Average=%d%\n",avg);
return 0;
}
Output
Average=80% - 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.
Example
#include< stdio.h >
int main()
{
float x=100.0,y=25.25;
printf("%f %f \n",x,y);
printf("%e %e\n",x,y);
return 0;
}
Output100.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.#include< stdio.h >
int main()
{
char text[100];
scanf("%[^\n]",text);
printf("%s",text);
return 0;
}
- 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.
- Syntax:%fieldwidth conversioncharacter
- 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.Rule1: Number of digits > field width=Add additional space
Rule2: Number of digits < field width=Add leading blankspace - Example:Output
#include<stdio.h>
int main()
{
int x=12345;
float y=123.456;
printf("%3d %5d %7d\n",x,x,x);
printf("%3f %10f %13f\n",y,y,y);
printf("%3e %10e %13e\n",y,y,y);
return 0;
}
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)=>12345
- g-type conversion:
In g-type conversion no extra trailing zeros are added in the floating point number.
Example:#include<stdio.h>
int main()
{
float x=123.456;
printf("%3g %10g %13g\n",x,x,x);
printf("%3g %13g %16g\n",x,x,x);
return 0;
}
Output
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.
- Example:#include<stdio.h>
int main()
{
float x=123.456,y=1.5,z=4575.255;
printf("%5f %7f %7.1f %7.2f %7.3f\n",x,x,x,x,x);
printf("%5f %7f %7.1f %7.2f %7.3f\n",y,y,y,y,y);
printf("%5f %7f %7.1f %7.2f %7.3f\n",z,z,z,z,z);
printf("%5g %7g %7.1g %7.2g %7.3g\n",x,x,x,x,x);
printf("12e %12.3e %12.5e\n",x,x,x);
//only precision no field width
printf("%f %0.1f %0.3f\n",x,x,x);
printf("%e %0.3e %0.5e\n",x,x,x);
return 0;
}
Output
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.
- string characters < field width=add leading blanks
- string characters > field width=add additional space
- The precision specification will determine the maximum number of characters that can be displayed. Precision < string characters=excess right most characters will not 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.
- Example:#include<stdio.h>
int main()
{
char text[20]="Welcome";
printf("%5s\n %7s\n %10s\n %15s\n %0.5s\n %15.5s\n",text,text,text,text,text,text);
return 0;
}
Output:
Welcome
Welcome
Welcome
Welcome
Welco
Welcome
%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.]