(q) What is prototype of printf function ? Explain each term.


Ans: 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:

#include

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. Eg “india” ,”tr4$”,”%d” etc.
A printf function generally display output What is written inside “ ”.
e.g

#include

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
width. In case right adjustment in put first (width-length of data) number of blank space then print 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();

}
Output:

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 indicate 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)
#include

#include

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

1 comments:

kailynvaliente said...

Casino Poker - Play Online for Real Money - Review
Casino Poker is 가입시꽁머니환전 a ishop4me.com classic Texas hold'em game with many variations. It 검증 업체 먹튀 랭크 is 감사 짤 easy to 라이트닝 바카라 play because it uses several software providers

Followers

 
C - Programming -