Advertisement
dmilicev

magic of C function printf() v1.c

Oct 17th, 2019
296
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     magic of C function printf() v1.c
  3.  
  4.     Use of \b in function printf()
  5.     to move print cursor left
  6.  
  7.     printf("\b");   // move cursor one space left (backward)
  8.  
  9. https://stackoverflow.com/questions/6792812/the-backspace-escape-character-b-unexpected-behavior
  10.  
  11. */
  12.  
  13. #include <stdio.h>
  14. #include <time.h>                   // for clock()
  15.  
  16. #define SHORT_TIME 25               // time for delay() in milliseconds
  17. #define LONG_TIME 30*SHORT_TIME     // time for delay() in milliseconds
  18.  
  19. void delay(int milliseconds)
  20. {
  21.     clock_t start_time = clock();   // get start time
  22.  
  23.     // looping (do nothing) till required time is not acheived
  24.     while (clock() < start_time + milliseconds)
  25.         ;                           // do nothing
  26. }
  27.  
  28. // move cursor howMuch spaces left
  29. void moveCursorLeft(int howMuch)
  30. {
  31.     int i;
  32.  
  33.     for (i=0; i<howMuch; i++)       // repeat howMuch times
  34.     {
  35.         printf("\b");               // move cursor one space left
  36.     }
  37. }
  38.  
  39. // delete characters howMuch spaces left
  40. void backspace(int howMuch)
  41. {
  42.     int i;
  43.  
  44.     for (i=0; i<howMuch; i++)       // repeat howMuch times
  45.     {
  46.         printf("\b \b");            // delete one left character
  47.         delay(SHORT_TIME);          // to make process visible
  48.     }
  49. }
  50.  
  51. // print howMuch characters ch
  52. void characters(char ch, int howMuch)
  53. {
  54.     int i;
  55.  
  56.     for (i=0; i<howMuch; i++)       // repeat howMuch times
  57.     {
  58.         printf("%c",ch);
  59.         delay(SHORT_TIME);          // to make process visible
  60.     }
  61. }
  62.  
  63. // printing howMuch digits, cyclic from firstDigit to 9
  64. // 0 (ascii 48), 9 (ascii 57)
  65. void digits(int firstDigit, int howMuch)
  66. {
  67.     int offset=0, counter=0;
  68.     char ch = '0' + firstDigit;     // calculate first character to print
  69.  
  70.     for (counter=0; counter<howMuch; counter++)     // repeat howMuch times
  71.     {
  72.         printf("%c",ch + offset);   // print character
  73.  
  74.         offset++;                   // next character
  75.  
  76.         if(ch+offset>'9')           // if we reach character '9'
  77.         {
  78.             offset = 0;             // to begin again
  79.             ch = '0';               // from '0'
  80.         }
  81.  
  82.         delay(SHORT_TIME);          // to make process visible
  83.     }
  84. }
  85.  
  86. // printing howMuch small letters, cyclic from ch to 'z'
  87. // a (ascii 97), z (ascii 122)
  88. void smallLetters(char ch, int howMuch)
  89. {
  90.     int offset=0, counter=0;
  91.  
  92.     for (counter=0; counter<howMuch; counter++)     // repeat howMuch times
  93.     {
  94.         printf("%c",ch+offset);     // print letter
  95.  
  96.         offset++;                   // next letter
  97.  
  98.         if(ch+offset>'z')           // if we reach letter z
  99.         {
  100.             offset=0;               // to begin again
  101.             ch = 'a';               // from 'a'
  102.         }
  103.  
  104.         delay(SHORT_TIME);          // to make process visible
  105.     }
  106. }
  107.  
  108. // printing howMuch capital letters, cyclic from ch to 'Z'
  109. // A (ascii 65), Z (ascii 90)
  110. void capitalLetters(char ch, int howMuch)
  111. {
  112.     int offset=0, counter=0;
  113.  
  114.     for (counter=0; counter<howMuch; counter++)     // repeat howMuch times
  115.     {
  116.         printf("%c",ch+offset);     // print letter
  117.  
  118.         offset++;                   // next letter
  119.  
  120.         if(ch+offset>'Z')           // if we reach last letter 'Z'
  121.         {
  122.             offset = 0;             // to begin again
  123.             ch = 'A';               // from 'A'
  124.         }
  125.  
  126.         delay(SHORT_TIME);          // to make process visible
  127.     }
  128. }
  129.  
  130. int main(void)
  131. {
  132.     int i, j, row_length = 52, number_of_times = 2;
  133.  
  134.     printf("\n \t \t Magic of C function printf() \n\n");
  135.  
  136.     // print ruler
  137.     printf("\n123456789012345678901234567890123456789012345678901234567890\n");
  138.  
  139.     printf("\n\n");
  140.  
  141.     characters('*',row_length);         // row_length times printing character '*'
  142.  
  143.     printf("\n\n");
  144.  
  145.     digits(1,row_length);               // printing row_length characters, cyclic from 1 to 9
  146.  
  147.     printf("\n\n");
  148.  
  149.     smallLetters('a',row_length);       // printing row_length characters, cyclic from 'a'
  150.  
  151.     printf("\n\n");
  152.  
  153.     capitalLetters('A',row_length);     // printing row_length characters, cyclic from 'A'
  154.  
  155.     printf("\n\n\n");
  156.  
  157.     for (j=0; j<number_of_times; j++)   // do this number_of_times times
  158.     {
  159.         characters('*', row_length);    // printing character '*' row_length times
  160.         delay(LONG_TIME);               // to make process visible
  161.         backspace(row_length);          // delete characters backward
  162.  
  163.         digits(1, row_length);          // printing row_length characters, cyclic from 1 to 9, row_length times
  164.         delay(20*SHORT_TIME);           // to make process visible
  165.         backspace(row_length);          // delete characters backward
  166.  
  167.         smallLetters('a', row_length);  // printing row_length characters, cyclic from 'a' to 'z', row_length times
  168.         delay(20*SHORT_TIME);           // to make process visible
  169.         backspace(row_length);          // delete characters backward
  170.  
  171.         capitalLetters('A', row_length);// printing row_length characters, cyclic from 'A' to 'Z', row_length times
  172.         delay(20*SHORT_TIME);           // to make process visible
  173.         backspace(row_length);          // delete characters backward
  174.     }
  175.  
  176.     printf("\t \t That's all Folks ! \n\n");
  177.  
  178.     return 0;
  179. }
Advertisement
RAW Paste Data Copied
Advertisement