1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*   ___       _               _           ____ 
 *  |_ _|_ __ | |_ _ __ ___   | |_ ___    / ___|
 *   | || '_ \| __| '__/ _ \  | __/ _ \  | |    
 *   | || | | | |_| | | (_) | | || (_) | | |___ 
 *  |___|_| |_|\__|_|  \___/   \__\___/   \____|
 */

/* Load function and value prototypes (signatures) */
#include <stdio.h>

/* Forward declarations - so the compiler knows how to compile
 * calls to these functions. */
int even(unsigned int n);
int odd(unsigned int n);

int main(int argc, char **argv) {
  /* Integral numeric types - signed or unsigned */
  /*  8-bit */ signed char c = -42;          // decimal notation
  /* 16-bit */ unsigned short int si = 0x2a; // hexadecimal notation
  /* 32-bit */ int x = 'a';                  // character notation
  /* 64-bit */ long long int l = 0217;       // octal notation

  /* Floating point numeric types */
  /* 32-bit */ float f = -1.1548;        // decimal notation
  /* 64-bit */ double d = 123.456e-17;   // scientific decimal notation

  /* Type sizes */
  printf(
      "sizeof(char) = %zu\n"
      "sizeof(short int) = %zu\n"
      "sizeof(int) = %zu\n"
      "sizeof(long int) = %zu\n"
      "sizeof(float) = %zu\n"
      "sizeof(double) = %zu\n",
      sizeof(char), 
      sizeof(short int), 
      sizeof(int), 
      sizeof(long int), 
      sizeof(float),
      sizeof(double));

  /* Boolean expressions compute an integer: 0 means false, non-0 true */

  /* Logical operations
   *   &&, ||    short-circuiting and/or
   *   !         logical negation
   *             (note, that !(!b) is not equal to b)
   *   ==        physical equality
   *   !=        not equal
   *   >, <, >=, <=
   *
   * Bitwise operators
   *   &, |, ^   and/or/xor
   *   ~         bitwise negation
   *
   * Ternary/conditional operator
   * COND ? EXPR1 : EXPR2  
   *             if COND is true, evaluate EXPR1 
   *             otherwise evaluate EXPR2
   */

  /* Warning: assignment is an expression! */
  int not_four = 2;

  if ((not_four = 4)) {
    printf("This will print and not_four is now %d\n", not_four);
  }

  /* For loops */
  for (int i = 0; i < 20; i += 2) {
    printf("i = %d\n", i);
  }

  /* While loops */
  int fact = 1;
  {
    int n = 10;
    while (n > 1) {
      fact *= n;
      n--;
    }
  }
  printf("fact = %d\n", fact);

  int number = 0;
  do {
    puts("Enter an integer (1-100):");
    scanf("%d", &number);
    printf("%d is %s\n", 
        number, 
        !odd(number) ? "even" : "odd");

  } while (number < 1 || number > 100);

  switch (number % 100) {
    case 1:
      printf("case 1\n");
      break;
    case 2:
      printf("case 2\n");
      //break;
    default:
      printf("default case\n");
      break;
  }

  return 0;
}

/** Is the given integer even? 
 *
 * @param n number to check
 * @return non-0 if n is even, otherwise 0 */
int even(unsigned int n) {
  return n == 2 || n > 2 && odd(n - 1);
}

/** Is the given integer odd?
 *
 * @param n number to check
 * @return non-0 if n is odd, otherwise 0 */
int odd(unsigned int n) {
  return n == 1 || n > 1 && even(n - 1);
}