Home » Archimedes archive » Acorn User » AU 1994-11.adf » !C_C » c/Precedence

c/Precedence

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 1994-11.adf » !C_C
Filename: c/Precedence
Read OK:
File size: 0364 bytes
Load address: 0000
Exec address: 0000
File contents
/* Precedence, which sets up a series of arithmetic expressions */
/* and prints out the results. The results are dependent on the */
/* order in which the expressions are evaluated, which depends  */
/* upon the precedence of the opertors. See the C Tutorial in   */
/* the magazine for more details.                               */

#include <stdio.h>

main()
{ int y = 2;

  printf("3 + 4 * 5 = %d\n",3 + 4 * 5);
  printf("y * 3 + 4 * 5 = %d\n", y * 3 + 4 * 5);
  printf("- y * 3 + 4 * 5 = %d\n", - y * 3 + 4 * 5);
  printf("- (y * 3 + 4 * 5) = %d\n\n", - (y * 3 + 4 * 5));

  printf("12 >> 1 = %d\n", 12 >> 1);
  printf("12 & 12 >> 1 = %d\n", 12 & 12 >> 1);
  printf("12 & 12 >> 1 ^ 6  = %d\n", 12 & 12 >> 1 ^ 6);
  printf("12 & 12 >> 1 ^ 6 | 8 = %d\n", 12 & 12 >> 1 ^ 6 | 8);
  printf("(12 & 12) >> 1 ^ (6 | 8) = %d\n", (12 & 12) >> 1 ^ (6 | 8));

  return 0;
}
00000000  2f 2a 20 50 72 65 63 65  64 65 6e 63 65 2c 20 77  |/* Precedence, w|
00000010  68 69 63 68 20 73 65 74  73 20 75 70 20 61 20 73  |hich sets up a s|
00000020  65 72 69 65 73 20 6f 66  20 61 72 69 74 68 6d 65  |eries of arithme|
00000030  74 69 63 20 65 78 70 72  65 73 73 69 6f 6e 73 20  |tic expressions |
00000040  2a 2f 0a 2f 2a 20 61 6e  64 20 70 72 69 6e 74 73  |*/./* and prints|
00000050  20 6f 75 74 20 74 68 65  20 72 65 73 75 6c 74 73  | out the results|
00000060  2e 20 54 68 65 20 72 65  73 75 6c 74 73 20 61 72  |. The results ar|
00000070  65 20 64 65 70 65 6e 64  65 6e 74 20 6f 6e 20 74  |e dependent on t|
00000080  68 65 20 2a 2f 0a 2f 2a  20 6f 72 64 65 72 20 69  |he */./* order i|
00000090  6e 20 77 68 69 63 68 20  74 68 65 20 65 78 70 72  |n which the expr|
000000a0  65 73 73 69 6f 6e 73 20  61 72 65 20 65 76 61 6c  |essions are eval|
000000b0  75 61 74 65 64 2c 20 77  68 69 63 68 20 64 65 70  |uated, which dep|
000000c0  65 6e 64 73 20 20 2a 2f  0a 2f 2a 20 75 70 6f 6e  |ends  */./* upon|
000000d0  20 74 68 65 20 70 72 65  63 65 64 65 6e 63 65 20  | the precedence |
000000e0  6f 66 20 74 68 65 20 6f  70 65 72 74 6f 72 73 2e  |of the opertors.|
000000f0  20 53 65 65 20 74 68 65  20 43 20 54 75 74 6f 72  | See the C Tutor|
00000100  69 61 6c 20 69 6e 20 20  20 2a 2f 0a 2f 2a 20 74  |ial in   */./* t|
00000110  68 65 20 6d 61 67 61 7a  69 6e 65 20 66 6f 72 20  |he magazine for |
00000120  6d 6f 72 65 20 64 65 74  61 69 6c 73 2e 20 20 20  |more details.   |
00000130  20 20 20 20 20 20 20 20  20 20 20 20 20 20 20 20  |                |
00000140  20 20 20 20 20 20 20 20  20 20 20 20 2a 2f 0a 0a  |            */..|
00000150  23 69 6e 63 6c 75 64 65  20 3c 73 74 64 69 6f 2e  |#include <stdio.|
00000160  68 3e 0a 0a 6d 61 69 6e  28 29 0a 7b 20 69 6e 74  |h>..main().{ int|
00000170  20 79 20 3d 20 32 3b 0a  0a 20 20 70 72 69 6e 74  | y = 2;..  print|
00000180  66 28 22 33 20 2b 20 34  20 2a 20 35 20 3d 20 25  |f("3 + 4 * 5 = %|
00000190  64 5c 6e 22 2c 33 20 2b  20 34 20 2a 20 35 29 3b  |d\n",3 + 4 * 5);|
000001a0  0a 20 20 70 72 69 6e 74  66 28 22 79 20 2a 20 33  |.  printf("y * 3|
000001b0  20 2b 20 34 20 2a 20 35  20 3d 20 25 64 5c 6e 22  | + 4 * 5 = %d\n"|
000001c0  2c 20 79 20 2a 20 33 20  2b 20 34 20 2a 20 35 29  |, y * 3 + 4 * 5)|
000001d0  3b 0a 20 20 70 72 69 6e  74 66 28 22 2d 20 79 20  |;.  printf("- y |
000001e0  2a 20 33 20 2b 20 34 20  2a 20 35 20 3d 20 25 64  |* 3 + 4 * 5 = %d|
000001f0  5c 6e 22 2c 20 2d 20 79  20 2a 20 33 20 2b 20 34  |\n", - y * 3 + 4|
00000200  20 2a 20 35 29 3b 0a 20  20 70 72 69 6e 74 66 28  | * 5);.  printf(|
00000210  22 2d 20 28 79 20 2a 20  33 20 2b 20 34 20 2a 20  |"- (y * 3 + 4 * |
00000220  35 29 20 3d 20 25 64 5c  6e 5c 6e 22 2c 20 2d 20  |5) = %d\n\n", - |
00000230  28 79 20 2a 20 33 20 2b  20 34 20 2a 20 35 29 29  |(y * 3 + 4 * 5))|
00000240  3b 0a 0a 20 20 70 72 69  6e 74 66 28 22 31 32 20  |;..  printf("12 |
00000250  3e 3e 20 31 20 3d 20 25  64 5c 6e 22 2c 20 31 32  |>> 1 = %d\n", 12|
00000260  20 3e 3e 20 31 29 3b 0a  20 20 70 72 69 6e 74 66  | >> 1);.  printf|
00000270  28 22 31 32 20 26 20 31  32 20 3e 3e 20 31 20 3d  |("12 & 12 >> 1 =|
00000280  20 25 64 5c 6e 22 2c 20  31 32 20 26 20 31 32 20  | %d\n", 12 & 12 |
00000290  3e 3e 20 31 29 3b 0a 20  20 70 72 69 6e 74 66 28  |>> 1);.  printf(|
000002a0  22 31 32 20 26 20 31 32  20 3e 3e 20 31 20 5e 20  |"12 & 12 >> 1 ^ |
000002b0  36 20 20 3d 20 25 64 5c  6e 22 2c 20 31 32 20 26  |6  = %d\n", 12 &|
000002c0  20 31 32 20 3e 3e 20 31  20 5e 20 36 29 3b 0a 20  | 12 >> 1 ^ 6);. |
000002d0  20 70 72 69 6e 74 66 28  22 31 32 20 26 20 31 32  | printf("12 & 12|
000002e0  20 3e 3e 20 31 20 5e 20  36 20 7c 20 38 20 3d 20  | >> 1 ^ 6 | 8 = |
000002f0  25 64 5c 6e 22 2c 20 31  32 20 26 20 31 32 20 3e  |%d\n", 12 & 12 >|
00000300  3e 20 31 20 5e 20 36 20  7c 20 38 29 3b 0a 20 20  |> 1 ^ 6 | 8);.  |
00000310  70 72 69 6e 74 66 28 22  28 31 32 20 26 20 31 32  |printf("(12 & 12|
00000320  29 20 3e 3e 20 31 20 5e  20 28 36 20 7c 20 38 29  |) >> 1 ^ (6 | 8)|
00000330  20 3d 20 25 64 5c 6e 22  2c 20 28 31 32 20 26 20  | = %d\n", (12 & |
00000340  31 32 29 20 3e 3e 20 31  20 5e 20 28 36 20 7c 20  |12) >> 1 ^ (6 | |
00000350  38 29 29 3b 0a 0a 20 20  72 65 74 75 72 6e 20 30  |8));..  return 0|
00000360  3b 0a 7d 0a                                       |;.}.|
00000364