Monday, October 20, 2025

Formatted Output

Formatted Output

printf():  The printf() function is used to print the data from the computers memory onto a standard output device.  This function can be used to output any combination of numerical values, single character and strings.

Syntax: 

printf(“Control String Control  Message Conversion String”, List of arg1 );


Where control string is a string that contains backslash characters, Control message that conations the user defined information and conversion string that contains formatted information, and list of arg.  are arguments that represent the output data items.


A simple format specification has the following form:

%w.p type-specifier

Where w is an integer number that specifies the total number of columns for the output value and p is another integer number that specifies the number of digits to the right of the decimal point (of a real number) or the number of characters to be printed from a string. Both wad p are optional. Some examples of formatted printf statement are:

Example :  

int  a=6; float b=2.75; char d=’A’, city[10]=”KHAMMAM”;


Statement                 Output

printf(“Welcome To C Program”);         Welcome  To C Program

printf(“\nWelcome  To C Program”);  (skip line and Print next line)

                                                                         Welcome  To C Program

printf(“%d”,a);                        5

printf(“%f”,b);                 2.75

printf(“%c”,d);                         A

printf(“%s”,city);                 KHAMMAM

printf(“%d%f%c%s”,a,b,d,city);         52.75AKHAMMAM

printf(“%d\t%f\t%c\t%s”,a,b,d,city); 5   2.75   A    KHAMMAM

printf(“A=%d\tB=%f\tD=%c”,a,b,d); A=5    B=2.75    D=A


Where  %c %s %d %f are conversion characters.

 Output of Integer Numbers: The format specification for printing an integer number is

%wd

                Format   Output

printf("%d", 9876)                              9876

printf("%6d", 9876)                            _ _ 9876

                     printf("%2d", 9876)                            9876

          printf("%-6d", 9876)                           9876_ _

          printf("%06d", 9876)                          009876


Output of Real Numbers

The output of a real number may be displayed in decimal notation using the following format specification.

%w.pf

 The integer w indicates the minimum number of positions that are to be used for the display of the value and the integer p indicates the number of digits to be displayed after the decimal point (precision). The value, when displayed, is rounded to p decimal places and printed right-justified in the field of w columns. Leading blanks and trailing zeros will appear as necessary. The default precision is 6 decimal places. The negative numbers will be printed with the minus sign. The number will be displayed in the form -.

            Ex. y=98.7654

Format                                                 Output

printf("% 7.4f",y)                                98.7654

printf("%7.2f",y)                                 _ _98.77

printf("%-7.2f",y)                                98.77_ _

printf("%f",y)                                      98.7654

printf("%10.2e",y)                               _ _9.88e+01

printf("%11.4e",-y)                             -9.8765e+01

printf("%-l0.2e",y)                              9.88e+01_ _

printf("%e",y)                                      9.876540e+01

Printing of a Single Character

A single character can be displayed in a desired position using the format

%wc

The character will be displayed right-justified the field of w columns. We can make the display left-Justified by placing a minus sign before the integer w. The default value for w is 1.

KHAMMAM 507001

Specification                   Output 

                        |1 |2 |3 | 4 |5 |6 |7 |8 |9 |0 |1 |2  |3 |4 |5 |6 |7 |8 |9 |0  |

%s                   |K|H|A|M|M|A|   |5 |0 |7 |0  |0 |1 |   |   |   |   |   |   |    |

%20s               |   |   |   |   |    |   |   |K|H|A|M|M|A|   |5 |0 |7 |0 |0 |1  |

%20.10s          |   |   |   |   |    |   |   |   |   |   |K|H|A|M|M|A|M|   |5 | 0 |

%.5s                |K|H|A|M|M||  |   |   |   |   |   |   |    |   |   |   |   |   |   |    |

%-20.10s         |K|H|A|M|M|A|   |5 |0 |   |   |   |    |   |   |   |   |   |   |    |

%5s                 |K|H|A|M|M|A|   |5 |0 |7 |0 |0 |1  |   |   |   |   |   |   |    |


No comments:

Post a Comment