Absurd: An Introduction to C, V


As you might imagine, a large series of conditional statements could get really tedious. Imagine a series of 20 if statements... and imagine typing that out. Very quickly, it would become very annoying. For this reason, C has the switch-break.

switch (i) { 
  case 1 : 
    printf("The value of i is 1\n");
  case 2 :
    printf("The value of i is 2\n");
  default :
    printf("The value of i is %d\n", i);

This may look intimidating, but it is actually quite simple. In the first step, we say "switch (i)" which uses the variable "i" as the control. Depending upon the value of "i" we can change the behavior of a given program. So, for our first case we have "case 1" which means should the value of i be equal to 1. If it is, we execute the following statement and then break (exit the switch). If it is equal to 2, we would execute the next statement, and should it be equal to neither 1 nor 2 we execute the default.

So far, I have only introduced int and char variable data types. These two are useful because of their small memory requirements, and ease of use. A char can hold a value between 0 and 255, this makes them perfect for ASCII data (text), and they typically occupy a single byte each. The int data type typically occupies 2 bytes and can hold any whole number between 0 and 65535. Each of these requires that the variable be "unsigned". In the event that they are signed their value ranges change. Signed variables have a sign of either positive or negative. For char, this means the range is now -128 to 127. For an int, the range is -32768 to 32767. An int can also be long or short or normal. If you declare it as short then it takes a single byte. If you declare it as long it uses 4 bytes, and should you leave off either marker it is as already stated. Do not stress remembering the byte sizes or anything at the moment. Just know that you can choose "short int" for small numbers "int" for some moderate numbers and "long int" for really big numbers.

There are also "float" and "double" data types. These data types are used for numbers that have greater precision. Pi (3.14159) would not fit well into the integer data type. It would be truncated to a simple 3. So, we use a float data type to make sure that we can keep the precision of the number. If we have a very large floating point number, we use "double". If we have a very large double precision floating point number, we can use "long double".

To illustrate everything so far, examine the following:

#include <stdio.h>
int main( void ) {
  int i;
  short s = 10000;
  long l = 40000;
  float f = 3.14159;
  double d = 1000000000000;
  for ( i = 0; i <= s; i++ ) {
    if ( i != s ) {
      printf("The value of f is %f in decimal.\n", f);
      printf("The value of f is %o in octal.\n", f);
      printf("The value of f is %x in hex.\n", f);
    } // end if does not equal 
    f++; // increment f
    if ( i < s ) {
      printf("The value of d is %f in decimal.\n", d);
      printf("The value of d is %o in octal.\n", d);
      printf("The value of d is %x in hex.\n", d); 
    } // end if less than
    d--; // decrement d
    if ( i == s ) {
      printf("i and s are now equal at %d\n", s);
    } // end if equal
    if ( ( i == s ) && ( f < 3000 ) ) {
      printf("i and s are equal and f is less than 3000");
    } // end if equal and less than
    if ( ( f < l ) || ( f > s ) ) {
      printf("f is either less than l or greater than s.\n");
    } // end if less than or greater than
  } // end for
  return 0;
} // end main

Some notes: "!=" means does not equal, "==" is used for comparison while "=" is used for assignment, "&&" means and, "||" means or.

You'll notice some English sentences preceded by "//" in the above example. Those are comments. They are completely ignored by the compiler. They are only inserted in the code to provide aid to a human reader.

In some of the print statements, you will notice a "%o" or a "%x". The "%o" will convert a number to octal before printing. Octal is computer talk for base 8. This means that numbers in octal are counted as 0 1 2 3 4 5 6 7 10, where 10 equals 8. In octal 11 is equal to 9. The percent "%x" converts a number to hexadecimal before printing. Hex is base 16 and is counted as 0 1 2 3 4 5 6 7 8 9 A B C D E F 10, where 10 is equal to 16 and 11 would be equal to 17. Don't get too hung up on this topic. We will review bases later on.

⇠ back

© MMIX - MMXXII, absurd.wtf

Licentiam Absurdum