1 Java vs C This document explains some differences between the C and Java languages. Generally Java and C have a lot in common. In fact, C was a central criterion in designing the Java language. Java adds several object oriented features of which C has none. In a non technical sense, Java might be considered a superset of C, but it is slightly different on a few points. 2 Comments Comments in C begin with "/*" and end with "*/". Any newlines that might happen to be between them are ignored like the rest of the text. (Java accepts this type of comment too.) 3 The "public class..." keywords Every Java file is enclosed by "public class classname {" and a closing brace "}". (These are required as part of Java s object orientation.) C programs do not include these. 4 Function declarations When you declare a C function, you should not use the words "public static". These words are required due to Java s object orientation features. An example C function is /* * This is a C square function. */ int square(int n) { return n * n; } 5 Variable declarations All variable declarations for a C function must be at the top of the function. 6 Array declarations An array in C is declared "eltType arrayName [arrayLength]". For example, to declare an array of thirty characters in C, you would type "char a [30]". Java syntax for this is very different. 7 Returning arrays In Java, there is nothing wrong with returning an array. A C array declared in the form "char a [30]", however, should not be returned. Returning an array in C requires pointers. You might have to allocate memory for it with a call to malloc(). The return type would be a pointer to a char; i.e., char *. 8 Array parameters To have an array parameter, you would type "char a[]", not "char[] a" as in Java. 9 Array length The array length of C arrays is not available using "a.length". You must pass the length as another parameter. 10 The boolean type C does not include a boolean type. The int type can be used to make up for this. (C treats integers that are 0 as false and integers that are not 0 as true.)