printf format identifiers.


printf formatting is controlled by 'format identifiers' which, are shown below in their simplest form.


    %d %i     Decimal signed integer.
    %o	      Octal integer.
    %x %X     Hex integer.
    %u	      Unsigned integer.
    %c	      Character.
    %s	      String. See below.
    %f	      double
    %e %E     double.
    %g %G     double.
    %p        pointer.
    %n	      Number of characters written by this printf.
              No argument expected.
    %%	      %. No argument expected.

These identifiers actually have upto 6 parts as shown in the table below. They MUST be used in the order shown.

% Flags Minimum field width Period Precision. Maximum field width Argument type
Required Optional Optional Optional Optional Required


%

The % marks the start and therfore is manatory.


Flags

The format identifers can be altered from their default function by applying the following flags:


   -      Left justify.
   0  	  Field is padded with 0's instead of blanks.
   +	  Sign of number always O/P.
   blank  Positive values begin with a blank.
   # 	  Various uses:
	  %#o (Octal) 0 prefix inserted.
	  %#x (Hex)   0x prefix added to non-zero values.
	  %#X (Hex)   0X prefix added to non-zero values.
	  %#e         Always show the decimal point.
	  %#E         Always show the decimal point.
	  %#f         Always show the decimal point.
	  %#g         Always show the decimal point trailing 
	  	      zeros not removed.
	  %#G         Always show the decimal point trailing
		      zeros not removed.
          

Here are a few more examples.


    printf(" %-10d \n", number);
    printf(" %010d \n", number);
    printf(" %-#10x \n", number);  
    printf(" %#x \n", number);


Minimum field width.

By default the width of a field will be the minimum required to hold the data. If you want to increase the field width you can use the following syntax.


  main()
  {
      int number    =  5;
      char *pointer = "little";

      printf("Here is a number-%4d-and a-%10s-word.\n", number, pointer);
  }
  
  /*********************************
   *
   *	Program result is:
   *
   * 	Here is a number-   5-and a-    little-word.
   *
   *********************************/

As you can see, the data is right justified within the field. It can be left justified by using the - flag. A maximum string width can also be specified.

The width can also be given as a variable as shown below.


    main()
    {
        int number=5;

        printf("---%*d----\n", 6, number);
    }
  
    /*********************************
     *
     *    Program result is:
     *
     *    ----     5---
     *
     *********************************/

The * is replaced with the supplied int to provide the ability to dynamically specify the field width.


Period

If you wish to specify the precision of an argument, it MUST be prefixed with the period.


Precision

The Precision takes different meanings for the different format types.

Float Precision

	%8.2f

This says you require a total field of 8 characters, within the 8 characters the last 2 will hold the decimal part.

	%.2f

The example above requests the minimum field width and the last two characters are to hold the decimal part.

Character String Maximum field width

The precision within a string format specifies the maximum field width.

	%4.8s

Specifies a minimum width of 4 and a maximum width of 8 characters. If the string is greater than 8 characters, it will be cropped down to size.

Here is a little program that shows an alternative to strncpy.

* Precision

As with the 'width' above, the precision does not have to be hard coded, the * symbol can be used and an integer supplied to give its value.


Format Identifiers

The format identifier describes the expected data. The identifier is the character that ends Here is a list of the format identifers as used in 'printf' ,'sprintf' ,'fprintf' and 'scanf'.

  1. Except for '%' and 'n', all the identifiers expect to extract an argument from the printf parameter list.
  2. All of the parmameters should be the value to be inserted. EXCEPT %s, this expects a pointer to be passed.

An example.


	main()
        {
            int number=5;
	    char *pointer="little";

	    printf("Here is a number %d and a %s word.\n", number, pointer);
	}
	/*********************************
	 *
	 *	Program result is:
	 *
	 * 	Here is a number 5 and a little word.
	 *
	 *********************************/


Including the library for printf will increase the memory used by the compiled application. In embedded systems or where memory usage is cricital, if printf is being used for only one or a few functions, it may be better to write something that serves the same function. See: C code for base conversion


Top Master Index Keywords Functions


Martin Leslie