Saturday, May 26, 2012

Loops in C

    We've discussed conditional commands, which may or may not be performed, but loops allow commands to be repeated many times.  Interactive applications wait for user input, process the input, output the results, and repeat in a loop until the user quits.  Three types of loops found in C are the while loop, the do-while loop, and the for loop.

While loop:
    The syntax for this loop is the simplest and allows the other loops to be built from it.  The syntax is "while (logic test) {commands;}", where logic test and commands are replaced by real code.  If the logic test is true, the commands are executed and the logic test is again tested.  This continues until the logic test is false.

    The following example should print a statement three times, wait for user input, then exit:
#include <stdio.h>

//wait_key prototype
int wait_key();

int main() {
    int cycles_performed = 0;
    int cycles_to_do = 3;

    while (cycles_performed < cycles_to_do) {
        printf("cycle %d\n", cycles_performed);
        cycles_performed = cycles_performed + 1;
        //go back to logic test
    }

    return wait_key();
}

//wait_key definition
int wait_key() {
    getchar();
    return 0;
}
output in prompt:
cycle 0
cycle 1
cycle 2

    To demonstrate how this works, let's run through the code.  The variable cycles_performed starts at 0 and we enter the while loop.  0 < 3, so we print and add 1 to cycles_performed. 1 < 3 also, so we print again and add 1 to cycles_performed.  Finally, 2 < 3 so we print and add 1 to arrive at the logic test 3 < 3.  Since 3 < 3 is false, the loop is done and the next statement is return wait_key();.

    Were you to switch the order of the printing and adding one commands, the output would start from cycle 1 and go to cycle 3.  The print command is still called three times, but the variable can be updated before the print in that case.  One loop that demonstrates the order of commands well is the do-while loop.

Do-while loop:
    Similar to the written style of the while loop, the do-while loop requires commands and a logic test.  The syntax is "do {commands;} while (logic test);" and has one key difference when compared to the while loop: the test is performed after the commands.

    If the logic test for a while loop is false, the commands will not be performed.  However, a do-while loop guarantees that the commands will be performed at least once.  The following example shows a possible side-effect of this command order:
#include <stdio.h>

//wait_key prototype
int wait_key();

int main() {
    int number = 4;

    while (number == 5) { //test first
        printf("while loop cycle\n");
        number = number + 1;
    }

    do { //test after a cycle
        number = number + 1;
        printf("do-while loop cycle\n");
    } while (number == 5);

    return wait_key();
}

//wait_key definition
int wait_key() {
    getchar();
    return 0;
}
output in prompt:
do-while loop cycle
do-while loop cycle

    The commands in the while loop are never called because 4 != 5.  For the do-while loop though, one cycle is performed and number is increased to 5.  Since 5 == 5, another cycle is performed and the loop ends because number is now 6 and 6 != 5.

    The do-while loop is the only loop which tests after a first cycle through the commands.  The equivalent for a while loop would be "commands; while(logic test) {commands;}", so the do-while loop can save some typing if you want to ensure the commands run once.

for loop:
    The final loop to discuss is the for loop.  The syntax requires more than a logic test: "for (initial command; logic test; cycle command) {commands;}".  The initial command is run once before the loop starts and the cycle command is run at the end of each loop, before the logic test.

    In previous examples, variable = variable + 1 was used to make the logic test false after a number of cycles.  This command is often used in the for loop as the cycle command, and setting the variable's value before the loop can be done in the initial command.

    For example, for (number = 0; number < 3; number = number + 1) {printf("hello\n";} will set the integer number to zero once, print "hello" and a newline, add one to number, then try the logic test.  If the logic test is true, it will print "hello" again, add 1 to number, and try the logic test again.

    The following example shows a for loop with some additional features:
#include <stdio.h>

//wait_key prototype
int wait_key();

int main() {
    int number;

    /*without break or continue, this loop
    would print 20 times*/
    for (number = 0; number < 20; number += 1) {
        if (number == 5) continue;
        printf("cycle #%d\n", number);
        if (number == 10) break;
    }

    return wait_key();
}

//wait_key definition
int wait_key() {
    getchar();
    return 0;
}
output in prompt:
cycle #0
cycle #1
cycle #2
cycle #3
cycle #4
cycle #6
cycle #7
cycle #8
cycle #9
cycle #10


    This example attempts to show the commands continue and break.  The break command was used in switch statements to exit the switch statement.  For loops, break does the same thing; in this case, when number is 10, the loop is broken and the return wait_key(); command is called.  While loops and do-while loops can also use the break statement to exit.

    The continue command in a for loop means commands from that point are no longer called, the cycle command number += 1 is called, and the logic test is run.  Notice that cycle #5 is not printed: this is because continue is called before the print statement.  However, cycle #10 is present because break is called after the print command when number is 10.

    The command number += 1; is a shortened version of number = number + 1.  This applies to subtraction(-=), multiplication(*=), division(/=), modulo(%=), and bitwise operators.  If number == 7, then number *= 5 changes the value of number to 35 since this is the equivalent of number = number * 5 (or 7 * 5).

    For loops can be converted into while loops with the form "initial command; while(logic test) {commands; cycle command;}".  However, if you call continue in this while loop, the cycle command is not called because it is a while loop.

    For more information about loops in C, check the pages http://www.codingunit.com/c-tutorial-for-loop-while-loop-break-and-continue and http://www.cprogramming.com/tutorial/c/lesson3.html.

    The next post will be about structures and custom data types.  Thank you for reading :)

Sunday, May 20, 2012

Logic and Conditions in C

    In C, logic allow your commands to change if certain conditions are met, making them a bit smarter.  This tutorial will attempt to discuss how to use the logic found in C.  It will cover the if statement, logic operators, and the switch statement.

If and else statements:
    The most common way to use logic is the if statement.  This is present in other languages as well, but the syntax in C is "if (logic test) {commands;}".  The terms "logic test" and "commands" are replaced with your own code in this case.  Commands can be functions, arithmetic, printing, waiting, etc.

    The logic test will be either true or false.  If is is true, the commands will be executed; but if it is false, the commands are skipped entirely.  In order to create a statement which is true or false for numbers, use the following comparison tests: is equal to, is not equal to, is less than, is greater than, is less than or equal to, is greater than or equal to.

    These are comparison operators in C: ==, !=, <, >, <=, >= (listed in same order as mentioned before).  They require a number on both sides and give a true or false result.  For example, 5 == 7 gives us false, but 5 != 7 is true.  The following example shows how to use the if statement:
#include <stdio.h>

//wait_key prototype
int wait_key();

int main() {
    int x = 7, y = 5;

    if (x > y) {
        printf("x > y");
    }
    else {
        printf("x <= y");
    }

    return wait_key();
}

//wait_key definition
int wait_key() {
    getchar();
    return 0;
}
output in prompt:
x > y

    Here, x is greater than y and the command printf("x > y"); is performed.  However, the command printf("x <= y"); does not occur because the comparison test was true.  This is not all to this example though; if you were change the value of x to 3 and y to 8, the output would say x <= y.

    Else means the opposite of the comparison test.  If x is not greater than y, x can only be less than or equal to y.  In this case, else could have been replaced with if (x <= y) since > and <= are opposite tests.  < and >= are opposites tests too, as well as the == and != operators.

    If and else allow commands for two cases: the comparison and its opposite, but what about functions where three or more possibilities can occur?  For example, what about a function that says if input is 3 or 7 or another number?  The following example shows a function that does just that:
#include <stdio.h>

//wait_key prototype
int wait_key();

void checkInt(int input) {
    if (input == 3) {
        printf("input is 3\n");
    }
    else if (input == 7) {
        printf("input is 7\n");
    }
    else {printf("input not 3 nor 7\n");}
}

int main() {
    int a = 3, b = 5, c = 7;

    checkInt(a);
    checkInt(b);
    checkInt(c);

    return wait_key();
}

//wait_key definition
int wait_key() {
    getchar();
    return 0;
}
output in prompt:
input is 3
input not 3 nor 7
input is 7


    The keyword "else if" is the equivalent of another if statement.  The reason I do not just use if is that "else if" affects else: now else is for cases where input != 3 and input != 7.  The else statement has become more complex, but can still be replaced with an if statement.

Logical operators:
    To make logic more interesting, C has operators which use comparison tests as input and output true or false.  They can be placed inside an if statement to account for more possibilities.  The operators are logical and(&&), logical or(||), and logical not(!).

    The logical and and or operators use symbols similar to bitwise and and or.  This is no coincidence: the logical operators treat true as a 1 bit and false as 0.  Here is a table to show how logical or and and work:
comparison testcomparison testlogical and(&&)logical or(||)
(5 == 7) false(5 == 7) falsefalsefalse
(5 == 7) false(5 == 5) truefalsetrue
(5 == 5) true(5 == 7) falsefalsetrue
(5 == 5) true(5 == 5) truetruetrue


    To clarify, the commands within if ((5 == 7) && (5 == 5)) { will never be executed.  This table is very similar to the bitwise table from the operators post, http://multimediaprogram.blogspot.com/2012/05/c-operators.html, except that there is no logical xor.

    Logical not works much like bitwise not: !(5 == 5) is false and !(5 == 7) is true.  In fact, !(5 == 5) is the equivalent of (5 != 5).  The comparison test becomes the opposite, so !(x > y) becomes (x <= y).

    Here is an example of how to use these operators effectively: if ((x != 3) && (x != 7)) { can replace the else statement from the previous code example.  This is not limited to equalities though, you can also test if a number is within a certain range:
#include <stdio.h>

//wait_key prototype
int wait_key();

void checkRange(int input) {
    if (input > 0 && input <= 10) {
        printf("input is 1 to 10\n");
    }
    else {
        printf("input is not 1 to 10\n");
    }
}

int main() {
    int a = -1, b = 5, c = 7;

    checkRange(a);
    checkRange(b);
    checkRange(c);

    return wait_key();
}

//wait_key definition
int wait_key() {
    getchar();
    return 0;
}
output in prompt:
input is not 1 to 10
input is 1 to 10
input is 1 to 10


    The function checkRange will tell us whether a number is within the range 1 to 10.  This is accomplished with two comparison tests and logical and.  The else statement for this example would be if (!(input > 0 && input <= 10)) { which can also be if (input <= 0 || input > 10) { since these two are logically the same.

Switch statement:
    The last concept of logic to discuss is a switch statement.  The logic is pretty tame, but the written style can be messy.  The format starts with switch (variable) {, where variable is any variable you choose.  Next, there is case value:, where value is any value, which is followed by commands.

    One way to use this statement is to revisit the checkInt function, which checks for input equal to 3 or 7 or something else:
#include <stdio.h>

//wait_key prototype
int wait_key();

void checkInt(int input) {
    switch (input) {
    case 3: //if (input == 3)
        printf("input is 3\n");
        break;
    case 7: //else if (input == 7)
        printf("input is 7\n");
        break;
    default: //else
        printf("input not 3 nor 7\n");
        break;
    }
}

int main() {
    int a = 3, b = 5, c = 7;

    checkInt(a);
    checkInt(b);
    checkInt(c);

    return wait_key();
}

//wait_key definition
int wait_key() {
    getchar();
    return 0;
}
output in prompt:
input is 3
input not 3 nor 7
input is 7



    The goal of this example is to show that this checkInt and the previous version perform the same task.  The if, else if, and else are included as comments here, so they are not part of the switch statement.  These comments attempt show that the first case is basically the if statement if (variable == value) {.  Any additional cases are the else if statement else if (variable == value) {.

    Finally, default is the equivalent of else.  Each case performs similar commands found in the original checkInt, but with the added break command.  Break is a command which exits the switch statement after the commands of a case are performed and is necessary for a proper switch statement.

    For more information about the switch statement, check out the C programming tutorial http://www.cprogramming.com/tutorial/lesson5.html.  For more about if and logic, try the page http://www.cs.utah.edu/~zachary/computing/lessons/uces-11/uces-11/node9.html.

    The next tutorial will cover loops (with more examples of break).

Thursday, May 17, 2012

C Functions

    I imagine your programs will need much more code than used so far, especially for a working game.  However, one problem we face is a larger and unorganized main function.  Also, some commands must be performed outside of main, so custom functions will help a lot.  We will talk about output, input, and prototypes of functions.

output:
    You may remember from the C basics post, http://multimediaprogram.blogspot.com/2012/05/c-basics.html, a talk of how to declare functions.  To reiterate, the syntax to create a function which returns an integer and has no parameters is int functionName(), which is followed by a {, commands, and a closing }.

    Like in main, a function which returns an integer needs a return statement.  For main, return 0; indicates the end of main with a success (or the 0 code).  Here is an example of a custom function which returns five:
#include <stdio.h>

int five() {
    return 5;
}

int wait_key() {
    getchar();
    return 0;
}

int main() {
    int x = five();
    printf("x = %d\n", x);

    return wait_key();
}
output in prompt:
x = 5

    The function five shown above returns 5.  By changing the command return 5; to return 10; the function's output would become 10, but the function would still be named five.  The wait_key function has also been brought back and updated; here, it returns the exit code to main, which then returns the output of wait_key.

Input:
    As you have learned in math class, functions also have input.  Think of y = f(x), y is assigned the output of the function f and x is the input of the function.  To begin creating f, start with double f() { since real numbers are often used in mathematics.  Now that input is required, the declaration of the function will change: double f (double x) {.

    The added double x tells C that the function f requires one double as input.  When calling the function f, you do not need to initialize x, since this has already been done in the declaration; all that is required is a value to pass to x.  Here is an example of the function f declared and called:
#include <stdio.h>

/*function f requires one double
as input and returns one double*/
double f (double x) {
    double output;
    output = 2 * x + 1;
    return output;
}

//no input required, returns int 0
int wait_key() {
    getchar();
    return 0;
}

//the main function
int main() {
    double x = 7, y;
    y = f(x); //call function f

    printf("f(%lg) = %lg\n", x, y);

    //call wait_key and return output
    return wait_key();
}
output in prompt:
f(7) = 15


    There are two variables named x in this example: while C does not allow two variables of the same name, this only applies to variables of a given function.  The function main has variables x and y while f uses x and output, so x can exist in two different functions.

    Modifying x in f does not modify x in main, since they are two different variables.  In fact, passing the value of main's x to f's x and vice versa is also acceptable.  But what if we want one x that can be modified everywhere, without passing through functions?  We would use global variables.

    The following example is the same code above with a global variable called x:
#include <stdio.h>

//x is now recognized by all functions
double x;

double f () { //no need to pass a value
    double output;
    output = 2 * x + 1;
    return output;
}

int wait_key() {
    getchar();
    return 0;
}

int main() {
    double y;

    x = 7; //set value of x
    y = f();

    printf("f(%lg) = %lg\n", x, y);

    return wait_key();
}
output in prompt:
f(7) = 15


    Global variables are declared underneath the #include commands and before function declarations.  Some changes to the example include no initializing x in main or passing its value to f (which now has no parameters).  Since x is recognized by all functions, we cannot name a variable x in any functions.

    Optionally, the function f could modify the global variable x and the effect could be seen in the main function.  If the statement x = x + 1; were placed in f, main would recognize the change in x from 7 to 8 after the function f is called.

Prototypes:
    All of the functions so far have been declared before the main function.  This is because a function must be defined first before being used.  However, one work-around is to create a function prototype which is the output type, name, and input of a function without any of the commands (which are added later).

    The following example shows a few prototypes in action:
#include <stdio.h>

//function prototypes
double add (double, double);
int wait_key();

/*prototypes do not need names for the
input, just types.  The names can be
provided in the definition*/

int main () {
    double x = 5, y = 8;
    double z = add(x,y);

    printf("%lg + %lg = %lg\n", x, y, z);

    return wait_key();
}

//now functions can be defined after main

double add(double a, double b) {
    return a + b;
}

int wait_key() {
    getchar();
    return 0;
}  
output in prompt:
5 + 8 = 13


    The function add is first defined in the form of a prototype which uses variable types without names, then with a regular function definition.  The key difference between this example and previous ones is that function definitions for add and wait_key can be placed anywhere in relation to main.

    Also, the function add accepts two parameters which are separated by a comma.  You are free to define a function with any arbitrary number of parameters and commands, but there can only be one type of output (unless you count modifying global variables).

    For more information about functions, the link http://www.tutorialspoint.com/ansi_c/c_using_functions.htm can provide some insight.

    The next post will discuss conditions and logic.

Sunday, May 13, 2012

C operators

    One of the great things to do with numbers is mathematics.  Fortunately, C allows arithmetic to be performed on variables and provides some basic operators to perform simple arithmetic. There are also some less intuitive operators which make use of the binary version of a variable.  We'll begin with standard mathematics, discuss a cool operator called modulo, and finish with bitwise operators.

Arithmetic:
    In C, the operators for arithmetic work very similar to their real world counterparts.  For example, the + operator requires a number on the left and right and creates a number which is the sum of the left and right numbers.  The same applies to the - operator, but * is used for for multiplication and / for division.

    Here is an example to demonstrate some mathematical operations:
#include <stdio.h>

int main() {
    //declare variables
    int a = 5, b = 7, c;

    //create variables for real numbers
    double d = a, e = b, f;
    //here, d is the double version of a's value

    //add and place in c, print
    c = a + b;
    printf("%d + %d = %d\n", a, b, c);

    //subtract and place in c, print
    c = a - b;
    printf("%d - %d = %d\n", a, b, c);

    //multiply and place in c, print
    c = a * b;
    printf("%d * %d = %d\n", a, b, c);

    //integer divide and place in c, print
    c = a / b;
    printf("%d / %d = %d (int)\n", a, b, c);
    //integer divisions are always rounded down

    //real divide and place in f, print
    f = d / e;
    printf("%lg / %lg = %lg\n", d, e, f);
    //real divisions work with two real numbers

    getchar();
    return 0;
}
output in prompt:
5 + 7 = 12
5 - 7 = -2
5 * 7 = 35
5 / 7 = 0 (int)
5 / 7 = 0.714286


    Notice that the expression 5 / 7 creates different numbers for integers and doubles.  If both operands, 5 and 7 in this case, are integer types, the result of their division is rounded down to the nearest whole number.  However, if the operands are floats or doubles, the outcome will be of the same type and not rounded down.

    Doubles can also be initialized as an integer value in a variable.  This example provides d = a to show that double d can be given the value of a, except in real number form.  You can also use a = d to convert a real number d to an integer a, but like division, the result is rounded down.

Modulo:
    Another operator present in C is modulo.  In other languages, this operator is used with the word mod, but for C, the symbol % is used.  This operation applies to integers and returns the remainder of a division.

    In a sense, the remainder is the part lost when integer division is used instead of real division.  The remainder is the numerator of the fractional part of a division.  For example, the real division 7/5 equals the mixed number 1 + 2/5, so integer division 7/5 returns 1 and 7%5 returns 2 (from the 2/5 fraction).

    One interesting property of modulo is that it will produce a number closer to zero than the denominator.  A positive number modulo 5, for example, will produce a number from zero to four without exception.

    Also, if the positive number continues to increase by one, the positive number modulo 5 will loop through 0 to 4 repeatedly.  Here is a table to show the cycle:
numbernumber % 5calculations
000 / 5 = 0 + 0/5
111 / 5 = 0 + 1/5
222 / 5 = 0 + 2/5
333 / 5 = 0 + 3/5
444 / 5 = 0 + 4/5
505 / 5 = 1 + 0/5
616 / 5 = 1 + 1/5
727 / 5 = 1 + 2/5
838 / 5 = 1 + 3/5
949 / 5 = 1 + 4/5
10010/ 5 = 2 + 0/5

    Notice that the division increases from 0 to 2 during this cycle and the modulo loops through 0 to 4 twice then back to 0.  To wrap up discussion of modulo, here is an example using modulo 10:
#include <stdio.h>

int main() {
    //declare variables
    int number = 19;
    int ones_place, tens_place;

    /*for numbers greater than 0, modulo 10
    calculates the ones place*/
    ones_place = number % 10;
    /*for numbers between 0 and 99, division
    by 10 gets the tens place*/
    tens_place = number / 10;

    printf("for integer %d:\n", number);
    printf("%d/10 = %d\n", number, tens_place);
    printf("%d%%10 = %d\n", number, ones_place);
    //place the numbers side by side
    printf("combined = %d%d\n",
        tens_place, ones_place);
    printf("\n");

    //increase value of number by one
    number = number + 1;
    ones_place = number % 10;
    tens_place = number / 10;

    printf("for integer %d:\n", number);
    printf("%d/10 = %d\n", number, tens_place);
    printf("%d%%10 = %d\n", number, ones_place);
    printf("combined = %d%d\n",
        tens_place, ones_place);
    printf("\n");

    ones_place = 1;
    tens_place = 2;
    /*Here, the reverse calculation is performed
    to show that modulo allows integer division
    to be reversed*/
    number = 10 * tens_place + ones_place;

    printf("for integer %d:\n", number);
    printf("%d/10 = %d\n", number, tens_place);
    printf("%d%%10 = %d\n", number, ones_place);
    printf("combined = %d%d\n",
        tens_place, ones_place);

    getchar();
    return 0;
}
output in prompt:
for integer 19:
19/10 = 1
19%10 = 9
combined = 19

for integer 20:
20/10 = 2
20%10 = 0
combined = 20

for integer 21:
21/10 = 2
21%10 = 1
combined = 21

    This example aims to show that from 19 to 20, the division by ten increases by one while the modulo 10 loops back to zero.  Also, by multiplying the division by 10 (the number used to divide) and adding the modulo, we successfully rebuild the number being divided (21 in this case).

Bitwise Operators:
    If you are not familiar with binary, here is my attempt to explain it: counting using only ones and zeros.  I will elaborate, but to spare you, the reader, some time, a better explanation can be found on the page http://mathworld.wolfram.com/Binary.html.

    Start at zero, the binary representation is 0.  Now jump to one, which has binary representation 1.  This is beginning to look easy, but now we are at 2 and can only use 1 and 0.  The binary form of 2 is 10.

    Think of binary as a sum of powers of two: for two, 2 = 1 * 2^1 + 0 * 2^0 = 2 + 0 * 1 = 2.  So three will equal 11 because 3 = 1 * 2^1 + 1 * 2^0 = 2 + 1 = 3.  What about four? 4 = 2^2 = 1 * 2^2 + 0 * 2^1 + 0 * 2^0, so in binary 100.  Here is a table of 0 to 7:
decimalbinarysum of powers of two
00000*4 + 0*2 + 0*1
10010*4 + 0*2 + 1*1
20100*4 + 1*2 + 0*1
30110*4 + 1*2 + 1*1
41001*4 + 0*2 + 0*1
51011*4 + 0*2 + 1*1
61101*4 + 1*2 + 0*1
71111*4 + 1*2 + 1*1

    This table shows the eight numbers which can be represented with three bits; the next number is 8 with binary form 1000 which requires the four bits 1, 0, 0, and 0.  A bit can only be one or zero.

    Note that 2^3 = 8 and that there are eight numbers on the previous table, each with three bits.  Since 2^4 = 16, that means sixteen numbers can be made using four bits.  A char in C is 8 bits which means 2^8 = 256 possible numbers.  Since this is the smallest size in bits of the variables types, we will work with char.

    Now onto operators!  There are only six operators to discuss now: not, and, or, xor, bit-shift left, and bit-shift right.  Their symbols in C are ~, &, |, ^, <<, and >> respectively.

    Not is a simple one: every bit of the character becomes the opposite of before.  If you perform ~31, the output is -32 because 31 = 00011111 and ~31 = 11100000.  The reason it is negative is because signed numbers use a negative power of two for the highest power.  In this case, -128 + 64 + 32 = -32.  Were the character unsigned, it would be +128 + 64 + 32 = 224.

    For and, or, and xor, two chars are required and the following table will help explain what they do to each bit:
bit in charbit in otherand(&)or(|)xor(^)
00000
01011
10011
11110

    As an example,
3 & 6 = 00000011 & 00000110 = 00000010 = 2,
3 | 6 = 00000011 | 00000110 = 00000111 = 7, and
3 ^ 6 = 00000011 ^ 00000110 = 00000101 = 5

    Finally, bit-shifting operates on a single char variable by moving all of the bits left or right a specified amount of times.  To shift 5's bits left once, use 5 << 1, which equals 00000101 << 1 = 00001010 = 10.  The bits now correspond to higher powers of two, so shifting once is the rough equivalent of multiplying by two.

    Now, to bit-shift right twice, 10 >> 2 = 00001010 >> 2 = 00000010 = 2.  In this case, a one bit is lost.  This is similar to 10 / 4 which also equals 2 because it rounds down.  Here is an example which serves as a review for bitwise operators:
#include <stdio.h>

int main() {
    char a = 3, b = 6;
    char c = 31;

    printf("bitwise operators:\n");

    printf("\n");

    //and, or, and xor of a and b
    printf("%d & %d = %d\n", a, b, a & b);
    printf("%d | %d = %d\n", a, b, a | b);
    printf("%d ^ %d = %d\n", a, b, a ^ b);

    printf("\n");

    //not and bit-shift examples
    printf("~%d = %d\n", c, ~c);
    //similar to multiplication by four
    printf("%d << 2 = %d\n", c, c << 2);
    //similar to integer division by two
    printf("%d >> 1 = %d\n", c, c >> 1);

    getchar();
    return 0;
}
output in prompt:
bitwise operators:

3 & 6 = 2
3 | 6 = 7
3 ^ 6 = 5

~31 = -32
31 << 2 = 124
31 >> 1 = 15




    For more information about operators, view the page http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/V40F_HTML/AQTLTBTE/DOCU_059.HTM.

    The next post will cover how to make custom functions.

Monday, May 7, 2012

Variables in C

    Before I begin improvising with this tutorial and possibly misinforming everyone reading, I recommend you, the reader, check out this site for its well-crafted C tutorials: http://www.cprogramming.com/tutorial.html.  Also, I will try to better structure my tutorials to make them more readable.

    Now, onto the topic of variables:

Creating:
    In order to create a variable, a type and name must be specified.  In this case, let's use the type int since it is the type the main function returns.  The term int is an abbreviation for integer which is a whole number (such as 0).

    The name can be anything, like a, b, apple, this_is_a_variable, etc.  However, note the lack of spaces because, like function names, variable names cannot have spaces in them.  Also, variables cannot have the name of existing variables or functions.

    The command to create integers called x and y should look something like int x, y; and be placed at the top of a program or function.  This means you cannot create a new variable in the middle of a function.  This is unfortunately part of the C89 standard and therefore inflexible.

Setting:
    To give this variable x a value of 5, simply add the command x = 5;  after the variable declaration.  Alternatively, the composite command int x = 5, y; works as both a creation of the integer x and an assignment of it's value to 5.

    In this case, the equals sign does not mean x must equal 5, rather x is given the value of 5 until changed.  By declaring x = 6 after these commands, the value of x is now 6 and the 5 is gone.  Variables can also be set by other variables, like the statement y = x; indicates.

Printing:
    By printing the value of a variable to the prompt, we ensure that the result of these commands is made visible.  To print, the function printf will be used and another special sequence of characters introduced.

    To print x, use the command printf("%d", x); after the declaration and assignment of x.  The %d in the quotes will not be printed because it indicates a placeholder for a decimal integer.  This placeholder is filled by the next parameter after the character string, x.

    This will only print the value of x though.  To give it a bit more context, the command printf("x: %d\n", x); will print x: 5 in this case.  The symbols after x may look a bit messy, but remember that %d is for printing the integer and \n is to start a new line.

    Here is code to demonstrate these concepts in action:
#include <stdio.h>

int main() {
    //declare integers x and y
    int x = 5, y;

    //re-assign value of x to 6
    x = 6;

    //display value of x
    printf("x: %d\n", x);


    //assign to value of y the value of x
    y = x;


    //display value of y
    printf("y: %d\n", y);

    //wait for key press, exit
    getchar();
    return 0;
}
output in prompt:
x: 6
y: 6



    Remember to set the value of a variable before using it since an uninitialized variable tends to be a random value every time the program runs.

Types:
    In addition to integers, there exist other types of variables, all of which hold number values.  Here is a table of their various properties:
namerangesize
int (signed)-2,147,483,648 to +2,147,483,6474 bytes
unsigned int0 to +4,294,967,2964 bytes
short (signed)-32,768 to +32,7672 bytes
unsigned short0 to +65,5352 bytes
char (signed)-128 to +1271 byte
unsigned char0 to +2551 byte
float1.1754943508222875079687365372222e-38 to
3.4028234663852885981170418348452e+38
4 bytes
double2.2250738585072013830902327173324e-308 to
1.797693134862315708145274237317e+308
8 bytes

    This table does not cover all data types available under C and may be inaccurate on certain types of computers.  To verify that a size in the table is the same for your system or compiler, use the command sizeof with a data type: printf("sizeof int = %d", sizeof(int)); for example should output sizeof int = 4 to the prompt.  If there is a discrepancy, the size of a data type tends to correspond to its range of numbers.

    The float and double are the real number data types; they store whole numbers as well as numbers with fractions.  The ranges provided may be misleading: both are capable of negative and positive numbers, so the smaller range is how close the number can get to zero (before becoming zero) and the larger range is how far away it can get from zero (before becoming + or - infinity).

    Now that we have new data types to work with, we must know how to print them.  Luckily, the only new special characters for printf are %u for all unsigned types, %d for all signed types, %g for floats, and %lg for doubles.

    To demonstrate variable types and printing in action, here is an example which prints variables of the mentioned types:
#include <stdio.h>

int main() {
    //declare variables
    double real_number = -4.5;
    unsigned int four_billion = 4000000000;
    float pi = 3.1415927;
    short neg_thirty_thousand = -30000;

    //print in different formats
    printf("real_number = %g\n", real_number);
    printf("four_billion = %u\n", four_billion);
    printf("pi = %g\n", pi);
    printf("neg_thirty_thousand = %d\n",
           neg_thirty_thousand);

    printf("\n"); //blank line

    //print sizes (for signed and unsigned)
    printf("sizes of data types:\n");
    printf("integer: %u\n", sizeof(int));
    printf("short int: %u\n", sizeof(short));
    printf("character: %u\n", sizeof(char));

    //sizeof works for variables and types
    printf("float: %u\n", sizeof(pi));
    printf("double: %u\n", sizeof(double));

    getchar();
    return 0;
}
output in prompt:
real_number = -4.5
four_billion = 4000000000
pi = 3.14159
neg_thirty_thousand = -30000

sizes of data types:
integer: 4
short int: 2
character: 1
float: 4
double: 8


    To find out more about special character sequences for printf, check out the page http://www.dgp.toronto.edu/~ajr/209/notes/printf.html.  For more information on data types found in C, read the website http://www.lix.polytechnique.fr/~liberti/public/computing/prog/c/C/CONCEPT/data_types.html.

    The next post will discuss operators and arithmetic.

Friday, May 4, 2012

C Basics

    Now that the C compiler is functioning, it's time to start building and understanding programs in the C language.  To begin, let us examine the program from the previous post:
#include <stdio.h>

int main() {
    printf("C compiler working");

    getchar();
    return 0;
}


    First, the command #include <stdio.h> will read the file stdio.h which is a reference to the C Standard Input and Output library (or cstdio) and has helpful functions for printing text output and reading user input.  More information about stdio.h can be found at http://www.cplusplus.com/reference/clibrary/cstdio/ which also discusses its helpful file handling functions.  For this program, only the functions printf and getchar were used from stdio.h.

    Next, the line int main() { declares a function called main with no parameters that returns an int, or integer.  Functions are free to return any type of data, have an arbitrary number of various parameters, and have any name; but main is reserved for the function where the program will first start performing commands in order.  Every C program must therefore have exactly one main function.  Also, the { symbol indicates the start of the sequence of commands which will be part of the main function.

    The command printf reads a string of characters from its parameter list and outputs it to the terminal or command prompt.  Upper case C and space are examples of individual characters.  Also, the command getchar waits until the user types any character from the keyboard then does nothing.

    Finally, the command return 0; ensures that the main function returns a zero to indicate a successful operation. The file closes with a } to complement the { from before and close the declaration of the main function.

    The semi-colons at the end of each command separates them from one another since C will ignore most white spaces(tabs, spaces, returns, etc.) in your file.  This means main could be written as int main(){printf("C compiler working");getchar();return 0;} but writing code that can be read by yourself and fellow humans is recommended.

    Now, to become more familiar with C syntax, here are some modifications to the program:
// use functions from cstdio 
#include <stdio.h>

//This line is a comment ignored by C
//This is because it begins with //

/*
For comments consisting of many lines,
it is best to place a /* at the
beginning and end the comment lines
with a * and / as seen here.
*/

//say_hello commands
void say_hello() {
    printf("C compiler working\n");
    return; //returns nothing
}

//wait_key commands
void wait_key() {
    printf("Press any key to exit\n");
    getchar();
    /* void functions don't require
    a return command, but it can be
    used to stop a function and
    return to where it was called */
}

//program starts here
int main() {
    //go to say_hello commands
    say_hello();

    //go to wait_key commands
    wait_key();

    //exit program successfully
    return 0;
}

    This example introduces comments which are also ignored by C.  Comments are intended to communicate the purpose of your code to people who read them.  It is highly recommended that programs contain comments because it helps others understand the code, but can also help the author if he or she revisits a long forgotten program and needs a quick review.

    This example also shows two new functions: say_hello and wait_key.  The names use underscores since C does not allow spaces in function names.  Both return nothing, which is indicated by the word void, and require no parameters.  An additional printf was called to tell the user that a key press is needed to exit.  Notice that these functions are declared before main is declared.  This is because main must only call functions which have already been defined.

    Both printf calls now have a \n character in the parameter to create a return or enter character.  That was not a typo; the "newline" character consists of \ and n.  The \ tells C that the next character will use its secondary meaning, so the character n has a second meaning as the end of a line and the start of a new line.  This also works for \" since placing " anywhere in the string of characters would mean the end of the string, while \" is used to place a " character in the string.  Try placing quotes around compiler as practice.

    The program begins at main, and the first command is to call the function say_hello.  The function say_hello calls printf and returns control of the program to main.  The program then returns to main and calls wait_key.  Function wait_key calls printf and getchar and finishes.  Control of the function returns to main again and finally return 0 is called to exit the program.

    The next post will discuss variables, especially integers.

Wednesday, May 2, 2012

Preparing to use C89

    The first step to building C programs is to obtain a C compiler.  There are several popular compilers, including the GNU C Compiler and Microsoft Visual C++, but the first step get a compiler is to identify your operating system:

If you are using Linux:
    Good for you.  To test whether you have GCC, open the terminal and type the command "gcc".  If a lot of text about how to use gcc is displayed, chances are you have gcc already.  If an error is returned (probably similar to "gcc is not a recognized command"), try the command "yum install gcc", use a package manager to install either "build-essentials" or "Development Tools", check the pages http://gcc.gnu.org/install/configure.html and http://www.faqs.org/docs/ldev/0130091154_71.htm for possible help, or google how to install gcc on your specific Linux distribution.  If you are using Ubuntu specifically, check the page http://www.shibuvarkala.com/2009/03/how-to-install-gcc-cc-compiler-in.html for help.  Also, there are Integrated Development Environments which make programming much easier, such as Code::Blocks (http://www.codeblocks.org/) or Eclipse CDT (http://www.eclipse.org/cdt/), but these are optional.

If you are using Macintosh:
    Like Linux, you can test whether you have a working version of GCC by opening the terminal and typing "gcc".  If a lot of text about how to use gcc is displayed, you most likely already have gcc.  If gcc is not recognized as a command, you may want to install the Integrated Development Environment xcode if you are using some version of Mac OS X (or possibly a C package for xcode if you already have xcode).  information about xcode can be found here: https://developer.apple.com/xcode/.  It has a lot of helpful features and I have heard mostly positive comments about xcode.  There are several ways to get gcc without xcode, one way can be found here: https://github.com/kennethreitz/osx-gcc-installer.

If you are using Windows:
     Microsoft offers a free, express version of its Visual Studio Compiler and Integrated Development Environment here: http://www.microsoft.com/visualstudio/en-us/products/2010-editions/visual-cpp-express.  Currently, the newest non-beta version is 2010.  To obtain Microsoft Visual C++ Express, you may need to create or use a windows live or hotmail account (which are also free).  Alternatively, you can obtain a Windows version of gcc by installing mingw(http://www.mingw.org/) which can optionally be used with an Integrated Development Environment like those mentioned for Linux: Code::Blocks (http://www.codeblocks.org/) or Eclipse CDT (http://www.eclipse.org/cdt/).

    Now, to test the C compiler, here is a simple C program which prints the phrase "C compiler working" and exits when a key is pressed:
#include <stdio.h>

int main() {
    printf("C compiler working");

    getchar();
    return 0;
}

    The coloring used in the example is meant to simulate what a IDE will do.  Various IDE's use different colors for parts of a program: commands with # before them, things in quotes, words recognized by C (like int and return), written numbers, and several others.

    Place the code above into a text file and change the extension to .c (for example, create a file test.c and save the code there).  If you are using an IDE, you can create the file in a project and copy the code into the open file.  Also, there should be a compile button and a run button for IDE's, so click both (in order) and the program should run.

    If you are not using an IDE, use the terminal or command prompt to change the current directory to the location you saved the file with the code (using the cd command).  To compile, simply type gcc <file name here>.c -o <program name and extension here>, where program name can be any name (except file name).

Introduction

    Hello and welcome to The partially-factual Multimedia Programming Blog.  You may ask "why is it partially-factual?"; this is because the information I provide will most likely be my own interpretation of certain aspects of programming, which may be almost correct or even completely wrong.

    The topics of multimedia programming I will discuss are only those which I have learned and applied, including: simple programming with C89, the basics of SDL, using OpenGL 2.1 and GLSL 1.0, using OpenAL 1.1, and various types of multimedia files.

    With that out of the way, here are some fundamental questions:

Who is this blog for?:
    Well me of course, silly.  However, it is open to anyone who wants to begin learning how to make programs with multimedia components (video games, picture editors, sound creators, etc.).  Also, it will help me review when I forget something, so I'll also leave it open to anyone who needs a quick review.  Actually, I'll just leave it open to anyone since it is accessible mostly everywhere.

What topics will be discussed?:
    I believe this was already mentioned, but if you must know, I will discuss how to make simple memory- and file- modifying programs with the C programming language, 1989 standard (which is probably outdated).  Then I will talk about SDL, a library which can already do most of what you need for multimedia programming (this does require header, library, and DLL files for Windows).  Finally, I will cover OpenGL and OpenAL, the open graphics and audio libraries (respectively), which make the transition to 3D applications much easier.  Also, I may continue to discuss more topics as time progresses.

    For the next post, I will begin discussion of C programs.