home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / tyc / list1414.c < prev    next >
C/C++ Source or Header  |  1993-10-16  |  2KB  |  68 lines

  1.  /* Demonstration of printf(). */
  2.  
  3.  #include <stdio.h>
  4.  
  5.  char *m1 = "Binary";
  6.  char *m2 = "Decimal";
  7.  char *m3 = "Octal";
  8.  char *m4 = "Hexadecimal";
  9.  
  10.  main()
  11.  {
  12.  
  13.  /* The terminating ; was left off the next statement */
  14.  /* in the book listing! */
  15.  
  16.      float d1 = 10000.123;
  17.      int n, f;
  18.  
  19.  
  20.      puts("Outputting a number with different field widths.\n");
  21.  
  22.      printf("%5f\n", d1);
  23.      printf("%10f\n", d1);
  24.      printf("%15f\n", d1);
  25.      printf("%20f\n", d1);
  26.      printf("%25f\n", d1);
  27.  
  28.      getch();
  29.  
  30.      puts("\nUse the * field width specifier to obtain field width");
  31.      puts("from a variable in the argument list.\n");
  32.  
  33.      for (n=5; n <=25; n+=5)
  34.          printf("%*f\n", n, d1);
  35.  
  36.      getch();
  37.  
  38.      puts("\nInclude leading zeros.\n");
  39.  
  40.      printf("%05f\n", d1);
  41.      printf("%010f\n", d1);
  42.      printf("%015f\n", d1);
  43.      printf("%020f\n", d1);
  44.      printf("%025f\n", d1);
  45.  
  46.      getch();
  47.  
  48.      puts("Display in octal, decimal, and hexadecimal.");
  49.      puts("Use # to precede octal and hex output with 0 and 0X.");
  50.      puts("Use - to left justify each value in its field.");
  51.      puts("First display column labels.\n");
  52.  
  53.      printf("%-15s%-15s%-15s", m1, m2, m3, m4);
  54.  
  55.      for (n = 1; n < 20; n++)
  56.          printf("\n%-15d%-#15o%-#15X", n, n, n, n);
  57.  
  58.      getch();
  59.  
  60.      puts("\n\nUse the %n conversion command to count characters.\n");
  61.  
  62.      printf("%s%s%s%s%n", m1, m2, m3, m4, &n);
  63.  
  64.      printf("\n\nThe last printf() output %d characters.", n);
  65.  
  66.      getch();
  67.  }
  68.