CMPT 115 C tutorial for students who took 111 in Java Mark G. Eramian Ian McQuillan University of Saskatchewan Mark G. Eramian, Ian McQuillan CMPT 115 1/32
Part I Starting out Mark G. Eramian, Ian McQuillan CMPT 115 2/32
Outline 1 C Mark G. Eramian, Ian McQuillan CMPT 115 3/32
Overview: C vs. Java Java is an Object Oriented Language. C uses a different programming paradigm called procedural programming. There are no objects in C. Only built-in data types and the facility to build other data types using built-in types as building blocks. C doesn t allow for packaging of code and data. There are no methods, only functions which exist as separate entities. Mark G. Eramian, Ian McQuillan CMPT 115 4/32
C vs. Java C is a compiled language. After compilation, you get machine code which is run directly on the hardware (machine dependent). Compiling Java code creates bytecode, which gets run on the Java Virtual Machine (which then gets converted to machine code by the JVM). Mark G. Eramian, Ian McQuillan CMPT 115 5/32
Our first program Here is our first program. Type it into an editor and save the contents in a file called hello.c. #include <stdio.h> /* Our first C Program */ int main(void) { printf("hello world!\n"); Mark G. Eramian, Ian McQuillan CMPT 115 6/32
Compiling and Running Open the command prompt and type the following: > gcc -Wall hello.c > ls a.out* hello.c >./a.out Hello world! > gcc -Wall -o hello hello.c > ls a.out* hello* hello.c >./hello Hello world! > Mark G. Eramian, Ian McQuillan CMPT 115 7/32
Variables Primitive types are identical in C and Java. There are int, float, char, double, etc.. Also, void is used when there is no return type. Assignment into primitive types is the same. int i; char c; i = 1; c = c ; Mark G. Eramian, Ian McQuillan CMPT 115 8/32
Boolean With one exception: there is no boolean type. People use int, and interpret 1 as true and 0 as false. int i; i = (1 == 1); printf("the integer %d is true ",i); i = (1 == 0); printf("the integer %d is false ",i); The first statment prints 1 while the second prints 0. Mark G. Eramian, Ian McQuillan CMPT 115 9/32
To note In C, all variables must be declared at the start of a function, before any executable code. /* OK */ int main(void) { int k = 1, l = 10; char c = X ; for(k=0; i < j; i++) { printf("%i\n", i); printf("%c\n", c); /* ILLEGAL! Compiler error. */ int main(void) { int j = 10; for(int i=0; i < j; i++) { printf("%i\n", i); char c = X ; printf("%c\n", c); Printing and reading from the command line will be dealt with extensively in lab 1. Mark G. Eramian, Ian McQuillan CMPT 115 10/32
Similarities Arithmetic operators +, -, *, /, % are identical. Conditionals >, <, >=, <=, ==,!= are identical. For loops are identical, but you have to watch out that you declare the counter at the top of the function instead of in the for loop (variables must be declared before executable code. int main(void){ int i, x = 100; for(i = 0; i < x; i++){ printf("i is %d ",i); Mark G. Eramian, Ian McQuillan CMPT 115 11/32
Similarities While loops are the same, as are do-while loops int i = 0, end = 50; while(i < 50){ //do something i++; do{ //do something i--; while(i >= 0); Mark G. Eramian, Ian McQuillan CMPT 115 12/32
Arrays Arrays are pretty much the same as in Java. int a[50]; // Declares an array of 50 ints char s[100]; // Declares an array of 100 characters int n[] = {1, 2, 3, 4, 5; //declares and initializes an array int main(void) { int Array[10], i; for(i=0; i < 10; i++) { Array[i] = i*i; Arrays will be studied in more depth in topic 3 of the notes. Mark G. Eramian, Ian McQuillan CMPT 115 13/32
Strings There are no strings in C. Only character arrays. There are many functions available in pre-made libraries to help with standard string operations. They will be dealt with in Lab 2. Mark G. Eramian, Ian McQuillan CMPT 115 14/32
Compound data types C allows you to combine basic data types together using something called a struct. A struct is like a Java class, except it contains only data (ie. the instance variables), but no methods. struct tagname { int x; int y; ; We can now declare a variable whose type is this structure: struct tagname point; The attributes of a struct are similar to instance variables in Java. Mark G. Eramian, Ian McQuillan CMPT 115 15/32
Compound data types We can access the fields of struct tagname using the. operator: struct tagname point; point.x = 10; point.y = 20; Mark G. Eramian, Ian McQuillan CMPT 115 16/32
Compound data types This pretty much worked the same in Java, but usually the instance variables were set to private meaning you needed to use instance methods to retrieve and manipulate the attributes. For example, you might write a method in the point class (in Java) called setx as follows; void setx(int i){ x = i; point.setx(10); Here we just manipulate the attributes directly with point.x = 10;. Mark G. Eramian, Ian McQuillan CMPT 115 17/32
Compound data types We can give better names to struct s using type definitions. A type definition is given as: typedef <known_type> <new type> Example: typedef struct optional_tagname { int x; int y; Point;... Point p; p.x = 10; p.y = 20; Mark G. Eramian, Ian McQuillan CMPT 115 18/32
Nested structures We can even have nested structures (just like in Java where in one class we could have instance variables of another class). typedef struct{ int x; int y; Point; typedef struct{ Point start; Point end; Line; int main(void){ Line newline; newline.start.x = 0; newline.start.y = 0; newline.end.x = 5; newline.end.y = 5; Mark G. Eramian, Ian McQuillan CMPT 115 19/32
Functions C functions look very much like Java methods. There is the function header, followed by the body of the function in curly braces. Syntax: <return type> <name>(arg1, arg2,...) Functions can have 0 or more arguments. If no return type is specified, the default is int. If no arguments, use void. Mark G. Eramian, Ian McQuillan CMPT 115 20/32
Functions - an example #include <stdio.h> int exp( int n, int m ){ int count = 0, answer = 1; for (count = 0; count < m; count++){ answer = answer * n; return answer; int main(void){ int exponent = 0, answer = 0; while (exponent < 20){ answer = exp(2,exponent); printf("two to the power of %d is %d \n", exponent, answer); exponent++; Mark G. Eramian, Ian McQuillan CMPT 115 21/32
Functions - Declarations vs Definitions A function must be declared before it is called. That is why the main function is at the bottom (it is calling on other functions). There are ways around that (we will go through this in class as well). A function declaration consists of just the function header, (also called a prototype), followed by a semicolon: int fib(int n); Once the function is declared, it can be called in other functions, even if the function body hasn t be defined yet. A function definition is the header plus the body. Mark G. Eramian, Ian McQuillan CMPT 115 22/32
An example #include <stdio.h> int exp(int n, int m); //function declaration int main(void){ int exponent = 0, answer = 0; while (exponent < 20){ answer = exp(2,exponent); printf("two to the power of %d is %d \n", exponent, answer); exponent++; int exp( int n, int m ){ //function definition int count = 0, answer = 1; for (count = 0; count < m; count++){ answer = answer * n; return answer; Mark G. Eramian, Ian McQuillan CMPT 115 23/32
Anatomy of a C program Include header files (yours or library headers). Make a habit to include these headers: #include <stdio.h> #include <stdlib.h> #include <string.h> Declare types/structs/constants. Declare and define functions. Declare and define main(). Mark G. Eramian, Ian McQuillan CMPT 115 24/32
Translate Java to C Take a look at the files Point.java and Ian.java for a java implementation of simple program which implements a point class, and uses a few methods from it. We will try to translate this to C. Mark G. Eramian, Ian McQuillan CMPT 115 25/32
Example First, we would like to translate the Point type into an equivalent data type in C using a struct. What should the attributes of the struct be? Mark G. Eramian, Ian McQuillan CMPT 115 26/32
Example In the main program, we declare a Point variable p, and we need to initialize the x and y coordinates to that of the origin, just like the Java constructor. How do we manipulate the x and y coordinate of p? How do we retrieve the x and y coordinates of p? Mark G. Eramian, Ian McQuillan CMPT 115 27/32
Example It would be nice to keep movediagonal in its own method, as it is a little bit more complex, and we may want to call it over and over again. We have to pass in p as a parameter to movediagonal. In C, when you pass in a variable as a parameter to a method, a copy of the variable contents is made and passed as a parameter. Therefore, if you changed po inside movediagonal, the contents of p in the main method would be left unchanged. However, we can return po back to the main function, and assign it to our original variable, p. We will implement the same program to C, in a more direct way starting on the next slide. Mark G. Eramian, Ian McQuillan CMPT 115 28/32
Pointers The most difficult difference between C and Java is in the use of so-called pointers. Pointers store addresses of variables. Every instance of a class in java is actually a pointer (ie. an address). In the Java line Person p = new Person(), the variable p is actually the address of a Person object, rather than the object itself. In C, if you have a struct called Person, and you declare a new variable Person p, the variable p actually contains the struct contents. Pointers were not discussed in 111, but we will discuss them in detail in Topic 3 of the class. Mark G. Eramian, Ian McQuillan CMPT 115 29/32
Pointers After we do Topic 3: References in the lectures, please return to the remainder of these slides. In what is to follow, we will translate the same Java program into C using a more direct approach. It will really help to understand exactly what Java is doing behind the scenes. Java hides a lot of the details of pointers, but it is important to understand what is actually happening. Mark G. Eramian, Ian McQuillan CMPT 115 30/32
Example See the files Point.java and Ian.java for the same java implementation of a simple program which implements a Point class, and uses a few methods from it. See the file PointApp.c for the equivalent implementation in C. Notice that for all methods in the Point class, the equivalent C implementation takes in the Point element as first parameter. Instead of saying p.setx(i), it is necessary in C to pass in p as the first parameter. The corresponding call in C would be setx(p, newx). This is because there are no instance methods in C. Mark G. Eramian, Ian McQuillan CMPT 115 31/32
Example The biggest change is that in the main program, we create a pointer to a Point. In Java, all object variables are actually pointers to the objects. For the methods setx, sety etc., we need to pass in a pointer to a Point as parameter, otherwise it would modify a copy of the Point, instead of the actual Point. This is just like the swap example in the Topic 3 notes. Mark G. Eramian, Ian McQuillan CMPT 115 32/32