What is prototype of printf function?




Answer:
Prototype of printf function is:

int printf( const char *format ,…)

Explanation of each term:
Parameter of printf function is:

Second parameter:  (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.

First parameter: 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.

#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 prints data.
What will be output of following program?

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:

What will be output of following program?
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();

}  
Output:
. precision
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.

What will be output of following program?
void main()
{
char str[8]=”world”;
printf(“%10.2s\n”,str);
printf(“%10.8s\n”,str);
printf(“%10.-4s\n”,str);
getch();
}
 Output:

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.

What will be output of following program?
void main()
{
float a=4.5446;
printf(“%10.5f\n”,a);
printf(“%10.3f\n”,a);
printf(“%.4f\n”,a);
getch();
}  
Output:
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.

What will be output of following program?
void main()
{
int a=45;
printf(“%10.5d\n”,a);
printf(“%4.3d\n”,a);
printf(“%.0d\n”,a);
getch();
} 
Output:  
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

What will be output of following program?
#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 data-blogger-escaped-span="" style="mso-spacerun: yes;">  or xx>4 else same as f  e.g. 5,-5e-3
G:  same as E xx<-4 data-blogger-escaped-or="" data-blogger-escaped-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

10 comments:

hai this is mbn said...

You know before reading it i had a big confusion abt so many C concepts but after reading it all got fixed.

Anonymous said...

this is so grrrrrrr8.....what a collection of questions and concepts!!!!

Anonymous said...

When i try to print the value of c i am getting error can any one y

#include

void ellipsis(int a,...);

int main(int argc, char *argv[])
{
printf("Hello, world\n");
int a=5,b=10,c=20;
clrscr();
ellipsis(a,b,c);

return 0;
}

void ellipsis(int a,...)
{
printf("%d ",c);
}

Anonymous said...

because c is not recognised in void ellipsis funtion

Anonymous said...

Please can you give the outputs of all those printf statements!

Anonymous said...

Please can u also give the output of the printf statements

Anonymous said...

pls provide outputs for printf statements also!

Anonymous said...

Then, what is the method to use those other args passed in the function?

murugesan said...

because you have not passed all the arguments in the function parameters.
for eg:
declaration of function:
void ellipsis(int a,...);//it is possible
definition of function:
void ellipsis(int a,int b,int c) //your function definition should be like this
{
}

Anonymous said...

Is the below prototype same as above one??

int printf(char *,...)