// Demonstrate various high-level syntax for the C language // ECE2036 // George F. Riley, Georgia Tech, Fall 2012 // Comments in C and C++ can be entered using the "slash-slash" double // character as done here. In this type of comment, the comment is // only to the end of the current line. /* Another way to include comments is to open a comment block with the slash-star double character, and terminate it with the star-slash double character as done here */ // Nearly all C and C++ programs start with "includes" which tell the // compiler to find some other file (in this example it is stdio.h) // and insert all of the text found in that file into this compilation // unit. Normally, the contents of the ".h" (header) file only contain // function prototypes, not the actual implementation. More on this // later. #include // In C and C++ variables must be "declared" before they can be used. // Declaring a variable is simple, just say the "type" of the variable // followed by the variable name. Below defines three variables each // of a different type. The three types used below (int, char, double) // are part of the "built-in" types defined by the language. There // are many built-in types, but for now we will concentrate on just // these three. There are also ways to define new types. // In the example below, we assigned an initial value to intVariable // and left the others "uninitialized" int intVariable = 10; // Define an integer variable (32 or 64 bit) char byteVariable; // Define a singe 8-bit byte variable double floatVariable; // Define an 8-byte "floating point" variable // A "function prototype" is a way to tell the compiler of the existence // of a particular "function" (some languages call this a "subroutine") // without actually providing the implementation of the function. // C and C++ differ on the requirements for this. The C language // allows functions to be called without prototypes, but C++ does not. // In 2036 we will always use the C++ compiler, so we need to // include prototypes. Actually, this is not completely true as will // be explained later. // Below we provide a function prototype for a function called "func1" // that accepts two arguments (an integer and a double) and returns // a computed value of type double. Note the trailing semicolon below. double func1(int arg1, double arg2); // Below is the implementation of a function called func2 that accepts // two arguments and returns an integer. Notice the difference here // as compared to func1; here the function is actually implemented. // Here is a case where a prototype is not necessary, as the function // is not actually called prior to the function being defined. // Note the use of the "open curly" and "close curly", as well as // "return" statement. "return" is a reserved word in C and C++ and // cannot be used for something else. int func2(int arg1, int arg2) { // Compute arg1 times arg2 and return the product return arg1 * arg2; } // Below defines and implements "func3", which illustrates the use // of a "pointer" in C/C++. A pointer is a variable just like any // other C/C++ variable, but the difference is the the VALUE of the // pointer is the address of some other variable somewhere in memory. // func3 also illustrates "de-referencing" the variable using the "star" // operator. Finally, func3 illustrates the "void" return type, which // indicates the function does not actually compute a return value. void func3(int* pInt1, // pInt1 is a "pointer" int int2) // int2 is a normal integer { // The open-curly starts the implementation of the func3 function // The next line says to take the value found in int2 and store it // in whatever address is found in pInt1. Pointers are used extensively // in C and C++ *pInt1 = int2; } // C and C++ allow "defined" constants, as illustrated below. // K2Length is defined as 20 and will be used later in the main. #define K2Length 20 // Below illustrates the definition of a C/C++ "structure". // A structure is a way to state that a single variable has // multiple "sub-variables". In the example below we have // a structure called "myStruct" with subvariables a, b, and c. // It is important to note that the typedef DOES NOT define a variable. // It simply defines a type (analagous to int, char, etc); variables // of that type must later be declared as is shown below in the // main function typedef struct { // myStruct has three subvariables (or components). // Note the components can be different types, but don't have to be int a; char b; double c; } myStruct; // All C and C++ programs start with a function called "main" that // returns an integer (generally ignored) and accepts two arguments. // The two arguments "argc" and "argv" are the count of the command // line arguments entered when the program was started, and an array // containing the actual arguments. We will discuss this in more // detail later in the class. int main(int argc, char** argv) { // Functions often declare "local variables" that exist only while // the function is being executed. Here we define several // local variables including an integer array. // Note the open-close square braces indicating // an array variable. The size of the array (using this syntax) must // be known at compile time. The array will be accessed a few // lines later. int i = 5; // declare local variable i and initialize to int j; // declare local varialbe j and leave uninitialized. int k[10]; // k is an array of 10 integer values, uninitialized int k2[K2Length]; // k2 is an array of 20 integer values, uninitialized double d = 1.5; // d is a single 8-byte floating point value. // Declare a variable of type myStruct. // THis is referenced later. myStruct mySt; // Initialize the sub-variables in mySt mySt.a = 1; mySt.b = 'C'; // Note the character constant with single quotes mySt.c = 2.0; // Both C and C++ make extensive use of "for loops". // In this example, we initialize the k array to known values. // We also use a local variable "i1" as the loop variable, which // has a lifetime only within the loop. This is common and good practice. // Finally note the use of the "++" operator, which in this case // essentially says to set variable i1 to i1 + 1. for (int i1 = 0; i1 < 10; i1++) { // the open curly brace indicates the start of the code repeated // by the for loop k[i1] = i1; // Note the array reference with square brackets // printf is one way to print things to the console window. // In C++ we generally use a different way using "cout" // discussed later in class. printf("Initialized k[%d] to %d\n", k[i1], i1); } // Illustrate another for loop iterating over k2; this loop is slightly // different, but much better (Why?) for (int i2 = 0; i2 < K2Length; i2++) { // the open curly brace indicates the start of the code repeated // by the for loop k2[i2] = i2; printf("Initialized k2[%d] to %d\n", k2[i2], i2); } // Note the below line of code won't comple. Why? // j = i2; // Illustrate "calling" a function, in this case func3. Note use of the // "address of" operator "&". The value of the first argument to func3 // is not the value "j" but the address in memory of j. func3(&j, i); printf("j is %d\n", j); // What do you think is printed here. // Illustrate calling a function in an expression. i = i + func2(10, 20); printf("i is %d\n", i); // What do you think is printed here. //Illustrate calling func1 even though func1 has not yet been // implemented. d = k2[1] + func1(20, 10); printf("d is %f\n", d); // What do you think is printed here. // Since the "main" function is declared to return an integer // we return 0. For main this is often omitted. return 0; // Notice you can legally put more code after a return statement // but of course that code is never executed printf("Should not be printed\n"); } // Now provide the implementation of func1 double func1(int arg1, double arg2) { // just return the quotient return arg1 / arg2; }