------------------------------------------------------------ Binary Numbers * Quick look at Binary Numbers * Let's look at the decimal system first * consider 5281 * The "1" is in the 1's column * The "8" is in the 10's column * The "2" is in the 100's column * The "5" is in the 1000's column * One column for each power of 10 * So we have: 5 * 1000 + 2 * 100 + 8 * 10 + 1 * 1 * Now let's move to the binary number system. * Consider this number: 110101 Starting at the right, we have: * a 1 in the 1's column * a 0 in the 2's column * a 1 in the 4's column * a 0 in the 8's column * a 1 in the 16's column * a 1 in the 32's column * Note: there is a separate column for each power of 2. So we have: 1*32 + 1*16 + 0*8 + 1*4 + 0*2 + 1 = 53 * Try a few more: * 1 1 1 1 = 1*8 + 1*4 +1*2 +1*1 = 15 * 0 1 0 1 = 0*8 + 1*4 + 0*2 + 1*1 = 5 * 1 0 1 1 1 = 1*16 + 0*8 + 1*4 + 1*2 + 1*1 = 23 ------------------------------------------------------------ Library Functions * Library Functions - Some functions are so useful that they have become standardized, and are available to every C programmer. These are called library functions. * There are actually several different libraries: * The "Standard Library" * Specialized Libraries: ex: * jpeg libraries * pdf libraries * mp3 libraries * We'll only cover functions from the standard library in this class. * Example: the absolute value function #include #include int main(void) { int x, y, z; x = -5; y = 10; z = abs(x) + 1; printf("%d %d %d\n", x, y, z); printf("%d\n", abs(x) ); printf("%d\n", abs(3 - 6) ); printf(%d\n", abs(z - y) ); printf(%d\n", abs(y - (x - z) ); printf(%d\n", abs(x - abs(x) ); } * Output of the above program: -5 10 6 5 3 4 21 10 * Explanation of the "#include " line at the top of the program: * Each standard library function is associated with a "standard header file"; * abs is associated with "stdlib.h" * The "#include" directive tells the compiler to get "stdlib.h" (it knows where to look) and insert it into the top of your program. * (Note: The brackets tell the compiler to look in the "/usr/include" directory for the stdlib.h file.) * The prototype for abs: int abs (int j); Tells you all you need to know about abs: * It has one parameter (an integer), and so the argument (constant, variable, or expression) should be an integer; * The return value is an integer. * Try this: % man abs All the standard library functions have "man pages", just like the shell commands. * another standard library function: pow * prototype: double pow(double x, double y); indicates: * 2 "double" parameters (thus 2 double arguments are needed in a call to pow) * return type is double * returns the value of x raised to the power of y. * requires: math.h #include <--- put this at top of your program * example: #include #include int main (void) { double x,y, z; x = 2.0; y = 10.10; z = pow(x,y); printf("z's value is: %.2f\n", z); } * A special note about math.h: If you need to include math.h, then you also need to do one more thing. When you compile, you have to use the "-lm" option. Example: * Assume your C program file name is pyth.c * Here's how to compile: % gcc -Wall -o pyth pyth.c -lm * Here's what "-lm" means: * -l means *link* * the "m" instructs the compiler to get a file called "libm.a" (it knows which directory to find this in.) * basic idea: link your program together with a file containing pre-compiled code for math operations. * the sqrt function: example: #include #include int main (void) { double x,y, z; x = 16.0; y = sqrt(x); printf("y's value is: %.2f\n", y); } ------------------------------------------------------------ The Switch Statement Example: switch (grade) { case 'A': printf("Excellent"); break; case 'B': printf("Good"); break; case 'C': printf("Not bad"); break; case 'D','F': printf("Sorry"); break; default: printf("Incorrect grade value"); } ------------------------------------------------------------