Displaying Numerical Results

 

printf ( )

 

printf allows us to :

  1. Evaluate arithmetic expressions.

  2. Display their results.

Arguments : items passed to a function and are always placed within the function name parentheses .

 

Arguments must be separated from one another with commas, so the function knows where one argument ends and the next begins.

 

For example printf ("The sum of 3 and 12 is %d", 3+12);

 

The first argument is the message The sum of 3 and 12 is %d , and the second argument is the expression 3+12 .

 

The first argument passed to printf( ) must always be a message.

 

A message that includes a conversion control sequence , such as %d is termed as control string  . A  conversion control sequence begins with a % symbol and ends with a conversion character (d,c,f ,etc.) . 

 

The  percent sign % tells printf( ) to print a value at a place in the message where the % is located . The d , placed immediately after the % tells printf( ) that the value should be printed as an integer.

 

When printf( ) sees the conversion control sequence in its control string , it substitutes the value of the next argument in place of the conversion control sequence.

 

Thus the statement

    printf ("The sum of 3 and 12 is %d .", 3+12);

causes the printout

    The sum of 3 and 12 is 15.

 

Datatype

Conversion control sequence

int

%d

float

%f

long

%ld

double

%lf

char

%c

 

The %f ,%lf conversion control sequences causes printf( ) to display 6 digits to the right of the decimal place.

For example , the statement

    printf ("The sum of %f and %f is %f",15.0, 18.0, 15.0 + 18.0);

 causes the display

    The sum of  15.000000 and 12.000000 is 18.000000

 

For example , the statement

    printf ("The first letter of the alphabet is %c",'a');

causes the printout

    The first letter of the alphabet is a

 

Formatted output

 

The format of numbers displayed by printf( ) can be controlled by field width specifiers included as part of conversion control sequence .

 

For example

    printf ("The total of %2d and %3d  is  %4d",7,8,7+8)

causes the printout

    The total of  7  and   8  is   15

 

The number 2 causes the first number to be print in a total field width of 2 space, in this case 1 blank space and followed by a number 7.

 

 Example

#include<stdio.h>
void main( ) {

printf("Absence of field width specifiers\n");
printf("\n %d",3);
printf("\n %d",15);
printf("\n %d",142);
printf("\n ---");
printf("\n %d",3+15+142);

printf("\n\nUsing field width specifiers\n");
printf("\n %3d",3);
printf("\n %3d",15);
printf("\n %3d",142);
printf("\n ---");
printf("\n %3d",3+15+142);
}

OUTPUT

3

15

142

---

160

 

    3

  15

142

---

160

 

 

In formatting floating point numbers we can use 1 or 2 field width specifiers .

 

Example of 1 field width specifiier

    printf (" %.3f ", 18.0) ;

causes the printout

    18.000

 

When using 2 field width specifiers . The first specifiers determine the total display width ,including the decimal point . The second determine how many digits are printed to the right of the decimal point .

 

For all numbers (integers, floating point and double ), printf( ) ignores the specified field width if the total field width is too small and allocates enough space for the integer part of the number.

 

The fractional part of both floating point and double is always displayed with the number of specified digits .

Specifier

Number

Display

Comments

|%2d|

3

|  3|

Number fits in field

|%2d|

24

|24|

Number fits in field

|%2d|

123

|123|

Field width ignored

|%2d|

2.5

Machine dependent ( 0 )

 

|%5.2f|

2.789

| 2.79|

rounded

|%5.2f|

12.1

|12.10|

add 0

|%5.2f|

123.123

|123.12|

truncated

|%5.2f|

123

Machine dependent

 

 

Format Modifiers

use a minus sign

For example

    printf("|%-10d",52);

    |52         |

Normally the sign is only displayed for negative numbers . To force positive and sign to be displayed , a (+) format modifier must be used , for example:

    printf("|%+10d|",12);

causes the display

    |       +12|

 

Format modifiers can be combined . For example , the conversion control sequence %+-10 would cause an integer number to both display its sign and be left justified . Since the order of the format modifiers is not critical ,we can use %-+10 in place of %+-10.

 

Other Number Bases

 

When outputting integers ,several display conversions are possible

 

Example

    #include<stdio.h>
    void main( ) {
    printf("\n The decimal (base 10) value of 18 is %d",18);
    printf("\n The octal (base 8) value of 15 is %o",15);
    printf("\n The hexadecimal (base 16) value of 10 is %x",10);
    }

would display

     The decimal (base 10) value of 18 is 18
     The octal (base 8) value of 15 is 17
     The hexadecimal (base 16) value of 10 is a

 

To designate an octal integer constant , the number must have a leading 0 . Hexadecimal numbers are denoted using a leading 0x.

 

Example

    #include<stdio.h>   
    void main( ) {
      printf("\n The decimal of value of octal 025 is %d",025);
      printf("\n The decimal of value of hexadecimal 0x37 is %d",0x37);
     }

display:
   
 The decimal of value of octal 025 is 21
     The decimal of value of hexadecimal 0x37 is 55

 

To force both octal and hexadecimal to be printed with a leading 0 and 0x we must use the # .

 

For example :

printf (" The octal value of decimal 21 is %#o ",21);

 

The same display conversion can be used to display character .

 

Example

    #include<stdio.h>
    void main( ) {
    printf("\n The decimal of value of the letter %c is %d",'a','a');   
    printf("\n The octal of value of the letter %c is %o",'a','a');
    printf("\n The hexadecimal of value of the letter %c is %x",'a','a');
    }

 

Display

      The decimal of value of the letter a is 97
      The octal of value of the letter a is 141
      The hexadecimal of value of the letter a is 61