Introduction to C
For the week of Feb 20 - 24, 2006
Up until this point, we've been shown only one programming language in class and in lab--Java.
In the wider world of programming, there are many other useful and commonly used languages.
One language, which is older but still used often, is called C. C and its cousin C++ have a
syntax very similar to Java. Where Java has classes, C does not (but C++ later added them).
In this lab, we will try to convert a C program into a Java program. If time allows, we
will then try to go back the other way (Java to C program).
Additional Information
For an introduction to C, please check out the first chapter of C for
Java Programmers: A Primer.
Procedure
C to Java
- Download (Save As...) guess.c onto a Unix machine. (Windows users
should log on to one of the Unix servers, and do this lab there.) The Java will compile
and run on your Windows machine fine if you set it up right, but unless you have additional
software on your computer, you will be unable to compile the C file.
- Compile and run the C file to see what it does (so you can compare it to the Java program later).
See page 7 of the PDF for instructions on how to do that.
- Start a new Java file (maybe Guess.java).
- Open guess.c and then begin to translate it into Java
- The closest thing in Java to C's
#include stdio.h is like java.lang
(which is always automatically imported by Java) so no import is needed for that.
Ignore the #include.
- The signature of the
main method in Java is a bit different than in
C. Change it to be like the Java main.
- In Java, all methods/functions must be inside of some class. Put a class wrapper
around all of the methods (actually all of the code for Guess.java).
- Change all
printf() statements to System.out.println().
(Hint: look up what %d means in the pdf linked
above.)
- Constants in C are done with
#define. In Java we use static
final. Change all of the #defines.
For example: #define PI 3.14159 becomes static final double PI
= 3.14159;
- Change
scanf() to input.nextInt() where input
is a Scanner object. Of course make sure you create the Scanner before that in main.
You will then need to pass the Scanner as a third argument to makeAGuess().
- Don't forget to add in
import java.util.*; so you can use the
Scanner class.
- For this simple program all methods should be static.
- Compile your Java program, and try to run it! It should be exactly or almost exactly like the
original C file.
Java to C
- Download (Save As...) TwentyOnePickup.java onto a
Unix machine. (Windows users should log on to one of the Unix servers, and do this lab there.)
The Java will compile and run on your Windows machine fine if you set it up right, but unless
you have additional software on your computer, you will be unable to compile the C file.
- Compile and run the Java file to see what it does (so you can compare it to the C program later).
- Start up a new C file (maybe twentyonepickup.c).
- Open TwentyOnePickup.java and then begin to translate it into C
- Replace the
import java.util.*; statement with #include
<stdio.h>
- The signature of the main method in Java is a bit different than in C. Change it
to be as required by C.
- Change all
System.out.println() statements to printf().
There is no string concatenation as in Java. In method printInstructions(),
just use multiple printf() statements.
- Constants in C are done with
#define. There is no boolean type, but
you can make your C program look almost like the Java program by defining two constants
like this:
#define TRUE 1
#define FALSE 0
Then use type int instead of boolean and replace "true" with
"TRUE" and "false" with "FALSE" (not quotation marks of course).
- In Java, the scope of a method definition is the entire class, regardless of where
in the class the definition occurs. In C, a definition must come before a use, or at
least a function prototype must come before the use (see C for Java
Programmers section 2.1). Either move
main() to the end of your C
program or add function prototypes for all of the functions except main, placing
those prototypes before the definition of main().
- Remove static from each of the methods.
- In Java, all methods/functions must be inside of some class. Not in C. Just delete
the class wrapper.
- Change
scan.nextInt() to scanf(). Don't forget the &.
- Compile your C program, and try to run it! See page 7 of the PDF
for instructions on how to do that. It should be exactly or almost exactly like the
original Java file.
|