C vs. Java
Learning Outcomes
- Use a “Hello World” program to intuit C program structure.
- Do a cursory comparison of C and Java.
🎥 Lecture Video
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 includestdioforprintf(), which prints to stdout (here, the command-line). - There is a
mainfunction.- C is function-oriented; unlike Java, this is not an object’s method.
- The return type of
mainis notvoid; it’s an integer. - By convention, C programs return
0on 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.
Compile and run C
To run this program, use the command-line program, gcc, to compile the program. This creates a binary program with the default name a.out (why the naming?). Then, run the binary program.
$ gcc hello_world.c
$ ./a.out
Hello World!
In practice, rename the binary to something more meaningful, like hello_world:
$ gcc -o hello_world hello_world.c
$ ./hello_world
Hello World!
You will also find it useful to generate debugging symbols for gdb, our debugger.
$ gcc -d -o hello_world hello_world.c
$ gdb hello_world
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:
argcis 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,argcis2.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 stringmy_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
-
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++. ↩
-
We’ve discussed this in detail in a previous section. ↩ ↩2
-
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. ↩
-
Slightly different than Java because there are both structures and pointers to structures, more next time ↩
-
cond ? body_true : body_false↩ -
Like Python, C arrays and strings are zero-indexed. ↩
-
Stack Overflow: Is it a bad practice to use an if-statement without curly braces? ↩