Small or expected differences for Java programmers
There are no classes
Print with the function printf
,
not the PrintStream
method printf
. Also, root with
the function sqrt
,
not the
static method Math.sqrt
.
Include files
Almost all C programs will start with a standard set of includes from the C standard library something like the following.
#include <stdio.h> #include <stdlib.h> #include <math.h> #include <string.h> #include <stdint.h>
These are similar to the import
statement of Java but are
not restricted to information about one class.
In general, whenever you use a standard C routine, you should add
the appropriate include
statements.
You can find these by looking at the
man page for the routine
or by consulting the C standard.
The .h at the end of an include file in a one letter abbreviation for header. On Unix systems, the include files are usually stored in the /usr/include directory.
Scanning input
C has a scanf
for reading input.
C’s I/O functions use a format descriiptor inspired by
FORTRAN’s
FORMAT
statement.
The Java Formatter
is an extension of scanf
.
In the old days, students were introduced to scanf
and printf
,
along with the mysterious &
operator, which allows you
to change the value of a parameter,
in the first week of their
introductory programming class.
printf("Enter two numbers to exponentiate\n") ; scanf("%f %f", &base, &power) ; printf("%f^^%f = %f\n", base, power, pow(base, power)) ;
There is no boolean
C does not have a boolean
.
C uses integers to represent true
and false
.
The integer 0 is interpreted as false
and any non-zero
integer is interpreted as true
in C control structures.
The following code will print the position of
the first element of an array containing zero.
while (i=0; A[i]; ++i) ; printf("The first zero element is at position %d\n", i) ;
In Java, you’d use the test A[i] != 0
.
For readability, C programmers should include the test.
This also means that the logical operators may be applied to integers.
0 && 0
is0
0 && 5
is0
5 && 0
is0
5 && 5
is1
And, the relational operators always yields either 0 or 1.
0 < 0
is0
0 == 0
is1
0 > 0
is0
Sometimes that allows you to write succinct, but confusing code, which some C programmers will argue is very efficient. For example, you can count the number of positive elements in an array with the following loop.
for (i=0, posCount=0; i<n; posCount += A[i++] >= 0) ;
Of course, it is considered bad style to use the results of logical
and relational operators as int
’s.
Be careful with equality tests. The following if
is legal C.
if (i=0) { zeroCount = zeroCount + 1 ; }
The C99 standard, when used with the appropriate include file, does
allow the use of the type bool
with values
true
and false
.
There is no length operator for arrays
C arrays are not objects. Unless you are using pointers (more on that later), they are declared with a fixed length.
int X[1000], Y[1000] ;
When writing functions that use arrays, it is necessary to use a parameter type in which the size of the array is omitted. and explicitly pass the length of the array.
int add_elements(int X[], int n) { int i ; int sum = 0 ; for (i=0; i<n; ++i) { sum = sum + X[i] ; } return sum ; }
There is no string type
In C, strings are implemented as null-terminated arrays of characters. Suppose you use the following statement to declare and initialize an array.
char[] course = "CSCI 320" ;
This statement actually allocates nine characters for the array.
The value of course[7]
is '0'
and the
value of course[8]
is '\0'
, the null
character.
Here’s an example of a C routine that counts the number of times the exclamation point appears in a string.
int shriek_count(char X[]) { int i ; int count = 0 ; for (i=0; X[i] != '\0'; ++i) { if (X[i] == '!') { count = count + 1 ; } } return count ; }
Arrays are declared differently in C
As you may have noticed, the following declaration is not allowed in C.
int[] X ;
The braces must follow the identifier name.
There are signed and unsigned numbers
In C, the integer types can be declared as signed
or unsigned
.
Although unsigned variables are appropriate for some “real world”
applications which you know only deal with positive numbers, you rarely
see unsigned
in C code.
There are no standard sizes for the number types
In Java, you know that an int
is stored
in a 32-bit twos-complement number. Consequently, you know that the
largest int
is 2147483647 and the smallest
int
is -2147483648.
In C, you know
that an int
is at least as big as a short
and that a int
is no longer than a long
.
Starting with the C99 standard, an include file allows the use of
types, such as int32_t
and
uint32_t
, that guarantee the size of integers.
This are useful in programming small microcontrollers where, by default,
integers are usually 16 bits wide.
(The Arduino
int
is 16 bits.)
Operators
C and Java have the same set of operators with a few exceptions.
- Java has a
>>>
that performs a logical right shift on an integer. Java’s>>>
performs an arithmetic right shift. For the C>>
operator; the logical right shift is used forunsigned
numbers, and the arithmetic right shift is used forsigned
numbers. - Java also has a corresponding
>>>=
assignment opeator. - Java has the
instanceof
operator for testing the type of an object. - C has a
sizeof
operator that returns the number of bytes required to store a value or “type”. Oddly enough, it issizeof(int)
andsizeof 202
. - C has a unary operator
&
that returns the address of an lvalue and a unary operator*
that returns the value stored at an address.
Old style declarations
Before C99, C only allowed variables of a function to be declared at the beginning of a code block. If you are accustomed to writing C++/Java-style loop declarations, you must compile your program with C99 enabled.
Large or unexpected differences for Java programmers
Here are some notes I wrote for the Fall 2011 offering of ECE 209 that illustrate some of the more significant differences between C and Java. We spent six weeks on this in ECE 209, but we will speed through in this course.
- Modifying function arguments with pointers (see the “Returning multiple values from a function” section).
- Makefile example (an illustration of a common compilation control mechanism)
- arrays and pointers
- strings and command line arguments
- variable scope
- bit fields (though odd, these are useful for embedded system programming)
- the C preprocessor