Review of C and C++


Prerequisites for this course are familiarity with C and Matlab. Most of the advanced features of C++ that will be used are hidden through the library that you will be using (though it is highly suggested that you look at its inner workings for your own personal benefit). Most of the code that you will be writing will use regular C syntax, and you won't need to write your own classes or anything of that sort. In some later labs, it will be useful to use the C++ Standard Template Library. Directions for its use will be provided then. This page contains a review on the following topics:

Basic Syntax

All functions start with the name of the function followed by input arguments passed to that function encompassed in parentheses. The function main signifies where the program starts. Most of the programming that you will be doing will be done in the main function. Within main you can call upon other functions that you've included with the #include sign or that you've simply included with the code. Commenting code is done by preceding the comment with /* and ending the commment with */. Note also that comments are provided explaining each statement. The only thing that you have to worry about in this first lab is inserting some simple lines of code where it says /* insert your code here */.


First of all, functions must have a type (i.e. void, int, double, etc.). The type of the function specifies the type of the value returned by the function. For example, say you have a function named Sum that will return an integer that is the sum of the two integers parameters it is called with. Then the declaration of this function would be:

int Sum (int foo, int bar)
    int result = foo + bar;
    return (result);

After all declarations for variables have been made within the braces, the programming begins. The reader is assumed to have knowledge of basic programming loops, if constructs, etc. from a basic programming course. All statements in C must be ended with a semicolon as can be seen in the body of the skeleton program.

For Loops

The statements that will be of use to you in this lab will be the following:

All for loops come in the following format:

for (expression1;expression2;expression3)
    // do something

The expression1 specifies the initial condition of the loop (e.g. i=0). The expression2 specifies the exit condition of the loop (e.g. i < 512). The expression3 specifies the increment condition (i.e. i++, which means to increment the variable i by 1, likewise, i-- decrements i by 1). After the conditions are specified, the body of the for loop, which is contained within the braces will be executed. So, let's say we have a loop,

int j = 0;
for (int i=0; i<512; i++)

Then the variable j will add 1 to it each time until i has reached 511 at which time it will exit the for loop.


In order to print a variable you will use the cout function. This function's use is fairly intuitive. The following code will print "The variable x is 5".

int x = 5;
cout << "The variable x is " << x << endl;

Note that the variable endl ends the line that is being printed on and scrolls down one.

Command-Line Arguments (Optional)

In your skeleton program, the function main takes two input arguments (int argc, char* argv[]). The first of these is one plus the number of the arguments passed to the program. The second of these is an array of strings with argc elements. The first element of this array, argv[0], is the command that was typed to invoke the program. For each i greater than zero, argv[i] is the ith parameter entered on the command line when the program was invoked. So if you type in:

./lab1 in.png out.png

Then argc will be 3 and argv will be {"./lab1", "in.png", "out.png"}. In most of the labs, you will not need to alter how the command-line arguments are handled.