Absurd: An Introduction to C

Part VII, 20120815

So far, I have thrown a lot of information at you very quickly. Let's recap with completely useless programs.

The character variable type uses 8 bits of memory (one byte). This type is declared with the 'char' keyword. While the character type is used to store ASCII characters, it is technically an integer type. The default range for character types on most platforms is -127 to 127 (as most platforms default to signed). If the type is not signed, it is declared 'unsigned char' and the range is 0 to 255.

#include <stdio.h>
int main( void ) {
  char c = 'a';
  while ( c <= 'z' ) {
    printf("%c, ", c);
    c++;
  }
  return 0;
} // end main

Here, we are seeing the declaration of a character variable 'c' and a while loop to control the printing of the alphabet. Note that we can increment characters as if they were integers.

The variable type 'short' is actually 'short int' as in not-so-big integer. These are 16 bits and have a signed range of -32,767 to 32,767. Unsigned shorts have a range of 0 to 65,535. Signed integers have ranges of -2,147,483,647 to 2,147,483,647at 32 bits. Unsigned integers have a range of 0 to 4,294,967,295 in 32 bits. Be sure to check your platform specs as some platforms will have 16 bit integers. The 'long' data type is the same as an int on most newer systems. A long long is 64 bits and has a range of -9,223,372,036,854,775,807 to 9,223,372,036,854,775,807. Unsigned it has a rang of 0 to 18,446,744,073,709,551,615.

#include <stdio.h>
int main(void) {
  short s = 2;
  unsigned short us = 10000;
  int i = 0;
  i = s * us;
  printf("%d tims %d is %d\n", s, us, i);
  i = us / s;
  printf("%d divided by %d is %d\n", us, s, i);
  i = us - s;
  printf("%d minus %d is %d\n", us, s, i);
  i = us + s;
  printf("%d plus %d is %d\n", us, s, i);
  return 0;
}

Above, you can see three variables. The two shorts and the int. In C, no arithmetic is done at a lower precision than that of an int. This means that doing math on char or short will promote the value to an int. It is also important to remember that a negative number can not be cast as an unsigned variable without giving you garbage out.

All of the float types have ridiculous ranges. In general consider a float 7 numerals long, and consider a double 15 numerals long. A float has a range of 1e to the negative 38 to 1e to the positive 38. A double has a range of 2e to the negative 308 and 2e to a positive 308. If your long double is 32 bits its range is the same as a double. With a long double, refer to your compiler/libc implementation's documentation. A float is typically 32 bits long, and a double is typically 64 bits long. A long double is typically either 64 bits or 128 bits in length.

#include <stdio.h>
int main(void) {
  float f = 3.14;
  double d = 6.1896;
  long double ld = 334455.667788;
  long double result = 0.0;
  result = ( f * ld ) / d;
  printf("%llf\n", result);
}

Above, you will notice that I used a text formatting type of "%llf" which is used for long doubles. All numeral types, as well as characters, use these format specifiers. They can be quite useful. You use "%s" for strings (more on strings later), "%d" for decimal values (integers), "%c" for characters, "%f" for floats, "%llu" for long integers, "%hu" or "%i" can be used for integers, "%x" will convert a number to hex and you can use "%llx" for example, "%o" is like "%x" but will convert the output to octal.

Scope is something that we haven't dealt with too much, but it is very important as your programs start growing in size. So far, out variables have all been local. That is to say that the variables are accessible only to the function in which they are declared. We can pass those values to other functions as I specified earlier; using variables as arguments to other functions and returning them when we are done working with them. The other way to handle this is to declare the variable as global. To do this you would do something like:

#include <stdio.h>
int globalint = 0;
int main( void ) {
  while ( globalint < 11 ) {
    printf("%d\n", globalint);
    globalint++;
  }
  secondFunction();
  return 0;
}
int secondFunction( void ) {
  globalint++;
  printf("%d", globalint);
}

⇠ back

© MMIX - MMXX, absurd.wtf
Licentiam Absurdum