Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

1Learning Outcomes

2Hello 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!");
  }
}

2.1Highlights

2.2Run Demo

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

3C vs. Java

Table 1 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.

Table 1:(a) C vs. Java; (b) similar operators

FeatureCJava
Language Paradigm[1]Function Oriented (programming unit: function)Object Oriented (programming unit: Class = Abstract Data Type)
Compilation[2]gcc hello.c creates machine language codejavac Hello.java creates Java virtual machine language bytecode
Execution[2]./a.out loads, executes programjava Hello interprets bytecodes
Dynamic Memory Management[3]Manual (malloc, free) (more later)Automatic garbage collection; new both allocates and initializes
Variable declarationTyped declaration; declare before you use it(same)
Function declarationUse curly braces. void means no return value(same)
Accessing a library#include <stdio.h>import java.util.*
CommentsMultiline: /* ... */, end-of-line: // ...(same)

(a)

OperatorC and Java
arithmetic+, -, *, /, %
assignment=
augmented assignment+=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=
bitwise logic~, &, |, ^
bitwise shifts<<, >>
boolean logic!, &&, ||
equality testing==, !=
subexpression grouping()
order relations<, <=, >, >=
increment and decrement++, --
member selection[4]., ->
conditional evaluation[5]? :

(b)

4More Highlights

1. Variable naming convention: In C, use snake_case[6], 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:

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 the control structure are considered outside of the body, omitting curly braces leads to many debugging errors[9]:

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.

  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. Like Python, C arrays and string are zero-indexed.