Tuesday, 18 October 2016

Printf : Output Function

Getting Knowledge about input output is the most basic process to start learning any programming language. In C all available function reside in library file whose extension is .h

Stdio.h is header file which has most of the input output related function.I am going to show you most frequently useful function.

Printf


Printf function is use to print a string and variable value on monitor.  Various formatting special character used to make formatted output.

Syntax

Printf(“Formatted string format”,[Variable_List])

Escape sequences
Special function
\n
New Line
\t
Horizontal Tab
\r
Carriage return
\a
Alert Beep
\f
Form feed
\v
Vertical Tab
\\
Backslash
\’
Single quote
\’’
Double quote
\?
Question mark

E.g.

Printing string

Printf(“Hello Girfa”);
Printf(“\n\tHello\tGirfa”);
Printf(“\n**************************************\n”);
Printf(“\n\tName\tMohan\n\tSub.\tC\nCity\tvns”);
Printf(“\n***************************************”);

Printing variable

void main()
{
int a,b,c;
a=10;
b=20;
c=a+b;
printf("\nA=%d\nB=%d\nSum=%d",a,b,c);

}

As you can see %d and some variable names are in printf function  calling. What is the mean of these character and format let see following bullet for explanation.

  • Printf function can print string constant with variable data
  • For printing variable . you need to use special character for particular variable.
  • %d is special symbol use to read and write in integer
  • Sequence position of %d decides which variable come first to print.
  • After closing double variable list are seprated by qomma will printf left to right order means value of a will copy on first %d in string then b to second %d and so on.

Formatted Output



Printf function allow you to produce formatted output by making customization using

      %[number]<Eacape-Sequence>

int n=12345;
printf("\n5 space tab %5d ",n);
printf("\n10 space tab %10d ",n);
printf("\n15 space tab %15d ",n);

Tab Setting C Language

To left justify, use a negative number in the field width

printf("\n10 space tab %-10d ",n);

Floating point printing


  • Use the %f modifer to print floating point values in fixed notation
  • Use %e for exponential notation (Note that the e+02 means "times 10 to the 2nd power" )
  • You can also control the decimal precision, which is the number of places after the decimal. Output will round to the appropriate number of decimal places, if necessary
  • Field width can also be controlled, as with integers
float n=123.45;
printf("\n\tFix Point Decimal Print %f",n);
printf("\n\tTwo digit from point %.2f",n);
printf("\n\tNormal Decimal Print %3e",n);



Float Printing C Language




Character Printing

 char n='a';
printf("\n\tDefault Tab %c",n);
printf("\n\t5 Space Tab %5c",n);
printf("\n\t10 Space Tab %10c",n);

Character Printing C Language


String Printing


    printf("\n%s%5s%10s%15s","one","two","Three","Four");

String Print C Language

Download pdf