C for Java Programmers

©2002 Sridhar Narayan

Take the Java program listed below:

public class FooBar {

public static void main(String [] args) {

System.out.println("Hello world\n");
}
}

Now consider the same program, modified as shown below:

public class FooBar {

public static void main(String [] args) {

System.out.printfln("Hello world\n");
}
}
This is an equivalent C program. Since C is NOT an object oriented language, it DOES NOT HAVE
the following concepts:
A C program is a collection of procedures. You can think of a procedure as a method. Procedures are named chunks of code, can be passed parameters, and typically return results. See example below, which defines a C program that has two procedures named main and sum, both defined in the same file:


main() {
  int x = sum(4, 5);
}
int sum(int a, int b) {
   int result;
   result = a+b;
   return result;
}
 A C procedure is a collection of statements, including calls to other  procedures, and has the same standard constructs like if statements, while loops, for loops, case statements etc. All these are expressed the same way as they are in Java.

Some important differences

Pointers

C programs make extensive use of pointers. Consider the declaration int x; This allocates storage for x in memory. A reference to x in a program is a reference to the value stored at that location in memory . A reference to the address of x is a pointer to x .

Pointers are declared like this :

int *y;


This says that y points to a location that holds an integer value. That is, y contains an address. At declaration, the address pointed to by y is unpredictable. One can initialize y, for instance,  to hold the address of x by writing:

y = &x;

&x refers to the address at which the value corresponding to x is stored in memory. Any reference to y is a reference to this address. The contents of that location, that is the value of x, can be accessed by dereferencing the pointer:

int z = *y;

*y refers to the contents of the location pointed to by y. Thus, the integer z acquires the value of the variable x. One can treat *y exactly the same way as one would treat an integer variable.

Pointer Arithmetic

The expression y+1 is a pointer to the address 1 unit away from the address pointed to by y. How far away this is depends on the number of bytes allocated to store a variable of the data type of y . If y where a char pointer, y+1 would point to the next byte. If y is an int pointer, y+1 points to an address four bytes away, assuming ints are stored in 4 bytes (32 bits).

The Array View

The previous discussion suggests that pointers can also be viewed as array references. A declaration of the form int a[10];, allocates storage for an array of 10 integers. The name of the array, a, is a pointer to the first element of the array. Thus a is the same as &a[0] . a[i] is, of course, the ith element of the array.

Pointers can also be regarded as array references. Thus, the pointer y (declared as int *y) can be regarded as the name of an integer array stored at that location. y[0] is the first element of this array, and refers to the same value as *y. y[1] is the next element and refers to the same value as *(y+1).

Structs

structs are a collection of (typically) dissimilar data types.

typedef struct {
  float bodyTemp;
  int age;
} Person;
This declaration defines a data type named Person with the described structure. You can declare Person p; and refer to the attributes of p. p.age is a reference to p's age. p.name[5] refers to the 6th character of p's name.

Structures are normally passed to procedures by reference. That is, a pointer to the structure is passed, as in &p. A procedure initialize that initializes a Person's name and age would be invoked in a function, say main, as
main () {
  Person p;
  initialize(&p);

printf("%d %f\n", p.age, p.bodyTemp);
}
The procedure would be declared as

void initialize(Person *q) {

 
     q->age = 32;
     q->bodyTemp = 98.4;
}
Since q is a pointer to a Person, not a Person, we use the q->age notation (instead of q.age).

Examples of code that illustrate these ideas can be found here , and here.