fork download
  1. #include <stdio.h>
  2.  
  3. #define SIZE 3
  4. #define NAMESIZE 20
  5.  
  6. struct president
  7. {
  8. char name [NAMESIZE];
  9. };
  10.  
  11. int main ()
  12. {
  13. int i; /* loop index */
  14.  
  15. /* These are my three favorite presidents */
  16. struct president myPresidents [SIZE] = {
  17. {"Ronald Reagan"},
  18. {"George Washington"},
  19. {"Thomas Jefferson"}
  20. };
  21.  
  22. printf ("\nPrint whatever size the names are, Left Justified \n");
  23. for (i = 0; i < SIZE; ++i )
  24. {
  25. printf ("%s\n",
  26. myPresidents[i].name);
  27. }
  28.  
  29. printf ("\n\nTake up a minimum of 20 spaces, Right Justified \n");
  30. for (i = 0; i < SIZE; ++i )
  31. {
  32. printf ("%20s\n",
  33. myPresidents[i].name);
  34. }
  35.  
  36. printf ("\n\nTake up a minimum of 10 spaces, pad as needed, Left Justified \n");
  37. printf ("... if name is longer, it will still print all characters\n");
  38. for (i = 0; i < SIZE; ++i )
  39. {
  40. printf ("%-10s\n",
  41. myPresidents[i].name);
  42. }
  43.  
  44. printf ("\n\nPrint a maximum of 15 characters, pad if needed, Left Justified,");
  45. printf ("\n... this is good if you need to limit a field size being displayed \n");
  46. for (i = 0; i < SIZE; ++i )
  47. {
  48. printf ("%-15.15s\n",
  49. myPresidents[i].name);
  50. }
  51.  
  52. printf ("\n\nPrint 20 characters, pad with spaces if needed, Left Justified,");
  53. printf ("\n... this is good if you need to display all characters, \n");
  54. printf ("\nand really useful in lining up items in a table format. \n");
  55. for (i = 0; i < SIZE; ++i )
  56. {
  57. printf ("%-20.20s\n",
  58. myPresidents[i].name);
  59. }
  60.  
  61. return (0);
  62. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Print whatever size the names are, Left Justified 
Ronald Reagan
George Washington
Thomas Jefferson


Take up a minimum of 20 spaces, Right Justified 
       Ronald Reagan
   George Washington
    Thomas Jefferson


Take up a minimum of 10 spaces, pad as needed, Left Justified 
... if name is longer, it will still print all characters
Ronald Reagan
George Washington
Thomas Jefferson


Print a maximum of 15 characters, pad if needed, Left Justified,
... this is good if you need to limit a field size being displayed 
Ronald Reagan  
George Washingt
Thomas Jefferso


Print 20 characters, pad with spaces if needed, Left Justified,
... this is good if you need to display all characters, 

and really useful in lining up items in a table format. 
Ronald Reagan       
George Washington   
Thomas Jefferson