(q) What is prototype of printf function? Explain each term.
Answer:
Prototype of printf function is:
int printf( const char *format ,…)
Explanation of each term:
Parameter of printf function is:
(1) … (Three continuous dots): It is called ellipsis. It indicates the variable number of arguments.
Example of ellipsis:
void ellipsis(int a,...);
void main()
{
int a=5,b=10;
clrscr();
ellipsis(a,b);
getch();
}
void ellipsis(int a,...)
{
printf("%d ",a);
}
Output:5
So printf function can have any number of variables as an argument.
(2) Format: It is constant character string i.e. string constant. E.g. “india ”,”tr4$”,”%d” etc.
A printf function generally display output What is written inside “ ”.
e.g.
void main()
{
clrscr();
printf("AX%%vd___*");
getch();
}
Output: AX%%vd
A format can have special meaning if it follow special syntax rule
Type 1:
% [flag] [width] [. Precision] [modifier] type
Note: [ ] indicate optional term.
If format starts with % and follow same syntax rule then it prints data which are store in variables after first arguments in same order.
Explanation of each term: (here we called 2nd argument data)
Flag:
- : - sign indicate left adjustment of data.
+: data is preceded by + or – sign if number is +ve and –ve respectively.
0: It adds leading zero.
#: In case of octal number it add prefix 0 and in case of hexadecimal number 0X or 0x.
In case of floating type number it is compulsory to print decimal point (.) even if it is whole number
Width: It is +ve integer .It defines minimum field width. If length of data is smaller than the width. In case right adjustment in put first (width-length of data) number of blank space then prints data.
.
What will be output?
void main()
{
char str[8]=”world”;
printf(“%10s\n”,str);
printf(“%-10s\n”,str);
printf(“%2s\n”,str);
printf(“%-2s\n”,str);
printf(“%(-10)s\n”,str);
printf(“%-(-10)s\n”,str);
printf(“%+10s\n”,str);
getch();
}
void main()
{
short int a=-85;
printf(“%-10d\n”,a);
printf(“%+10d\n”,a);
printf(“%010d,%+010d,%-010d\n”,a,a,a);
printf(“%o %#o\n”,a,a);
printf(“%#x %X\n”,a,a);
printf(“%.0f %#.0f”,9.0f,9.0f);
getch();
}
. Precision
1. Case 1 if data type of data is string: precision is +ve integer which indicates how many character in the string will be printed. If precision is larger than length of data whole string will be printed.
e.g.
void main(){
char str[8]=”world”;
printf(“%10.2s\n”,str);
printf(“%10.8s\n”,str);
printf(“%10.-4s\n”,str);
getch();
}
Output:
2. Case 2 if data type of data is fractional (float, double, long double): It indicates how many digits will be printed after decimal. If precision is less than number of digits after decimal point in data then rounded value will be printed.
e.g.
void main(){
float a=4.5446;
printf(“%10.5f\n”,a);
printf(“%10.3f\n”,a);
printf(“%.4f\n”,a);
getch();
}
3 case 3 if data type of data is integer: It will that number of zero before the data.
(Total no. of zero=precision-length of data)
e.g.
void main(){
int a=45;
printf(“%10.5d\n”,a);
printf(“%4.3d\n”,a);
printf(“%.0d\n”,a);
getch();
}
4. Case 4: if data type of data is character: In case of char data type char has no meaning.
Note: In place of width and precision we can write * symbol which get values from arguments in printf function
(q)
void main(){
char *str="**********";
int i;
clrscr();
for(i=0;i<=10;i++)
printf("%*.*s\n",20,i,str);
getch();
}
Output:
* ** ***
modifier:
h: for short integer
l: for long integer or double
L: long double type
d: to print decimal integer e.g. 5,-5
i: to print decimal integer e.g. 5,-4
u: to print unsigned decimal integer e.g. 5
o: to print octal integer e.g. 05
x: to print hexadecimal integer e.g. ox5 (x will be in small letter)
X: to print hexadecimal integer e.g. oX5 (x will be in capital letter)
c: to print character e.g. ‘5’ s: to print string e.g. “5”
f: to print float number in this format n.ffffff e.g. 5.000000
e: to print float number in this format n.ffffffe+or -xx e.g. 5.000000e+01 (e will be in small letter)
E: to print float number in this format n.ffffff E+ or -xx e.g. 5.000000E-01 (e will be in capital letter) g: same as e if xx<-4 or xx>4 else same as f e.g. 5,-5e-3
G: same as E xx<-4 or xx>4 else same as f e.g. 5,-5E3
p: to print the segment address of pointer variable in hexadecimal integer e.g. 12a0
Fp: to print segment and offset address in hexadecimal integer e.g. 0004:00a1
TYPE 2:
Syntax: \x
Where x must be one of the following
a: to produce a sound(bell),ASCII value=7
b: to shift one space back(back space) ,ASCII value=8
t: to shift zero to seven space forward (horizontal tab) ,ASCII value=9
n: to print data in new line(new line) ,ASCII value=10
v: use in printer for vertical tab(vertical tab) ,ASCII value=11
f: use in printer to come first position(from feed) ,ASCII value=12
r: shift the cursor to first position(carriage return) ,ASCII value=13
No comments:
Post a Comment