▶ 目录 ← 首页
CS61C

C vs. Java

Learning Outcomes

  • Use a “Hello World” program to intuit C program structure.
  • Do a cursory comparison of C and Java.

Hello World

C program: hello_world.c


#include <stdio.h>
int main(int argc, char *argv[]) {
  printf("Hello World!\n");
  return 0;
}

Java program: hello.java


public class HelloWorld {
  public static void main(String[] args) {
    System.out.println("Hello world!");
  }
}

Highlights

  • In C, we import libraries using #include. Here, we include stdio for printf(), which prints to stdout (here, the command-line).
  • There is a main function.
    • C is function-oriented; unlike Java, this is not an object’s method.
    • The return type of main is not void; it’s an integer.
    • By convention, C programs return 0 on success. (The main rationale is it is easier to check equality to zero; we return to this soon)

Run Demo

The below instructions are mostly for reference. Refer to this section to understand details.

C vs. Java

#tab-c-vs-java below is adapted from the C Programming vs. Java Programming table, created for Princeton University’s introductory CS sequence. Hover over the footnote for more information about each row.

More Highlights

1. Variable naming convention: In C, use snake_case6, NOT camelCase 7.

2. Command-line arguments: In our hello_world.c program, the main function can take in command-line arguments with two parameters:

  • argc is an integer count of how many arguments you have. The executable itself counts as one argument. If you run something like ./hello_world my_file, argc is 2.
  • argv: is a pointer to an array of the arguments themselves, as C strings. We discuss pointers, arrays, and strings in more detail next time. For now, if you run ./hello_world my_file, the first8 argument is the path of the program itself (./hello_world) and the second argument is the string my_file.

Command-line arguments: What looks familiar about array syntax?


#include <stdio.h>
int main(int argc, char *argv[]) {
  printf("Received %d args\n", argc);
  for (int i = 0; i < argc; i++) {
    printf("arg %d: %s\n", i, argv[i]);
  }
  return 0;
}

3. Curly braces: The C language allows for some omission of curly braces for single-line statements—even for control structures like if-else and for. This is the same as in Java, but we didn’t tell you. :-)

But just because you can, doesn’t mean you should. Because subsequent lines of the control structure are considered outside of the body, omitting curly braces leads to many debugging errors9:

Omitting curly braces: Which lines are printed?


#include <stdio.h>
int main(int argc, char *argv[]) {
    int x = 0;
    if (x == 0)
        printf("x is 0\n");
    if (x != 0) // careful!
        printf("x not 0 line 1\n");
        printf("x not 0 line 2\n");
    return 0;
}

Footnotes

  1. Java is an object-oriented language; C is mostly a functional language. In C, the core idea is the function, whereas in Java, it’s the class or abstract data type. While it is possible to write object-like code in C, if programs required objects then you should really be using C++. ↩

  2. We’ve discussed this in detail in a previous section. ↩ ↩2

  3. Java manages memory for you with garbage collection. In C, all the safety belts and padded rooms are gone; you do all the memory management yourself, and you can get in trouble very quickly. More next time. ↩

  4. Slightly different than Java because there are both structures and pointers to structures, more next time ↩

  5. cond ? body_true : body_false ↩

  6. Wikipedia ↩

  7. Wikipedia ↩

  8. Like Python, C arrays and strings are zero-indexed. ↩

  9. Stack Overflow: Is it a bad practice to use an if-statement without curly braces? ↩