Some basic of printf

This is the system define function  used to print something on monitor screen .

Definition of this function is written in header file stdio.h  and this is the reason we write #include<stdio.h> at top of our program code 

prototype of printf : 

 int printf(char const *,...) ;

printf always return integer value , and the value is equal to how much character or anything else printed on monitor screen.

printf("%d",2);   will return 1
printf("this is core");  will return 12

printf("%f",2.3)     will return 8( because this time printf     print  2.300000  on monitor screen )


Now parameter of printf is (char const *,...) 
first parameter is char const * (constant character string ) 

Ex- "what a look" , "Never go back" "12gshs"  

second parameter is three dot ...    this is called ellipsis
meaning of the ellipsis is you can put variable number of argument in printf 


variable number of arguments means sometime you are using printf to print one value , sometime more than one 

Have a look on this program it will be more clear 




#include<stdio.h>
void ellipsis(int var,...);

void main()

{

int a=5,b=10,c=20;


ellipsis(a);
ellipsis(a,b);
ellipsis(a,b,c);

}

void ellipsis(int var,...)

{

printf("%d",var);


}
output :- 5 5 5 
So printf can have variable number of argument , 


printf  format specifier have the following form 

%[flag][width][.precision] [F|N|H|L] type_char

Each  format specifier  begins with % (percent character )


details of all specifier with example 

[flag] - (optional)        work  
   
   -                  :-     Left-justifies the result, pads on the right with blanks. If not given, it right                   
                              justifies the result, pads on the left with zeros or blanks.

+                       data is preceded by + or – sign if number is +ve and 
                         -ve respectively.
 0(blank)          If value is nonnegative, the output begins with a blank instead of a plus;   

                                 negative values still begin with a minus.

#                         Specifies that arg is to be converted using an alternate form.

Note: Plus (+) takes precedence over blank () if both are given.


    
[width]             (optional)       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.      

width specifier 
n                      At least n characters are printed. If the output value has less than n characters,   
                             the output is padded with blanks (right-padded if - flag given, left-padded
                             otherwise).


0n                     At least n characters are printed. If the output value has less than n characters, 

                                is filled on the left with zeros.


*                The argument list supplies the width specifier, which must precede the actual         

                        argument being formatted.



  #include<stdio.h>
void main()

{

char str[12]="world_of_c";

printf("%15s\n",str);

printf("%-15s\n",str);

printf("%10s\n",str);

printf("%-10s\n",str);

printf("%(-10)s\n",str);

printf("%-(-10)s\n",str);

printf("%10s\n",str);

}


[.precision]   : (optional)this is used to print maximum number of character in case of string and in case of integer minimum  number of digit 


1. in case of string precision is a integer,which indicate
how many character of string will be printed .if the string length is less than the precision length the whole string will be printed 
  



#include<stdio.h>
int main()
{
   char str[10]=''worldwide"; 
 printf("%.2s",str);   // .precision      .2 
}


output :- wo 


Share on Google Plus

0 comments: