Conversion Specifications with the printf() Function

A conversion specification is represented by a two character string that consists of the percent sign followed by a conversion character. The most common conversion characters are c, d, u, f, and s.

The program below uses the %c conversion code to print out single characters.

#include <stdio.h>


int main(void){
 
  printf("My first initial is on the next line:\n");
  printf("%c\n", 'A');
  printf("My second initial is on the next line:\n");
  printf("%c\n", 'B');
  printf("My third initial is on the next line:\n");
  printf("%c\n", 'J');
 
  return 0;

}

Between the % that starts a conversion specification and the conversion character that ends it, we may place an integer. For whole numbers, this specifies the minimum number of spaces the the integer should take up. With floating point numbers, we can place a period followed by a nonnegative integer to specify the number of digits to the right of the decimal point to be displayed.

 #include <stdio.h>


int main(void){
 
  /*
   * use a decimal-controlling modfier
   */
  printf("Today's high was %.1f degrees Celsius\n", 33.2);
  printf("$%.2f\n", 1.05);
 
  /*
   * use a width modifier
   */
  printf("%5d\n", 73);
  printf("%5d\n", 1138);
  printf("%5d\n", 451);
 
  /*
   * use both
   */
  printf("PRICES:\n\n");
  printf("$%7.2f\n", 19.19);
  printf("$%7.2f\n", 1.05);
  printf("$%7.2f\n", 3435.42);
  printf("$%7.2f\n", 2.5);
 
  return 0;
 
}

Using decimal-controlling modifiers with floating point output is quite common, as we rarely want the six decimal places that C produces without modifiers.

Adding a plus flag forces numbers to print with a leading sign.

#include <stdio.h>


int main(void){
 
  printf("%+7.2f\n", 33.0);
  printf("%+7.2f\n", -14.92);
  printf("%+7.2f\n", -7.77);
  printf("%+7.2f\n", 19.99);
  printf("%+7.2f\n", 234.5);
 
  return 0;
 
}

It is also possible to print an integer in hexadecimal and octal format.

#include <stdio.h>


int main(void){
 
  int a = 15;
  int b = 20;
  int c = 25;
  int d = 30;
  int e = 35;
 
  printf("The hex equivalent of %d is %x\n", a, a);
  printf("The hex equivalent of %d is %x\n", b, b);
  printf("The hex equivalent of %d is %x\n", d, d);
 
  printf("The octal equivalent of %d is %o\n", c, c);
  printf("The octal equivalent of %d is %o\n", e, e);
 
  return 0;
 
}

The C language contains a number of escape sequence characters in addition to the newline character, \n, that we have been using so far. Any time we insert an escape sequence within a printf() format string, the C program performs the action described by the escape sequence instead of printing it.

#include <stdio.h>


int main(void){
 
  printf("Ring a bell: \a \n");
  printf("Tabs \t are \t great.\n");
  printf("\"Illegitimi non carborundum\"\n");
 
  return 0;
 
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s