UCSCBaskin School of Engineering  
General Information Events, News & Organizations Degrees & Departments Research Classes Admissions & Advising People & Jobs Administration
CMPS 012A/L - Week 7 Lab

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

  1. 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.
  2. 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.
  3. Start a new Java file (maybe Guess.java).
  4. Open guess.c and then begin to translate it into Java
    1. 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.
    2. The signature of the main method in Java is a bit different than in C. Change it to be like the Java main.
    3. 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).
    4. Change all printf() statements to System.out.println(). (Hint: look up what %d means in the pdf linked above.)
    5. 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;

    6. 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.
    7. For this simple program all methods should be static.
  5. Compile your Java program, and try to run it! It should be exactly or almost exactly like the original C file.

Java to C

  1. 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.
  2. Compile and run the Java file to see what it does (so you can compare it to the C program later).
  3. Start up a new C file (maybe twentyonepickup.c).
  4. Open TwentyOnePickup.java and then begin to translate it into C
    1. Replace the import java.util.*; statement with #include <stdio.h>
    2. The signature of the main method in Java is a bit different than in C. Change it to be as required by C.
    3. Change all System.out.println() statements to printf(). There is no string concatenation as in Java. In method printInstructions(), just use multiple printf() statements.
    4. 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).
    5. 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().
    6. Remove static from each of the methods.
    7. In Java, all methods/functions must be inside of some class. Not in C. Just delete the class wrapper.
    8. Change scan.nextInt() to scanf(). Don't forget the &.
  5. 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.

General info · News · Events · Degree Programs · Research · Classes · Admissions · Advising · People · Jobs · Administration
SOE Webmail · SOE SSH · SOE Wiki · Search · Sitemap · Contact us · Driving directions · Privacy · UCSC
© Baskin School of Engineering, University of California, Santa Cruz
1156 High St., Santa Cruz, CA 95064 · (831) 459-2158 · webmaster@soe.ucsc.edu