Saturday 17 May 2014

Format Specifiers

We see printf() function in previous tutorial in which format specifier played a specific role in the function. So lets discuss more about Format Specifier. There are many types of format specifier which is shown in the below:

Conversion Specification
Output
%a
Floating-point number, hexadecimal digits and p-notation (C99).
%A
Floating-point number, hexadecimal digits and P-notation (C99).
%c
Single character.
%d
Signed decimal integer.
%e
Floating-point number, e-notation.
%E
Floating-point number, e-notation.
%f
Floating-point number, decimal notation.
%g
Use %f or %e, depending on the value. The %e style is used if the exponent is less than –4 or greater than or equal to the precision.
%G
Use %f or %E, depending on the value. The %E style is used if the exponent is less than –4 or greater than or equal to the precision.
%i
Signed decimal integer (same as %d).
%o
Unsigned octal integer.
%p
A pointer.
%s
Character string.
%u
Unsigned decimal integer.
%x
Unsigned hexadecimal integer, using hex digits 0f.
%X
Unsigned hexadecimal integer, using hex digits 0F.
%%
Prints a percent sign.

Lets take an example to understand:

 #include<stdio.h>

 main()
 {
  int a,b;
  float c,d;

  a = 15;
  b = a / 2;
  printf("%d\n",b);
  printf("%3d\n",b);
  printf("%03d\n",b);

  c = 15.3;
  d = c / 3;
  printf("%3.2f\n",d);
 }
Output of the source above:
7
   7
007
5.10

As you can see in the first printf statement we print a decimal. In the second printf statement we print the same decimal, but we use a width (%3d) to say that we want three digits (positions) reserved for the output.
The result is that two “space characters” are placed before printing the character. In the third printf statement we say almost the same as the previous one. Print the output with a width of three digits, but fill the space with 0.
In the fourth printf statement we want to print a float. In this printf statement we want to print three position before the decimal point (called width) and two positions behind the decimal point (called precision).Let’s recap:
  • %d (print as a decimal integer)
  • %6d (print as a decimal integer with a width of at least 6 wide)
  • %f (print as a floating point)
  • %4f (print as a floating point with a width of at least 4 wide)
  • %.4f (print as a floating point with a precision of four characters after the decimal point)
  • %3.2f (print as a floating point at least 3 wide and a precision of 2)
Lets take an another example:

#include<stdio.h>

main()
{
 printf("The color: %s\n", "blue");
 printf("First number: %d\n", 12345);
 printf("Second number: %04d\n", 25);
 printf("Third number: %i\n", 1234);
 printf("Float number: %3.2f\n", 3.14159);
 printf("Hexadecimal: %x\n", 255);
 printf("Octal: %o\n", 255);
 printf("Unsigned value: %u\n", 150);
 printf("Just print the percentage sign %%\n", 10);
}
Output of the source example:

The color: blue
First number: 12345
Second number: 0025
Third number: 1234
Float number: 3.14
Hexadecimal: ff
Octal: 377
Unsigned value: 150
Just print the percentage sign % 

Note: In the last printf statement only the percentage sign is printed.
The number 10 in this statement doesn’t matter; it’s not used in the output. So if you want to print a percentage number you would use something like this: printf(“%2d%%\n”, 10); (The output will be 10%)

More Example:

#include<stdio.h>

main()
{
 printf(":%s:\n", "Hello, world!");
 printf(":%15s:\n", "Hello, world!");
 printf(":%.10s:\n", "Hello, world!");
 printf(":%-10s:\n", "Hello, world!");
 printf(":%-15s:\n", "Hello, world!");
 printf(":%.15s:\n", "Hello, world!");
 printf(":%15.10s:\n", "Hello, world!");
 printf(":%-15.10s:\n", "Hello, world!");
}

:Hello, world!:
:  Hello, world!:
:Hello, wor:
:Hello, world!:
:Hello, world!  :
:Hello, world!:
:     Hello, wor:
:Hello, wor     :

As you can see, the string format conversion reacts very different from number format conversions.
  • The printf(“:%s:\n”, “Hello, world!”); statement prints the string (nothing special happens.)
  • The printf(“:%15s:\n”, “Hello, world!”); statement prints the string, but print 15 characters. If the string is smaller the “empty” positions will be filled with “whitespace.”
  • The printf(“:%.10s:\n”, “Hello, world!”); statement prints the string, but print only 10 characters of the string.
  • The printf(“:%-10s:\n”, “Hello, world!”); statement prints the string, but prints at least 10 characters. If the string is smaller “whitespace” is added at the end. (See next example.)
  • The printf(“:%-15s:\n”, “Hello, world!”); statement prints the string, but prints at least 15 characters. The string in this case is shorter than the defined 15 character, thus “whitespace” is added at the end (defined by the minus sign.)
  • The printf(“:%.15s:\n”, “Hello, world!”); statement prints the string, but print only 15 characters of the string. In this case the string is shorter than 15, thus the whole string is printed.
  • The printf(“:%15.10s:\n”, “Hello, world!”); statement prints the string, but print 15 characters.
    If the string is smaller the “empty” positions will be filled with “whitespace.” But it will only print a maximum of 10 characters, thus only part of new string (old string plus the whitespace positions) is printed.
  • The printf(“:%-15.10s:\n”, “Hello, world!”); statement prints the string, but it does the exact same thing as the previous statement, accept the “whitespace” is added at the end.

0 comments :

Post a Comment