Home » Archimedes archive » Acorn User » AU 1995-05.adf » !C_C » c/printf

c/printf

This website contains an archive of files for the Acorn Electron, BBC Micro, Acorn Archimedes, Commodore 16 and Commodore 64 computers, which Dominic Ford has rescued from his private collection of floppy disks and cassettes.

Some of these files were originally commercial releases in the 1980s and 1990s, but they are now widely available online. I assume that copyright over them is no longer being asserted. If you own the copyright and would like files to be removed, please contact me.

Tape/disk: Home » Archimedes archive » Acorn User » AU 1995-05.adf » !C_C
Filename: c/printf
Read OK:
File size: 0971 bytes
Load address: 0000
Exec address: 0000
File contents
/* C for yourself - Acorn User Demonstration C program
   by Steve Mumford for Acorn User May 1995
   printf, scanf and file access */

#include <stdio.h>

main()
{
   int   var1 = 15;
   int   var2 = 34;
   float fvar = 14.3782;
   FILE  *fpointer;
   char  letter;

   printf("printf and scanf - C for Yourself May 1995\n");
   printf("------------------------------------------\n\n");

   printf("Variable 1 = %d, variable 2 = %d.\n", var1, var2);
   printf("Floating point variable = %f\n\n", fvar);

   printf("The last variable only has four decimal places, but it was\n");
   printf("printed out to six. This can be altered by including other\n");
   printf("information along with the conversion specifiers, as follows.\n\n");

   printf("Floating point number to 4 decimal places: %.4f\n\n",fvar);
   printf("This is how you print a fixed width field:\n");
   printf("%7d %7d %7.4f\n", var1, var2, fvar);
   printf("%7.4f %7d %7d\n\n", fvar, var2, var1);

   printf("You can also change the justification so that the figures are\n");
   printf("left justified.\n");
   printf("%-7d %-7d %-7.4f\n\n", var1, var2, fvar);

   letter = 'B';
   printf("Characters can also be printed: %c\n\n",letter);

   printf("Please press RETURN to continue.\n");
   letter = getchar();
   
   printf("\f"); /* This control code clears the screen */
   printf("That last printf cleared the screen by using the code \\f.\n");
   printf("If you want to print the \\ character, use \\\\ as the control code.\n\n");
   printf("Please enter a new integer for var1:\n");
   scanf("%d", &var1);
   while (getchar() != '\n');
   printf("Now, please enter another integer for var2:\n");
   scanf("%d", &var2);
   while (getchar() != '\n');
   printf("Finally, please enter a floating point number for fvar:\n");
   scanf("%f",&fvar);
   while (getchar() != '\n');


   if ((fpointer = fopen("testfile","w")) == 0) {
      printf("File open failed. Please change directory to one which has write access.\n");
      return;
   }
   fprintf(fpointer, "This is some text, and this is variable 1: %d\n", var1);
   fprintf(fpointer, "Variable 2: %d\nFloating point variable: %.4f\n", var2, fvar);
   if (fclose(fpointer) != 0) {
      printf("File close failed. Sorry!\n");
   } else {
      printf("A text file called \'testfile\' has just been created in the current directory.\n");
      printf("Try loading it into Edit...\n");
   }
}

00000000  2f 2a 20 43 20 66 6f 72  20 79 6f 75 72 73 65 6c  |/* C for yoursel|
00000010  66 20 2d 20 41 63 6f 72  6e 20 55 73 65 72 20 44  |f - Acorn User D|
00000020  65 6d 6f 6e 73 74 72 61  74 69 6f 6e 20 43 20 70  |emonstration C p|
00000030  72 6f 67 72 61 6d 0a 20  20 20 62 79 20 53 74 65  |rogram.   by Ste|
00000040  76 65 20 4d 75 6d 66 6f  72 64 20 66 6f 72 20 41  |ve Mumford for A|
00000050  63 6f 72 6e 20 55 73 65  72 20 4d 61 79 20 31 39  |corn User May 19|
00000060  39 35 0a 20 20 20 70 72  69 6e 74 66 2c 20 73 63  |95.   printf, sc|
00000070  61 6e 66 20 61 6e 64 20  66 69 6c 65 20 61 63 63  |anf and file acc|
00000080  65 73 73 20 2a 2f 0a 0a  23 69 6e 63 6c 75 64 65  |ess */..#include|
00000090  20 3c 73 74 64 69 6f 2e  68 3e 0a 0a 6d 61 69 6e  | <stdio.h>..main|
000000a0  28 29 0a 7b 0a 20 20 20  69 6e 74 20 20 20 76 61  |().{.   int   va|
000000b0  72 31 20 3d 20 31 35 3b  0a 20 20 20 69 6e 74 20  |r1 = 15;.   int |
000000c0  20 20 76 61 72 32 20 3d  20 33 34 3b 0a 20 20 20  |  var2 = 34;.   |
000000d0  66 6c 6f 61 74 20 66 76  61 72 20 3d 20 31 34 2e  |float fvar = 14.|
000000e0  33 37 38 32 3b 0a 20 20  20 46 49 4c 45 20 20 2a  |3782;.   FILE  *|
000000f0  66 70 6f 69 6e 74 65 72  3b 0a 20 20 20 63 68 61  |fpointer;.   cha|
00000100  72 20 20 6c 65 74 74 65  72 3b 0a 0a 20 20 20 70  |r  letter;..   p|
00000110  72 69 6e 74 66 28 22 70  72 69 6e 74 66 20 61 6e  |rintf("printf an|
00000120  64 20 73 63 61 6e 66 20  2d 20 43 20 66 6f 72 20  |d scanf - C for |
00000130  59 6f 75 72 73 65 6c 66  20 4d 61 79 20 31 39 39  |Yourself May 199|
00000140  35 5c 6e 22 29 3b 0a 20  20 20 70 72 69 6e 74 66  |5\n");.   printf|
00000150  28 22 2d 2d 2d 2d 2d 2d  2d 2d 2d 2d 2d 2d 2d 2d  |("--------------|
00000160  2d 2d 2d 2d 2d 2d 2d 2d  2d 2d 2d 2d 2d 2d 2d 2d  |----------------|
00000170  2d 2d 2d 2d 2d 2d 2d 2d  2d 2d 2d 2d 5c 6e 5c 6e  |------------\n\n|
00000180  22 29 3b 0a 0a 20 20 20  70 72 69 6e 74 66 28 22  |");..   printf("|
00000190  56 61 72 69 61 62 6c 65  20 31 20 3d 20 25 64 2c  |Variable 1 = %d,|
000001a0  20 76 61 72 69 61 62 6c  65 20 32 20 3d 20 25 64  | variable 2 = %d|
000001b0  2e 5c 6e 22 2c 20 76 61  72 31 2c 20 76 61 72 32  |.\n", var1, var2|
000001c0  29 3b 0a 20 20 20 70 72  69 6e 74 66 28 22 46 6c  |);.   printf("Fl|
000001d0  6f 61 74 69 6e 67 20 70  6f 69 6e 74 20 76 61 72  |oating point var|
000001e0  69 61 62 6c 65 20 3d 20  25 66 5c 6e 5c 6e 22 2c  |iable = %f\n\n",|
000001f0  20 66 76 61 72 29 3b 0a  0a 20 20 20 70 72 69 6e  | fvar);..   prin|
00000200  74 66 28 22 54 68 65 20  6c 61 73 74 20 76 61 72  |tf("The last var|
00000210  69 61 62 6c 65 20 6f 6e  6c 79 20 68 61 73 20 66  |iable only has f|
00000220  6f 75 72 20 64 65 63 69  6d 61 6c 20 70 6c 61 63  |our decimal plac|
00000230  65 73 2c 20 62 75 74 20  69 74 20 77 61 73 5c 6e  |es, but it was\n|
00000240  22 29 3b 0a 20 20 20 70  72 69 6e 74 66 28 22 70  |");.   printf("p|
00000250  72 69 6e 74 65 64 20 6f  75 74 20 74 6f 20 73 69  |rinted out to si|
00000260  78 2e 20 54 68 69 73 20  63 61 6e 20 62 65 20 61  |x. This can be a|
00000270  6c 74 65 72 65 64 20 62  79 20 69 6e 63 6c 75 64  |ltered by includ|
00000280  69 6e 67 20 6f 74 68 65  72 5c 6e 22 29 3b 0a 20  |ing other\n");. |
00000290  20 20 70 72 69 6e 74 66  28 22 69 6e 66 6f 72 6d  |  printf("inform|
000002a0  61 74 69 6f 6e 20 61 6c  6f 6e 67 20 77 69 74 68  |ation along with|
000002b0  20 74 68 65 20 63 6f 6e  76 65 72 73 69 6f 6e 20  | the conversion |
000002c0  73 70 65 63 69 66 69 65  72 73 2c 20 61 73 20 66  |specifiers, as f|
000002d0  6f 6c 6c 6f 77 73 2e 5c  6e 5c 6e 22 29 3b 0a 0a  |ollows.\n\n");..|
000002e0  20 20 20 70 72 69 6e 74  66 28 22 46 6c 6f 61 74  |   printf("Float|
000002f0  69 6e 67 20 70 6f 69 6e  74 20 6e 75 6d 62 65 72  |ing point number|
00000300  20 74 6f 20 34 20 64 65  63 69 6d 61 6c 20 70 6c  | to 4 decimal pl|
00000310  61 63 65 73 3a 20 25 2e  34 66 5c 6e 5c 6e 22 2c  |aces: %.4f\n\n",|
00000320  66 76 61 72 29 3b 0a 20  20 20 70 72 69 6e 74 66  |fvar);.   printf|
00000330  28 22 54 68 69 73 20 69  73 20 68 6f 77 20 79 6f  |("This is how yo|
00000340  75 20 70 72 69 6e 74 20  61 20 66 69 78 65 64 20  |u print a fixed |
00000350  77 69 64 74 68 20 66 69  65 6c 64 3a 5c 6e 22 29  |width field:\n")|
00000360  3b 0a 20 20 20 70 72 69  6e 74 66 28 22 25 37 64  |;.   printf("%7d|
00000370  20 25 37 64 20 25 37 2e  34 66 5c 6e 22 2c 20 76  | %7d %7.4f\n", v|
00000380  61 72 31 2c 20 76 61 72  32 2c 20 66 76 61 72 29  |ar1, var2, fvar)|
00000390  3b 0a 20 20 20 70 72 69  6e 74 66 28 22 25 37 2e  |;.   printf("%7.|
000003a0  34 66 20 25 37 64 20 25  37 64 5c 6e 5c 6e 22 2c  |4f %7d %7d\n\n",|
000003b0  20 66 76 61 72 2c 20 76  61 72 32 2c 20 76 61 72  | fvar, var2, var|
000003c0  31 29 3b 0a 0a 20 20 20  70 72 69 6e 74 66 28 22  |1);..   printf("|
000003d0  59 6f 75 20 63 61 6e 20  61 6c 73 6f 20 63 68 61  |You can also cha|
000003e0  6e 67 65 20 74 68 65 20  6a 75 73 74 69 66 69 63  |nge the justific|
000003f0  61 74 69 6f 6e 20 73 6f  20 74 68 61 74 20 74 68  |ation so that th|
00000400  65 20 66 69 67 75 72 65  73 20 61 72 65 5c 6e 22  |e figures are\n"|
00000410  29 3b 0a 20 20 20 70 72  69 6e 74 66 28 22 6c 65  |);.   printf("le|
00000420  66 74 20 6a 75 73 74 69  66 69 65 64 2e 5c 6e 22  |ft justified.\n"|
00000430  29 3b 0a 20 20 20 70 72  69 6e 74 66 28 22 25 2d  |);.   printf("%-|
00000440  37 64 20 25 2d 37 64 20  25 2d 37 2e 34 66 5c 6e  |7d %-7d %-7.4f\n|
00000450  5c 6e 22 2c 20 76 61 72  31 2c 20 76 61 72 32 2c  |\n", var1, var2,|
00000460  20 66 76 61 72 29 3b 0a  0a 20 20 20 6c 65 74 74  | fvar);..   lett|
00000470  65 72 20 3d 20 27 42 27  3b 0a 20 20 20 70 72 69  |er = 'B';.   pri|
00000480  6e 74 66 28 22 43 68 61  72 61 63 74 65 72 73 20  |ntf("Characters |
00000490  63 61 6e 20 61 6c 73 6f  20 62 65 20 70 72 69 6e  |can also be prin|
000004a0  74 65 64 3a 20 25 63 5c  6e 5c 6e 22 2c 6c 65 74  |ted: %c\n\n",let|
000004b0  74 65 72 29 3b 0a 0a 20  20 20 70 72 69 6e 74 66  |ter);..   printf|
000004c0  28 22 50 6c 65 61 73 65  20 70 72 65 73 73 20 52  |("Please press R|
000004d0  45 54 55 52 4e 20 74 6f  20 63 6f 6e 74 69 6e 75  |ETURN to continu|
000004e0  65 2e 5c 6e 22 29 3b 0a  20 20 20 6c 65 74 74 65  |e.\n");.   lette|
000004f0  72 20 3d 20 67 65 74 63  68 61 72 28 29 3b 0a 20  |r = getchar();. |
00000500  20 20 0a 20 20 20 70 72  69 6e 74 66 28 22 5c 66  |  .   printf("\f|
00000510  22 29 3b 20 2f 2a 20 54  68 69 73 20 63 6f 6e 74  |"); /* This cont|
00000520  72 6f 6c 20 63 6f 64 65  20 63 6c 65 61 72 73 20  |rol code clears |
00000530  74 68 65 20 73 63 72 65  65 6e 20 2a 2f 0a 20 20  |the screen */.  |
00000540  20 70 72 69 6e 74 66 28  22 54 68 61 74 20 6c 61  | printf("That la|
00000550  73 74 20 70 72 69 6e 74  66 20 63 6c 65 61 72 65  |st printf cleare|
00000560  64 20 74 68 65 20 73 63  72 65 65 6e 20 62 79 20  |d the screen by |
00000570  75 73 69 6e 67 20 74 68  65 20 63 6f 64 65 20 5c  |using the code \|
00000580  5c 66 2e 5c 6e 22 29 3b  0a 20 20 20 70 72 69 6e  |\f.\n");.   prin|
00000590  74 66 28 22 49 66 20 79  6f 75 20 77 61 6e 74 20  |tf("If you want |
000005a0  74 6f 20 70 72 69 6e 74  20 74 68 65 20 5c 5c 20  |to print the \\ |
000005b0  63 68 61 72 61 63 74 65  72 2c 20 75 73 65 20 5c  |character, use \|
000005c0  5c 5c 5c 20 61 73 20 74  68 65 20 63 6f 6e 74 72  |\\\ as the contr|
000005d0  6f 6c 20 63 6f 64 65 2e  5c 6e 5c 6e 22 29 3b 0a  |ol code.\n\n");.|
000005e0  20 20 20 70 72 69 6e 74  66 28 22 50 6c 65 61 73  |   printf("Pleas|
000005f0  65 20 65 6e 74 65 72 20  61 20 6e 65 77 20 69 6e  |e enter a new in|
00000600  74 65 67 65 72 20 66 6f  72 20 76 61 72 31 3a 5c  |teger for var1:\|
00000610  6e 22 29 3b 0a 20 20 20  73 63 61 6e 66 28 22 25  |n");.   scanf("%|
00000620  64 22 2c 20 26 76 61 72  31 29 3b 0a 20 20 20 77  |d", &var1);.   w|
00000630  68 69 6c 65 20 28 67 65  74 63 68 61 72 28 29 20  |hile (getchar() |
00000640  21 3d 20 27 5c 6e 27 29  3b 0a 20 20 20 70 72 69  |!= '\n');.   pri|
00000650  6e 74 66 28 22 4e 6f 77  2c 20 70 6c 65 61 73 65  |ntf("Now, please|
00000660  20 65 6e 74 65 72 20 61  6e 6f 74 68 65 72 20 69  | enter another i|
00000670  6e 74 65 67 65 72 20 66  6f 72 20 76 61 72 32 3a  |nteger for var2:|
00000680  5c 6e 22 29 3b 0a 20 20  20 73 63 61 6e 66 28 22  |\n");.   scanf("|
00000690  25 64 22 2c 20 26 76 61  72 32 29 3b 0a 20 20 20  |%d", &var2);.   |
000006a0  77 68 69 6c 65 20 28 67  65 74 63 68 61 72 28 29  |while (getchar()|
000006b0  20 21 3d 20 27 5c 6e 27  29 3b 0a 20 20 20 70 72  | != '\n');.   pr|
000006c0  69 6e 74 66 28 22 46 69  6e 61 6c 6c 79 2c 20 70  |intf("Finally, p|
000006d0  6c 65 61 73 65 20 65 6e  74 65 72 20 61 20 66 6c  |lease enter a fl|
000006e0  6f 61 74 69 6e 67 20 70  6f 69 6e 74 20 6e 75 6d  |oating point num|
000006f0  62 65 72 20 66 6f 72 20  66 76 61 72 3a 5c 6e 22  |ber for fvar:\n"|
00000700  29 3b 0a 20 20 20 73 63  61 6e 66 28 22 25 66 22  |);.   scanf("%f"|
00000710  2c 26 66 76 61 72 29 3b  0a 20 20 20 77 68 69 6c  |,&fvar);.   whil|
00000720  65 20 28 67 65 74 63 68  61 72 28 29 20 21 3d 20  |e (getchar() != |
00000730  27 5c 6e 27 29 3b 0a 0a  0a 20 20 20 69 66 20 28  |'\n');...   if (|
00000740  28 66 70 6f 69 6e 74 65  72 20 3d 20 66 6f 70 65  |(fpointer = fope|
00000750  6e 28 22 74 65 73 74 66  69 6c 65 22 2c 22 77 22  |n("testfile","w"|
00000760  29 29 20 3d 3d 20 30 29  20 7b 0a 20 20 20 20 20  |)) == 0) {.     |
00000770  20 70 72 69 6e 74 66 28  22 46 69 6c 65 20 6f 70  | printf("File op|
00000780  65 6e 20 66 61 69 6c 65  64 2e 20 50 6c 65 61 73  |en failed. Pleas|
00000790  65 20 63 68 61 6e 67 65  20 64 69 72 65 63 74 6f  |e change directo|
000007a0  72 79 20 74 6f 20 6f 6e  65 20 77 68 69 63 68 20  |ry to one which |
000007b0  68 61 73 20 77 72 69 74  65 20 61 63 63 65 73 73  |has write access|
000007c0  2e 5c 6e 22 29 3b 0a 20  20 20 20 20 20 72 65 74  |.\n");.      ret|
000007d0  75 72 6e 3b 0a 20 20 20  7d 0a 20 20 20 66 70 72  |urn;.   }.   fpr|
000007e0  69 6e 74 66 28 66 70 6f  69 6e 74 65 72 2c 20 22  |intf(fpointer, "|
000007f0  54 68 69 73 20 69 73 20  73 6f 6d 65 20 74 65 78  |This is some tex|
00000800  74 2c 20 61 6e 64 20 74  68 69 73 20 69 73 20 76  |t, and this is v|
00000810  61 72 69 61 62 6c 65 20  31 3a 20 25 64 5c 6e 22  |ariable 1: %d\n"|
00000820  2c 20 76 61 72 31 29 3b  0a 20 20 20 66 70 72 69  |, var1);.   fpri|
00000830  6e 74 66 28 66 70 6f 69  6e 74 65 72 2c 20 22 56  |ntf(fpointer, "V|
00000840  61 72 69 61 62 6c 65 20  32 3a 20 25 64 5c 6e 46  |ariable 2: %d\nF|
00000850  6c 6f 61 74 69 6e 67 20  70 6f 69 6e 74 20 76 61  |loating point va|
00000860  72 69 61 62 6c 65 3a 20  25 2e 34 66 5c 6e 22 2c  |riable: %.4f\n",|
00000870  20 76 61 72 32 2c 20 66  76 61 72 29 3b 0a 20 20  | var2, fvar);.  |
00000880  20 69 66 20 28 66 63 6c  6f 73 65 28 66 70 6f 69  | if (fclose(fpoi|
00000890  6e 74 65 72 29 20 21 3d  20 30 29 20 7b 0a 20 20  |nter) != 0) {.  |
000008a0  20 20 20 20 70 72 69 6e  74 66 28 22 46 69 6c 65  |    printf("File|
000008b0  20 63 6c 6f 73 65 20 66  61 69 6c 65 64 2e 20 53  | close failed. S|
000008c0  6f 72 72 79 21 5c 6e 22  29 3b 0a 20 20 20 7d 20  |orry!\n");.   } |
000008d0  65 6c 73 65 20 7b 0a 20  20 20 20 20 20 70 72 69  |else {.      pri|
000008e0  6e 74 66 28 22 41 20 74  65 78 74 20 66 69 6c 65  |ntf("A text file|
000008f0  20 63 61 6c 6c 65 64 20  5c 27 74 65 73 74 66 69  | called \'testfi|
00000900  6c 65 5c 27 20 68 61 73  20 6a 75 73 74 20 62 65  |le\' has just be|
00000910  65 6e 20 63 72 65 61 74  65 64 20 69 6e 20 74 68  |en created in th|
00000920  65 20 63 75 72 72 65 6e  74 20 64 69 72 65 63 74  |e current direct|
00000930  6f 72 79 2e 5c 6e 22 29  3b 0a 20 20 20 20 20 20  |ory.\n");.      |
00000940  70 72 69 6e 74 66 28 22  54 72 79 20 6c 6f 61 64  |printf("Try load|
00000950  69 6e 67 20 69 74 20 69  6e 74 6f 20 45 64 69 74  |ing it into Edit|
00000960  2e 2e 2e 5c 6e 22 29 3b  0a 20 20 20 7d 0a 7d 0a  |...\n");.   }.}.|
00000970  0a                                                |.|
00000971